-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Refer to the GIL as a thread state in the C API #127989
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
Labels
Comments
encukou
pushed a commit
that referenced
this issue
Mar 20, 2025
GH-127990) Co-authored-by: Victor Stinner <vstinner@python.org>
Merged to main. |
Me neither :) |
seehwan
pushed a commit
to seehwan/cpython
that referenced
this issue
Apr 16, 2025
…the GIL (pythonGH-127990) Co-authored-by: Victor Stinner <vstinner@python.org>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Documentation
"The GIL" is used all over the place in the C API documentation, but it's not really correct, and is a bit misleading for users. The biggest issue is that for free-threading, there is no GIL, so users erroneously call the C API inside
Py_BEGIN_ALLOW_THREADS
blocks or omitPyGILState_Ensure
in fresh threads. Similarly, PEP-684 let's us have multiple GILs. Which one is "the" GIL?Really, what "holding the GIL" should actually mean these days, is having an attached thread state (which also means holding the GIL for an interpreter on default builds). Thread states are documented, but not well. As far as I can tell, users know very little about them, other than that they hold some thread-specific information. That isn't great, because basically the only way to use subinterpreters right now is with thread state shenanigans, and the free-threading problem I mentioned above is also an issue.
Documenting them better, and replacing phrases like "the caller must hold the GIL" with "the caller must have an attached thread state" should help users understand both subinterpreters and nogil better. This will be a decently big change to the documentation, though. Right now, we're sort of all over the place in what a thread state means. From PyInterpreterState_Get
Most of this is redundant. If there is no thread state, that implies that there isn't an interpreter, and therefore can't hold a GIL. Redundant differences like this make it seem like there is a meaningful difference (for users) between an attached thread state and holding the GIL, which isn't good.
I have a PR ready (which I'll submit as a draft), but I'm putting this up for discussion first, because I'm not too sure how people will feel about it.
Linked PRs
The text was updated successfully, but these errors were encountered: