Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix for #379, save() ignores write failures #380

Merged
merged 3 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/annoylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <sys/types.h>
#include <fcntl.h>
#include <stddef.h>

#if defined(_MSC_VER) && _MSC_VER == 1500
typedef unsigned char uint8_t;
typedef signed __int32 int32_t;
Expand All @@ -44,6 +45,7 @@ typedef unsigned __int64 uint64_t;
#include <sys/mman.h>
#endif

#include <cerrno>
#include <string.h>
#include <math.h>
#include <vector>
Expand Down Expand Up @@ -878,11 +880,25 @@ template<typename S, typename T, typename Distance, typename Random>
if (f == NULL)
return false;

fwrite(_nodes, _s, _n_nodes, f);
fclose(f);
bool write_failed = false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't think this one is really needed, could just return false at each point?

not blocking though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored that a few times deciding if I wanted to unload or not in case of error. That is a vestige of that.

size_t write_result = fwrite(_nodes, _s, _n_nodes, f);
if (write_result != (size_t) _n_nodes) {
showUpdate("Unable to write %s\n", strerror(errno));
write_failed = true;
}

int close_result = fclose(f);
if (close_result == EOF) {
showUpdate("Unable to close %s\n", strerror(errno));
write_failed = true;
}

unload();
return load(filename, prefault=false);
if (write_failed) {
return false;
} else {
unload();
return load(filename, prefault=false);
}
}
}

Expand Down
34 changes: 33 additions & 1 deletion test/index_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
# the License.

import os
import sys
import random
from common import TestCase
from annoy import AnnoyIndex


class IndexTest(TestCase):
def test_not_found_tree(self):
i = AnnoyIndex(10)
Expand Down Expand Up @@ -172,3 +172,35 @@ def test_get_n_trees(self):
i = AnnoyIndex(10)
i.load('test/test.tree')
self.assertEqual(i.get_n_trees(), 10)

def test_write_failed(self):
f = 40

# Build the initial index
t = AnnoyIndex(f)
for i in range(1000):
v = [random.gauss(0, 1) for z in range(f)]
t.add_item(i, v)
t.build(10)

if sys.platform == "linux" or sys.platform == "linux2":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# linux
try:
t.save("/dev/full")
self.fail("didn't get expected exception")
except Exception as e:
self.assertTrue(str(e).find("No space left on device") > 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python nit: could do self.assertTrue("No space left on device" in str(e)) or in newer versions, self.assertIn("No space left on device", str(e))

elif sys.platform == "darwin":
volume = "FULLDISK"
device = os.popen('hdiutil attach -nomount ram://64').read()
os.popen('diskutil erasevolume MS-DOS %s %s' % (volume, device))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but I'm pretty wary of calling diskutil erasevolume from a unit test. Is there a simpler way we can cause an error? (Maybe writing to a special file, like a named pipe or socket instead?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure a pipe would work. Might just block rather than out of disk space. I agree it is a bit wonky. Possibly we just test this on linux? Where it is clear and concise?

os.popen('touch "/Volumes/%s/full"' % volume)
try:
t.save('/Volumes/%s/annoy.tree' % volume)
self.fail("didn't get expected exception")
except Exception as e:
self.assertTrue(str(e).find("No space left on device") > 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

finally:
os.popen("hdiutil detach %s" % device)