Skip to content

Clarify the Purpose of the PyGILState API and Identify Better Alternatives #131267

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

Open
ericsnowcurrently opened this issue Mar 14, 2025 · 2 comments
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes docs Documentation in the Doc dir topic-C-API

Comments

@ericsnowcurrently
Copy link
Member

This especially applies to the following sections:

but also:

The PyGILState_* C-API was introduced by PEP 311 as a convenience for extension maintainers for some very specific situations:

  • code running in the current thread does not know if it holds the GIL
  • this situation may involve reentrancy

The PyGILState_* API also (almost incidentally) supports use cases that are more appropriately satisfied by other API, like Py_BEGIN_ALLOW_THREADS()/Py_END_ALLOW_THREADS() for balanced acquire-release.

The documentation isn't so clear about the specific use cases, nor that an interested user may actually want a different API. For example, from a recent issue I saw: We were wrapping each evaluation in PyGILState_Ensure and PyGILState_Release, which I believe is a correct way to do this.

FTR, https://docs.python.org/3/c-api/init.html#non-python-created-threads does a decent job of explaining, as does the PEP, but the entries for PyGILState_Ensure() and PyGILState_Release() are less clear.

FWIW, I expect that it is easier now to deal with the motivating cases using the exiting C-API than it was in 2003. I'm not sure we would accept something like PEP 311 if it were proposed today. Then again, for a pair of operations that needs to balanced like this, having a specific pair of functions is helpful.


Related: a recent discussion about replacing the PyGILState_* API: https://discuss.python.org/t/a-new-api-for-ensuring-releasing-thread-states/83959.

Also see gh-131264 and gh-131265.

@ericsnowcurrently ericsnowcurrently added 3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes docs Documentation in the Doc dir topic-C-API labels Mar 14, 2025
@sharktide
Copy link
Contributor

sharktide commented Mar 15, 2025

@ericsnowcurrently I can make a pull request if you would like

@ZeroIntensity
Copy link
Member

PTAL at #131087, which implements the "alternatives" part here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes docs Documentation in the Doc dir topic-C-API
Projects
Status: Todo
Development

No branches or pull requests

3 participants