Repository navigation
Conversation
gil_scoped_acquire_simple and gil_scoped_release_simple now have disarm(), like the default classes in gil.h. After disarm(), the destructor does not call PyGILState_Release or PyEval_RestoreThread. Code that calls disarm() now compiles with PYBIND11_SIMPLE_GIL_MANAGEMENT (PyPy, GraalPy, stable ABI). Assisted-by: ClaudeCode:claude-opus-5-5
|
— codex on behalf of Ralf — Restore the saved thread state reliably in the release test. The new test uses Generated by Codex. Ralf has yet to review these findings. |
CPython makes PyGILState_Check always return 1 after a subinterpreter was created. The release test then returned to Python without a thread state and crashed. Use the current thread state on CPython. Assisted-by: ClaudeCode:claude-fable-5-2
|
🤖 AI text below 🤖 Confirmed. On CPython 3.13, Fixed in the latest commit: Checked with and without a prior subinterpreter on 3.13, 3.15t, and 3.14 with |
I think this is the main blocker to using
PYBIND11_SIMPLE_GIL_MANAGEMENTfor projects like PyTorch. This should enable us to deprecate the complex version if it works out.🤖 AI text below 🤖
Description
The default
gil_scoped_acquireandgil_scoped_releasehavedisarm(). WithPYBIND11_SIMPLE_GIL_MANAGEMENT(always on for PyPy, GraalPy, and the stable ABI), these names are aliases for the_simpleclasses, which did not havedisarm(). Code that calls it, such as PyTorch's autograd engine, did not compile in that mode.This PR adds
disarm()to both_simpleclasses. Afterdisarm():gil_scoped_release_simpledoes not callPyEval_RestoreThreadin its destructor.gil_scoped_acquire_simpledoes not callPyGILState_Releasein its destructor.The new tests in
test_gil_scopedcheck the GIL state after a disarmed scope ends, then do the skipped cleanup. UnderPy_LIMITED_APIthey cannot check the GIL state (PyGILState_Checkis not in the stable ABI), so they only run the code. Before the fix, the tests do not compile in simple mode. With a no-opdisarm(), both tests fail.Testing beyond CI: an embedded program where a worker thread is in a
gil_scoped_releasescope while the main thread finalizes the interpreter. In simple mode on CPython 3.12 and 3.14, the worker thread finishes if it callsdisarm(), and never finishes if it does not.Suggested changelog entry:
disarm()togil_scoped_acquireandgil_scoped_releasewhenPYBIND11_SIMPLE_GIL_MANAGEMENTis defined (always on for PyPy, GraalPy, and the stable ABI).