pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

Documentation should specify if the GIL helpers are reentrant and if they can be called without an ambient GIL

Open ezyang opened this issue 4 years ago • 2 comments

In https://pybind11.readthedocs.io/en/stable/advanced/misc.html#global-interpreter-lock-gil it says that gil_scoped_release and gil_scoped_acquire are available, and doesn't elaborate further. But there are some important correctness conditions related to the use of these functions:

  1. Is it valid to gil_scoped_acquire, and then immediately gil_scoped_acquire again? (I... think this is supposed to be yes?) Ditto with gil_scoped_release. (I have no idea! See 3)
  2. Is it OK to mix these functions with traditional PyGILState functions? (According to #1276, no)
  3. Is it OK to call gil_scoped_release in a context where you never had the GIL to begin with? (Empirically, no.)

The documentation should address all these questions.

ezyang avatar Apr 19 '21 16:04 ezyang

Any update on this?

harsh1995 avatar Mar 27 '22 05:03 harsh1995

Looking at the destructor of gil_scoped_release it seems the answer to 3 is that this is allowed

    ~gil_scoped_release() {
        if (!tstate) {
            return;
        }

Which is probably to make nesting of gil_scoped_release objects a no-op (?)

Tishj avatar Feb 26 '24 14:02 Tishj