Skip to content

Commit

Permalink
Merge pull request #292 from benfred/gil_deadlock_fix
Browse files Browse the repository at this point in the history
Fix deadlock in child threads launched from python
  • Loading branch information
Leonid Boytsov authored and GitHub committed Feb 16, 2018
2 parents b9cfcf4 + 6e3f91d commit f8f2514
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Non-Metric Space Library (NMSLIB)
=================
The latest **pre**-release is [1.7](https://github.com/searchivarius/nmslib/releases/tag/v1.7). Note that the manual is not updated to reflect some of the changes. In particular, we changed the build procedure for Windows. Also note that the manual targets primiarily developers who will extend the library. For most other folks, [Python binding docs should be sufficient](python_bindings).
The latest **pre**-release is [1.7.1](https://github.com/searchivarius/nmslib/releases/tag/v1.7.1). Note that the manual is not updated to reflect some of the changes. In particular, we changed the build procedure for Windows. Also note that the manual targets primiarily developers who will extend the library. For most other folks, [Python binding docs should be sufficient](python_bindings).
-----------------
Non-Metric Space Library (NMSLIB) is an **efficient** cross-platform similarity search library and a toolkit for evaluation of similarity search methods. The core-library does **not** have any third-party dependencies.

Expand Down
20 changes: 17 additions & 3 deletions python_bindings/nmslib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ struct IndexWrapper {
loadParams(space_params))) {
auto vectSpacePtr = dynamic_cast<VectorSpace<dist_t>*>(space.get());
if (data_type == DATATYPE_DENSE_VECTOR && vectSpacePtr == nullptr) {
throw std::invalid_argument("The space type " + space_type +
throw std::invalid_argument("The space type " + space_type +
" is not compatible with the type DENSE_VECTOR, only dense vector spaces are allowed!");
}
auto vectSiftPtr = dynamic_cast<SpaceL2SqrSift*>(space.get());
if (data_type == DATATYPE_DENSE_UINT8_VECTOR && vectSiftPtr == nullptr) {
throw std::invalid_argument("The space type " + space_type +
throw std::invalid_argument("The space type " + space_type +
" is not compatible with the type DENSE_UINT8_VECTOR!");
}
}
Expand Down Expand Up @@ -361,6 +361,20 @@ struct IndexWrapper {
ObjectVector data;
};

// pybind11::gil_scoped_acquire can deadlock when acquiring the GIL on threads
// created from python (https://github.com/searchivarius/nmslib/issues/291)
// This might be fixed in a future version of pybind11 (https://github.com/pybind/pybind11/pull/1211)
// but until then, lets fall back to the python c-api to fix.
struct AcquireGIL {
PyGILState_STATE state;
AcquireGIL()
: state(PyGILState_Ensure()) {
}
~AcquireGIL() {
PyGILState_Release(state);
}
};

class PythonLogger
: public Logger {
public:
Expand All @@ -374,7 +388,7 @@ class PythonLogger
int line,
const char * function,
const std::string & message) {
py::gil_scoped_acquire l;
AcquireGIL l;
switch(severity) {
case LIB_DEBUG:
inner.attr("debug")(message);
Expand Down

0 comments on commit f8f2514

Please sign in to comment.