Skip to content

Commit

Permalink
Don't throw exceptions when logging
Browse files Browse the repository at this point in the history
When NMSLIB is logging to the python logger, there can occasionally be exceptions
be thrown on system exit (https://github.com/nmslib/nmslib/issues/327).

Fix by trapping these exceptions since making a log call shouldn't throw an exception.
  • Loading branch information
Ben Frederickson committed Jul 2, 2018
1 parent 6280786 commit 4a8026a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
43 changes: 26 additions & 17 deletions python_bindings/nmslib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -388,23 +388,32 @@ class PythonLogger
int line,
const char * function,
const std::string & message) {
AcquireGIL l;
switch(severity) {
case LIB_DEBUG:
inner.attr("debug")(message);
break;
case LIB_INFO:
inner.attr("info")(message);
break;
case LIB_WARNING:
inner.attr("warning")(message);
break;
case LIB_ERROR:
inner.attr("error")(message);
break;
case LIB_FATAL:
inner.attr("critical")(message);
break;
// In cases when the interpreter was shutting down, attempting to log in python
// could throw an exception (https://github.com/nmslib/nmslib/issues/327).
// Logging shouldn't cause exceptions, so catch it and dump to stderr instead.
try {
AcquireGIL l;
switch(severity) {
case LIB_DEBUG:
inner.attr("debug")(message);
break;
case LIB_INFO:
inner.attr("info")(message);
break;
case LIB_WARNING:
inner.attr("warning")(message);
break;
case LIB_ERROR:
inner.attr("error")(message);
break;
case LIB_FATAL:
inner.attr("critical")(message);
break;
}
} catch (const std::exception & e) {
std::cerr << "Failed to log '" << message << "'. Exception:" << e.what() << std::endl;
} catch (...) {
std::cerr << "Failed to log '" << message << "'" << std::endl;
}
}
};
Expand Down
6 changes: 6 additions & 0 deletions python_bindings/tests/bindings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,11 @@ def testSparse(self):
self.assertEqual(index[3], [(3, 1.0)])


class GlobalTestCase(unittest.TestCase):
def testGlobal(self):
# this is a one line reproduction of https://github.com/nmslib/nmslib/issues/327
GlobalTestCase.index = nmslib.init()


if __name__ == "__main__":
unittest.main()

0 comments on commit 4a8026a

Please sign in to comment.