Skip to content

fixes #5900 -- address race condition with initialization and site.py loading#5903

Merged
alex merged 3 commits into
PyO3:mainfrom
alex:init-race-site
Mar 22, 2026
Merged

fixes #5900 -- address race condition with initialization and site.py loading#5903
alex merged 3 commits into
PyO3:mainfrom
alex:init-race-site

Conversation

@alex
Copy link
Copy Markdown
Member

@alex alex commented Mar 22, 2026

No description provided.

@alex
Copy link
Copy Markdown
Member Author

alex commented Mar 22, 2026

Grrr, wait_force requires an MSRV of 1.86... potential choices:

  1. Use call() with a no-op closure. Should always be ok with how we use this, but becomes a bit of a footgun.
  2. Raise MSRV
  3. Some third thing?

@alex alex force-pushed the init-race-site branch 2 times, most recently from fc545a6 to d90ce9f Compare March 22, 2026 14:00
Copy link
Copy Markdown
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Thanks for fixing! It would be nice if Py_Initialize were thread-safe, but I think in practice that's unlikely to happen because in real applications there's more obviously a "main thread" which becomes the Python main thread, this auto-initialize behaviour is most commonly seen in tests.

I think I'd prefer to avoid bumping MSRV and go down the call() route. I suspect that we could keep the complexity almost entirely inside interpreter_lifecycle.rs enough that the footgun is less of an issue. We could also document that we should migrate to wait when MSRV is next bumped.

Before rolling back - what is the current MSRV of cryptography? We still have the constraint that Debian trixie is Rust 1.85, so unless we believe that bumping past that is a non-issue, I think we will need to keep MSRV below 1.85 for some time yet. 🤔

@alex
Copy link
Copy Markdown
Member Author

alex commented Mar 22, 2026

Currently cryptography is at 1.83. Looked like about ~3.5% of our users where >=1.83 but <1.86.

Comment thread src/internal/state.rs
Comment on lines +102 to +103
// Py_IsInitialized() can return 1 while Py_InitializeEx is still
// running (e.g. importing site.py). Block until any in-progress PyO3
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is accurate as implemented in CPython today. (even if we fixed this, older interpreters would still have the behavior)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for taking a look!

@alex
Copy link
Copy Markdown
Member Author

alex commented Mar 22, 2026

(I switched over to the call version so we don't need to raise MSRV + an assert so if someone mis-used the function it'd trigger.)

Copy link
Copy Markdown
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Thanks!

Comment thread src/interpreter_lifecycle.rs
Co-authored-by: David Hewitt <mail@davidhewitt.dev>
@alex alex enabled auto-merge March 22, 2026 21:36
@alex alex added this pull request to the merge queue Mar 22, 2026
Merged via the queue into PyO3:main with commit fea8332 Mar 22, 2026
45 checks passed
@alex alex deleted the init-race-site branch March 22, 2026 23:24
davidhewitt added a commit that referenced this pull request Mar 31, 2026
… loading (#5903)

* fixes #5900 -- address race condition with initialization and site.py loading

* Use call_once since wait requires a higher MSRV

* Update src/interpreter_lifecycle.rs

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

---------

Co-authored-by: David Hewitt <mail@davidhewitt.dev>
davidhewitt added a commit that referenced this pull request Apr 2, 2026
… loading (#5903)

* fixes #5900 -- address race condition with initialization and site.py loading

* Use call_once since wait requires a higher MSRV

* Update src/interpreter_lifecycle.rs

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

---------

Co-authored-by: David Hewitt <mail@davidhewitt.dev>
@gpshead
Copy link
Copy Markdown

gpshead commented Apr 12, 2026

I merged my cpython PR to fix the Py_Initialize() internals, it should be in the next 3.15 pre-release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants