Skip to content
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

PYTHON_GIL=1 and -Xgil=1 should work in non-free-threading builds #123275

Closed
godlygeek opened this issue Aug 23, 2024 · 3 comments
Closed

PYTHON_GIL=1 and -Xgil=1 should work in non-free-threading builds #123275

godlygeek opened this issue Aug 23, 2024 · 3 comments
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes topic-free-threading type-bug An unexpected behavior, bug, or error

Comments

@godlygeek
Copy link
Contributor

godlygeek commented Aug 23, 2024

Bug report

Bug description:

In a non-free-threading build of Python 3.13, using environment variables or CLI arguments to ensure the GIL is used results in an error:

$ PYTHON_GIL=1 python3.13
Fatal Python error: config_read_gil: PYTHON_GIL / -X gil are not supported by this build
Python runtime state: preinitialized

$ python3.13 -Xgil=1
Fatal Python error: config_read_gil: PYTHON_GIL / -X gil are not supported by this build
Python runtime state: preinitialized

This is user-hostile behavior that makes it more difficult to write tests and set up test environments. It makes sense that PYTHON_GIL=0 or -Xgil=0 must fail for non-free-threading builds, but there's no reason why a user requesting that the GIL be enabled in a build where the GIL is always enabled should be an error rather than a no-op.

I bumped into this while working on the test suite for PyStack, where it has forced me into this nasty hack:

@pytest.fixture(autouse=True)
def enable_gil_if_free_threading(python, monkeypatch):
    _, python_executable = python
    proc = subprocess.run([python_executable, "-Xgil=1", "-cpass"], capture_output=True)
    free_threading = proc.returncode == 0
    if free_threading:
        monkeypatch.setenv("PYTHON_GIL", "1")

instead of what I had originally wanted to do:

@pytest.fixture(autouse=True)
def enable_gil_if_free_threading(monkeypatch):
    monkeypatch.setenv("PYTHON_GIL", "1")

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

@corona10
Copy link
Member

corona10 commented Sep 6, 2024

@ZeroIntensity PTAL #123276 (comment)

corona10 pushed a commit that referenced this issue Sep 6, 2024
…readed builds (gh-123276) (gh-123753)

gh-123275: Support `-Xgil=1` and `PYTHON_GIL=1` on non-free-threaded builds (gh-123276)
(cherry picked from commit 84ad264)

Co-authored-by: Peter Bierma <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 6, 2024
…-123754)

(cherry picked from commit fe24b71)

Co-authored-by: Peter Bierma <[email protected]>
corona10 pushed a commit that referenced this issue Sep 6, 2024
…) (gh-123755)

gh-123275: Add tests for `PYTHON_GIL=1` and `-Xgil=1` (gh-123754)
(cherry picked from commit fe24b71)

Co-authored-by: Peter Bierma <[email protected]>
@corona10
Copy link
Member

corona10 commented Sep 6, 2024

@ZeroIntensity Thanks for the work!

@corona10 corona10 closed this as completed Sep 6, 2024
@corona10
Copy link
Member

corona10 commented Sep 6, 2024

@tonybaloney
IIRC, you had an interest in this feature at this year's language summit :)
Now the default build can use PYTHON_GIL=1 and -Xgil=1!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes topic-free-threading type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants