Skip to content

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

@gpshead

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

Metadata

Metadata

Assignees

Labels

interpreter-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