Use PyGILState_GetThisThreadState when using gil_scoped_acquire. (#1211)

This avoids GIL deadlocking when pybind11 tries to acquire the GIL in a thread that already acquired it using standard Python API (e.g. when running from a Python thread).
This commit is contained in:
Borja Zarco
2018-12-01 08:47:40 -05:00
committed by Wenzel Jakob
parent 81da9888c7
commit e2b884c33b
4 changed files with 133 additions and 0 deletions

View File

@@ -1871,6 +1871,15 @@ public:
auto const &internals = detail::get_internals();
tstate = (PyThreadState *) PYBIND11_TLS_GET_VALUE(internals.tstate);
if (!tstate) {
/* Check if the GIL was acquired using the PyGILState_* API instead (e.g. if
calling from a Python thread). Since we use a different key, this ensures
we don't create a new thread state and deadlock in PyEval_AcquireThread
below. Note we don't save this state with internals.tstate, since we don't
create it we would fail to clear it (its reference count should be > 0). */
tstate = PyGILState_GetThisThreadState();
}
if (!tstate) {
tstate = PyThreadState_New(internals.istate);
#if !defined(NDEBUG)