Skip to content

feat: add disarm() to the simple GIL scoped classes - #6313

Open
henryiii wants to merge 2 commits into
pybind:masterfrom
henryiii:feat/simple-gil-disarm
Open

henryiii wants to merge 2 commits into
pybind:masterfrom
henryiii:feat/simple-gil-disarm

Conversation

@henryiii

@henryiii henryiii commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

I think this is the main blocker to using PYBIND11_SIMPLE_GIL_MANAGEMENT for projects like PyTorch. This should enable us to deprecate the complex version if it works out.

🤖 AI text below 🤖

Description

The default gil_scoped_acquire and gil_scoped_release have disarm(). With PYBIND11_SIMPLE_GIL_MANAGEMENT (always on for PyPy, GraalPy, and the stable ABI), these names are aliases for the _simple classes, which did not have disarm(). Code that calls it, such as PyTorch's autograd engine, did not compile in that mode.

This PR adds disarm() to both _simple classes. After disarm():

  • gil_scoped_release_simple does not call PyEval_RestoreThread in its destructor.
  • gil_scoped_acquire_simple does not call PyGILState_Release in its destructor.

The new tests in test_gil_scoped check the GIL state after a disarmed scope ends, then do the skipped cleanup. Under Py_LIMITED_API they cannot check the GIL state (PyGILState_Check is 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-op disarm(), both tests fail.

Testing beyond CI: an embedded program where a worker thread is in a gil_scoped_release scope while the main thread finalizes the interpreter. In simple mode on CPython 3.12 and 3.14, the worker thread finishes if it calls disarm(), and never finishes if it does not.

Suggested changelog entry:

  • Added disarm() to gil_scoped_acquire and gil_scoped_release when PYBIND11_SIMPLE_GIL_MANAGEMENT is defined (always on for PyPy, GraalPy, and the stable ABI).

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
@henryiii
henryiii marked this pull request as ready for review November 26, 2026 00:10
@rwgk

rwgk commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

— codex on behalf of Ralf —

Restore the saved thread state reliably in the release test. The new test uses PyGILState_Check() to decide whether to reattach before returning to Python. CPython makes that function always return true once a subinterpreter has existed. With the unchanged test body on CPython 3.15.2, creating and destroying a subinterpreter first made the test return while detached and segfault, on both regular and free-threaded builds. The same test passed in a fresh process. Please use an actual thread-state query where available, or run this probe in its own subprocess. Release probe, CPython API

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
@henryiii

Copy link
Copy Markdown
Collaborator Author

🤖 AI text below 🤖

Confirmed. On CPython 3.13, test_release_disarm segfaults if a subinterpreter was created and destroyed first.

Fixed in the latest commit: gil_held() now uses py::detail::get_thread_state_unchecked() != nullptr on CPython. PyPy and GraalPy keep PyGILState_Check(), because they have no subinterpreters.

Checked with and without a prior subinterpreter on 3.13, 3.15t, and 3.14 with PYBIND11_SIMPLE_GIL_MANAGEMENT.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants