Skip to content

Py_IsInitialized() can return 1 before Py_InitializeEx() has completed. #146302

Closed

Description

@gpshead

Bug report

Bug description:

The race condition is more easily noticed if site.py takes a while to execute as the internal "initialized" flag is set to 1 before the site import.

Example impact? PyO3 has to jump through some hoops to avoid problems here in embedding scenarios where multiple threads all want to use an interpreter (most often test suites, resulting in some flaky tests). PyO3/pyo3#5903

We document Py_IsInitialized() as safe to call at any time and as being accurate. It is technically safe, but it isn't currently accurate.

CPython versions tested on:

3.13, 3.14, 3.15-alpha

Linked PRs

Activity

  1. added 2 commits that reference this issue on Mar 22, 2026
  2. self-assigned this
    on Mar 22, 2026
  3. added 2 commits that reference this issue on Apr 11, 2026
  4. gpshead commented on Apr 12, 2026

    @gpshead
    MemberAuthor

    not backporting based on this conversation: #146303 (comment)

  5. added
    3.13only security fixes
    3.14bugs and security fixes
    3.15bugs and security fixes
    on Apr 12, 2026
  6. added a commit that references this issue on Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.13only security fixes3.14bugs and security fixes3.15bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions