Skip to content

Avoid thread termination in scoped_released #2657

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 19, 2020
Merged
Changes from 1 commit
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
44 changes: 29 additions & 15 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1168,17 +1168,6 @@ inline void add_class_method(object& cls, const char *name_, const cpp_function
}
}

// Thread state manipulation C API should not be called while Python runtime is finalizing.
// For more detail see https://docs.python.org/3.7/c-api/init.html#c.PyEval_RestoreThread
// `is_finalizing()` provides a version agnostic way to check if runtime is finalizing.
inline bool is_finalizing() {
#if PY_VERSION_HEX >= 0x03070000
return _Py_IsFinalizing();
#else
return false;
#endif
}

PYBIND11_NAMESPACE_END(detail)

/// Given a pointer to a member function, cast it to its `Derived` version.
Expand Down Expand Up @@ -2132,13 +2121,21 @@ class gil_scoped_acquire {
pybind11_fail("scoped_acquire::dec_ref(): internal error!");
#endif
PyThreadState_Clear(tstate);
if (!detail::is_finalizing())
if (active)
PyThreadState_DeleteCurrent();
PYBIND11_TLS_DELETE_VALUE(detail::get_internals().tstate);
release = false;
}
}

/// This method will disable the PyThreadState_DeleteCurrent call. This
/// should be called if the interpreter is shutting down, as the thread
/// deletion is not allowed during shutdown.
/// (_Py_IsFinalizing() on Python 3.7+)
PYBIND11_NOINLINE void disarm() {
active = false;
}

PYBIND11_NOINLINE ~gil_scoped_acquire() {
dec_ref();
if (release)
Expand All @@ -2147,6 +2144,7 @@ class gil_scoped_acquire {
private:
PyThreadState *tstate = nullptr;
bool release = true;
bool active = true;
};

class gil_scoped_release {
Expand All @@ -2162,11 +2160,20 @@ class gil_scoped_release {
PYBIND11_TLS_DELETE_VALUE(key);
}
}
//
/// This method will disable the PyThreadState_DeleteCurrent call. This
/// should be called if the interpreter is shutting down, as the thread
/// deletion is not allowed during shutdown.
/// (_Py_IsFinalizing() on Python 3.7+)
PYBIND11_NOINLINE void disarm() {
active = false;
}

~gil_scoped_release() {
if (!tstate)
return;
// `PyEval_RestoreThread()` should not be called if runtime is finalizing
if (!detail::is_finalizing())
if (active)
PyEval_RestoreThread(tstate);
if (disassoc) {
auto key = detail::get_internals().tstate;
Expand All @@ -2176,24 +2183,31 @@ class gil_scoped_release {
private:
PyThreadState *tstate;
bool disassoc;
bool active = true;
};
#elif defined(PYPY_VERSION)
class gil_scoped_acquire {
PyGILState_STATE state;
public:
gil_scoped_acquire() { state = PyGILState_Ensure(); }
~gil_scoped_acquire() { PyGILState_Release(state); }
void disarm() {}
};

class gil_scoped_release {
PyThreadState *state;
public:
gil_scoped_release() { state = PyEval_SaveThread(); }
~gil_scoped_release() { PyEval_RestoreThread(state); }
void disarm() {}
};
#else
class gil_scoped_acquire { };
class gil_scoped_release { };
class gil_scoped_acquire {
void disarm() {}
};
class gil_scoped_release {
void disarm() {}
};
#endif

error_already_set::~error_already_set() {
Expand Down