GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-116322: Add Py_mod_gil module slot by swtaarrs · Pull Request #116882 · python/cpython

Conversation

@swtaarrs

@swtaarrs swtaarrs commented Mar 15, 2024 •
edited by github-actions Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

This PR adds the ability to enable the GIL if it was disabled at interpreter startup, and modifies the multi-phase module initialization path to enable the GIL when loading a module, unless that module's spec includes a slot indicating it can run safely without the GIL.

PEP 703 called the constant for the slot Py_mod_gil_not_used; I went with Py_MOD_GIL_NOT_USED for consistency with gh-104148.

A warning will be issued up to once per interpreter for the first GIL-using module that is loaded. If -v is given, a shorter message will be printed to stderr every time a GIL-using module is loaded (including the first one that issues a warning).

  • Issue: Allow C extensions to declare compatibility with free-threading #116322

📚 Documentation preview 📚: https://cpython-previews--116882.org.readthedocs.build/

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@swtaarrs
Add Py_mod_gil module slot
fa92cb4
@swtaarrs swtaarrs added the topic-free-threading label Mar 15, 2024
@bedevere-app bedevere-app Bot mentioned this pull request Mar 15, 2024
Allow C extensions to declare compatibility with free-threading #116322
Closed
@swtaarrs

swtaarrs commented Mar 15, 2024

Copy link
Copy Markdown
Member Author

The code here is ready, but I'll keep it as a draft until gh-116749 and gh-116329 are done, since this doesn't do anything when the GIL is enabled by default. I changed the default locally to test this as it works as intended.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@swtaarrs
Merge remote-tracking branch 'upstream/main' into cpython-mod-gil
7edf796
@swtaarrs

swtaarrs commented Mar 22, 2024

Copy link
Copy Markdown
Member Author

I think it does make sense to land this now after all. The code won't function as intended until the GIL is disabled by default, but it will be nice to have the module slot available while I work on gh-116738, so I can tag modules as they're ready.

This is structured such that there shouldn't be any behavioral differences until the GIL is disabled by default, except when a --disable-gil build is run with -v, when it will print a single line for every non-free-threading-safe module that's loaded.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@swtaarrs
swtaarrs marked this pull request as ready for review March 22, 2024 21:54
@swtaarrs
swtaarrs requested a review from ericsnowcurrently as a code owner March 22, 2024 21:54
@bedevere-app bedevere-app Bot added the awaiting review label Mar 22, 2024
@swtaarrs
swtaarrs requested a review from colesbury March 22, 2024 21:54
@ericsnowcurrently

ericsnowcurrently commented Mar 23, 2024

Copy link
Copy Markdown
Member

I'll take a look on Monday.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ericsnowcurrently
ericsnowcurrently requested changes Mar 25, 2024
View reviewed changes

@ericsnowcurrently ericsnowcurrently left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mostly LGTM

I've left a few comments and I have some feedback on the relationship between Py_mod_multiple_interpreters and Py_mod_gil.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/c-api/module.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/c-api/module.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/c-api/module.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Objects/moduleobject.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Objects/moduleobject.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Objects/moduleobject.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Objects/moduleobject.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting review label Mar 25, 2024
@bedevere-app

bedevere-app Bot commented Mar 25, 2024

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added the awaiting changes label Mar 25, 2024
swtaarrs added 2 commits April 3, 2024 11:53
@swtaarrs
Some review comments from Eric
450aa41
@swtaarrs
Merge remote-tracking branch 'upstream/main' into cpython-mod-gil
6bbd281
@swtaarrs

swtaarrs commented Apr 3, 2024

Copy link
Copy Markdown
Member Author

@ericsnowcurrently I don't think there should be any code changes as a result of our big sub-thread. Please let me know if that's wrong!

Before this is ready for another review, I need to make some other changes. It looks like the consensus in Discord is that we should enable the GIL when calling a module init function, because we don't know before the call if it's a single-phase init module that needs the GIL. Then if it's a multi-phase init module that provides Py_GIL_NOT_USED, we can disable the GIL and continue, otherwise we leave the GIL on for the rest of the interpreter's lifetime. I'll make an issue soon to nail down how we handle single-phase init modules.

The only nontrivial (but not too complicated) part of this will be adding a function to disable the GIL safely. It should also move the GIL-related logic up out of PyModule_FromDefAndSpec2(), which will be a nice side-effect.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@swtaarrs swtaarrs mentioned this pull request Apr 3, 2024
Add a mechanism for single-phase init modules to declare free-threading compatibility #117526
Closed
swtaarrs
swtaarrs commented Apr 4, 2024
View reviewed changes
Comment thread Python/ceval_gil.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ericsnowcurrently

ericsnowcurrently commented Apr 5, 2024

Copy link
Copy Markdown
Member

I don't think there should be any code changes as a result of our big sub-thread.

Yeah, I think you're right.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

swtaarrs added 2 commits April 15, 2024 15:05
@swtaarrs
Fix enabling the GIL, also support disabling the GIL
47b9e26
More details:
- Fix a race while enabling the GIL by checking if the GIL was enabled between
  a no-op call to `_PyEval_AcquireLock()` and the thread attaching, and trying
  again if it was.
- Enable the GIL before running a module init function, since we can't know if
  it's a single-phase init module that doesn't support free-threading. Look at
  the state of the module after initialization to determine if it's safe to
  disable the GIL again.
- Add `PyModule_SetGIL()`, which can be used by single-phase init modules to
  declare that they support running without the GIL.
- Change `gil->enabled` from a simple on/off switch to a count of active
  requests to enable the GIL. This allows us to support multiple interleaved
  imports that each independently track whether the GIL should remain
  enabled. See the big comment in `pycore_ceval.h` for more details.
@swtaarrs
Merge remote-tracking branch 'upstream/main' into cpython-mod-gil
f7c3e50
@swtaarrs
swtaarrs requested review from brettcannon, kumaraditya303, ncoghlan and warsaw as code owners April 15, 2024 22:11
@swtaarrs

swtaarrs commented Apr 15, 2024

Copy link
Copy Markdown
Member Author

I have made the requested changes; please review again.

This should be ready for review again. It grew in scope a bit, and now includes support for single-phase init modules (gh-117526) (some details are in the commit message). With the infrastructure needed to properly support multi-phase init modules, supporting single-phase init modules is just a few extra lines, but I can pull it out into a separate PR if anyone wants.

One specific thing I'd like input on is the timing of the _PyImport_CheckGILForModule() call in PyModule_FromDefAndSpec2(): I put it before module creation to disable the GIL as soon as possible for modules that don't need the GIL. The downside of this is that if the module does need the GIL and a subsequent part of the import process fails, the GIL will remain enabled even though we didn't actually load the module. Maybe this should be rare enough that it's not worth worrying about.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting change review and removed awaiting changes labels Apr 15, 2024
@AlexWaygood
AlexWaygood removed request for AlexWaygood, FFY00, erlend-aasland, kumaraditya303 and pablogsal May 1, 2024 23:08
swtaarrs added 3 commits May 1, 2024 16:10
@swtaarrs
Fix 'gil_slot set but not used' warning
99bc5cb
@swtaarrs
Patch generator for Python-ast.c instead of just the file itself
6882c13
@swtaarrs
Update limited API version for _scproxy.c
da63df1
@swtaarrs
swtaarrs requested a review from isidentical as a code owner May 1, 2024 23:17
swtaarrs added 2 commits May 1, 2024 16:22
@swtaarrs
Fix _testconsole.c for Windows build
2998def
@swtaarrs
Fix winsound.c for Windows build
77d1652
@colesbury

colesbury commented May 2, 2024

Copy link
Copy Markdown
Contributor

!buildbot nogil

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented May 2, 2024

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @colesbury for commit 77d1652 🤖

The command will test the builders whose names match following regular expression: nogil

The builders matched are:

  • x86-64 MacOS Intel ASAN NoGIL PR
  • AMD64 Ubuntu NoGIL Refleaks PR
  • ARM64 MacOS M1 NoGIL PR
  • AMD64 Windows Server 2022 NoGIL PR
  • AMD64 Ubuntu NoGIL PR
  • ARM64 MacOS M1 Refleaks NoGIL PR
  • x86-64 MacOS Intel NoGIL PR
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

colesbury
colesbury approved these changes May 2, 2024
View reviewed changes
@swtaarrs

swtaarrs commented May 3, 2024

Copy link
Copy Markdown
Member Author

I found a few more modules that I missed during some testing, and will push a commit to add those shortly.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@swtaarrs
Mark more modules as not using the GIL
d1fe0cc
@swtaarrs

swtaarrs commented May 3, 2024

Copy link
Copy Markdown
Member Author

!buildbot nogil

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented May 3, 2024

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @swtaarrs for commit d1fe0cc 🤖

The command will test the builders whose names match following regular expression: nogil

The builders matched are:

  • x86-64 MacOS Intel ASAN NoGIL PR
  • AMD64 Ubuntu NoGIL Refleaks PR
  • ARM64 MacOS M1 NoGIL PR
  • AMD64 Windows Server 2022 NoGIL PR
  • AMD64 Ubuntu NoGIL PR
  • ARM64 MacOS M1 Refleaks NoGIL PR
  • x86-64 MacOS Intel NoGIL PR
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@colesbury
colesbury merged commit c2627d6 into python:main May 3, 2024
@bedevere-app bedevere-app Bot removed the awaiting merge label May 3, 2024
@swtaarrs swtaarrs mentioned this pull request May 3, 2024
gh-116322: Enable the GIL while loading C extension modules #118560
Merged
Yhg1s
Yhg1s reviewed May 5, 2024
View reviewed changes
Comment thread Doc/c-api/module.rst

.. versionadded:: 3.9

.. c:function:: int PyModule_ExperimentalSetGIL(PyObject *module, void *gil)

@Yhg1s Yhg1s May 5, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ehm, can this please be renamed to PyUnstable as suggested before? We have such naming schemes for a reason.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

encukou reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@swtaarrs swtaarrs May 6, 2024

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'll get a PR ready. I went with this name because having both Unstable and Experimental in the name felt redundant, and in this comment, @ericsnowcurrently expressed a preference for PyModule_ExperimentalSetGIL() over PyUnstable_Module_SetGIL().

Does anyone else have strong feelings on whether it should be PyUnstable_Module_ExperimentalSetGIL() or PyUnstable_Module_SetGIL()?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@encukou encukou May 6, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR here: #118645

IMO, the “experimental” is redundant. If we need “Experimental” too, we should rethink the entire PEP-689 naming convention.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

JelleZijlstra and swtaarrs reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
SonicField pushed a commit to SonicField/cpython that referenced this pull request May 8, 2024
@swtaarrs @SonicField
pythongh-116322: Add Py_mod_gil module slot (python#116882)
f301715
This PR adds the ability to enable the GIL if it was disabled at
interpreter startup, and modifies the multi-phase module initialization
path to enable the GIL when loading a module, unless that module's spec
includes a slot indicating it can run safely without the GIL.

PEP 703 called the constant for the slot `Py_mod_gil_not_used`; I went
with `Py_MOD_GIL_NOT_USED` for consistency with pythongh-104148.

A warning will be issued up to once per interpreter for the first
GIL-using module that is loaded. If `-v` is given, a shorter message
will be printed to stderr every time a GIL-using module is loaded
(including the first one that issues a warning).
@mikeshardmind mikeshardmind mentioned this pull request May 11, 2024
I think this is all that's needed AbstractUmbra/audioop#7
Merged
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Jul 25, 2024
@serhiy-storchaka
pythongh-116882: Fix typo in the #ifdef check
30a9ee1
serhiy-storchaka
serhiy-storchaka reviewed Jul 25, 2024
View reviewed changes
Comment thread Objects/moduleobject.c
}
}
m->md_def = module;
#ifdef Py_GIL_DISABLE

@serhiy-storchaka serhiy-storchaka Jul 25, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be Py_GIL_DISABLED.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

erlend-aasland and colesbury reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
colesbury added a commit to colesbury/cpython that referenced this pull request Jul 25, 2024
@colesbury
pythongh-116882: Fix typo in moduleobject.c
ae70061
stefanholek added a commit to stefanholek/rl that referenced this pull request Oct 14, 2024
@stefanholek
Mark readline module in no-gil builds.
65457c6
This means: "Do not re-enable the GIL when importing rl.readline."

See python/cpython#116322
See python/cpython#116882
@cpcloud cpcloud mentioned this pull request Oct 8, 2025
build: enable free-threading compatibility NVIDIA/cuda-python#1103
Merged
@encukou encukou mentioned this pull request Dec 16, 2025
gh-140550: Update xxlimited with 3.15 limited API & PEP 697 #142827
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ambv ambv ambv left review comments
@encukou encukou encukou left review comments
@ericsnowcurrently ericsnowcurrently ericsnowcurrently approved these changes
@serhiy-storchaka serhiy-storchaka serhiy-storchaka left review comments
@Yhg1s Yhg1s Yhg1s left review comments
@colesbury colesbury colesbury approved these changes
@brettcannon brettcannon Awaiting requested review from brettcannon brettcannon is a code owner
@ncoghlan ncoghlan Awaiting requested review from ncoghlan ncoghlan is a code owner
@warsaw warsaw Awaiting requested review from warsaw warsaw is a code owner
@JelleZijlstra JelleZijlstra Awaiting requested review from JelleZijlstra JelleZijlstra is a code owner
@rhettinger rhettinger Awaiting requested review from rhettinger rhettinger is a code owner
@gpshead gpshead Awaiting requested review from gpshead gpshead is a code owner
@berkerpeksag berkerpeksag Awaiting requested review from berkerpeksag
@pganssle pganssle Awaiting requested review from pganssle pganssle is a code owner
@abalkin abalkin Awaiting requested review from abalkin
@tiran tiran Awaiting requested review from tiran
@1st1 1st1 Awaiting requested review from 1st1 1st1 is a code owner
@asvetlov asvetlov Awaiting requested review from asvetlov asvetlov is a code owner
@gvanrossum gvanrossum Awaiting requested review from gvanrossum
@willingc willingc Awaiting requested review from willingc willingc is a code owner
@isidentical isidentical Awaiting requested review from isidentical

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

topic-free-threading

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

8 participants

@swtaarrs @ericsnowcurrently @colesbury @bedevere-bot @ambv @encukou @serhiy-storchaka @Yhg1s
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.