This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Everyone uses GIL wrong! = DEADLOCK
Type: crash Stage: resolved
Components: Interpreter Core Versions: Python 3.7
Status: closed Resolution: duplicate
Dependencies: Superseder: [subinterpreters] Make the PyGILState API compatible with multiple interpreters
View: 10915
Assigned To: Nosy List: Roffild, eric.snow, vstinner
Priority: normal Keywords:

Created on 2019-06-07 00:03 by Roffild, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg344891 - (view) Author: Roffild (Roffild) * Date: 2019-06-07 00:03
Everyone uses GIL wrong! = DEADLOCK

I used sub-interpreters in embedded Python:

PyGILState_Ensure() = DEADLOCK

A deadlock happens in the line:

Of course in the help there is the note:
Note that the PyGILState_() functions assume there is only one global interpreter (created automatically by Py_Initialize()). Python supports the creation of additional interpreters (using Py_NewInterpreter()), but mixing multiple interpreters and the PyGILState_() API is unsupported.

But functions PyGILState_() are used in third-party libraries. Most often, these functions are used without checking that GIL is already locked. Often, these functions are added to the code for reinsurance only and this can affect performance.



Pybind11 developers have already fixed this problem:

It is necessary to change the code of PyGILState_() functions to support sub-interpreters.

Or add to warning:
Some Python libraries cannot be used in a sub-interpreter due to the likelihood of deadlock.

For me, this is a critical vulnerability!

There is another problem:
Calling PyEval_AcquireThread() again results in a deadlock. This can be controlled in your code, but not in a third-party library.
msg344974 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2019-06-07 17:29
The situation with subinterpreters and the gilstate API is a known problem (see issues #10915 and #15751).  We plan on fixing that in the near future for 3.9 (and probably for 3.8).  Feel free to chime in on those earlier issues.  Note that the subinterpreter experience is going to be much improved with the 3.9 release.

You also mentioned an issue with PyEval_AcquireThread().  If you don't think this is directly related to the gilstate API (I suspect it is) then please open a new issue for that.
Date User Action Args
2022-04-11 14:59:16adminsetgithub: 81367
2019-06-07 17:29:01eric.snowsetstatus: open -> closed
superseder: [subinterpreters] Make the PyGILState API compatible with multiple interpreters
messages: + msg344974

resolution: duplicate
stage: resolved
2019-06-07 00:37:16vstinnersetnosy: + vstinner, eric.snow
2019-06-07 00:03:04Roffildcreate