cpython/Doc/c-api
Gregory P. Smith 64afa947f4
gh-146302: make Py_IsInitialized() thread-safe and reflect true init completion (GH-146303)
## Summary

- Move the `runtime->initialized = 1` store from before `site.py` import to the end of `init_interp_main()`, so `Py_IsInitialized()` only returns true after initialization has fully completed
- Access `initialized` and `core_initialized` through new inline accessors using acquire/release atomics, to also protect from data race undefined behavior
- `PySys_AddAuditHook()` now uses the accessor, so with the flag move it correctly skips audit hook invocation during all init phases (matching the documented "after runtime initialization" behavior) ... We could argue that running these earlier would be good even if the intent was never explicitly expressed, but that'd be its own issue.

## Motivation

`Py_IsInitialized()` returned 1 while `Py_InitializeEx()` was still running — specifically, before `site.py` had been imported. See https://github.com/PyO3/pyo3/issues/5900 where a second thread could acquire the GIL and start executing Python with an incomplete `sys.path` because `site.py` hadn't finished.

The flag was also a plain `int` with no atomic operations, making concurrent reads a C-standard data race, though unlikely to manifest.

## Regression test:

The added test properly fails on `main` with `ERROR: Py_IsInitialized() was true during site import`.

---

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-11 21:54:23 +00:00
..
abstract.rst gh-85275: Remove old buffer APIs (#105137) 2023-06-02 01:12:40 +00:00
allocation.rst gh-141004: Document deprecated aliases for memory allocation (GH-141146) 2025-11-09 10:06:38 -05:00
apiabiversion.rst gh-143578: Restore note about patchlevel.h (#143596) 2026-01-12 11:21:14 +00:00
arg.rst Python 3.15.0a7 2026-03-10 14:31:15 +02:00
bool.rst gh-110481, doc: Add "immortal" term to the glossary (#112180) 2023-11-17 15:09:19 +01:00
buffer.rst gh-137696: Rename 'fort' parameter to 'order' in PyBuffer_* functions (#137866) 2026-03-19 16:54:27 +05:30
bytearray.rst gh-142518: add thread safety annotations for bytearray C-API (#146514) 2026-03-27 18:40:26 +05:30
bytes.rst gh-142518: add thread safety docs on bytes C-API (#146415) 2026-03-27 18:31:18 +05:30
call.rst gh-141004: Document old provisional names for vectorcall/code API (GH-143568) 2026-01-14 10:54:45 +01:00
capsule.rst gh-141004: Document PyCapsule_Type (GH-141079) 2025-11-07 09:09:38 -05:00
cell.rst Fix minor typos and wording in C API docs (#140955) 2025-11-03 22:04:46 +02:00
code.rst Docs: remove unneeded author attributions (#145002) 2026-02-19 18:45:28 -05:00
codec.rst gh-141004: Document Py_hexdigits (GH-141059) 2025-11-06 08:48:42 -05:00
complex.rst Fix minor typos and wording in C API docs (#140955) 2025-11-03 22:04:46 +02:00
concrete.rst gh-141004: Document the PyPickleBuffer_* C API (GH-141630) 2025-11-16 13:50:54 -05:00
contextvars.rst gh-124872: Replace enter/exit events with "switched" (#125532) 2024-10-16 13:53:21 +02:00
conversion.rst gh-141004: Mark up constants for PyOS_double_to_string (GH-143867) 2026-01-22 11:53:17 +01:00
coro.rst bpo-42528: Improve the docs of most Py*_Check{,Exact} API calls (GH-23602) 2021-01-06 12:38:26 +01:00
curses.rst gh-141004: document curses C API (#141254) 2025-11-09 15:44:26 +00:00
datetime.rst add whatsnew entry for PyUnstable_SetImmortal and PyDatetime_IMPORT (#144830) 2026-02-15 20:21:54 +05:30
descriptor.rst Docs: fill in descriptor C API docs (GH-146644) 2026-03-31 17:52:11 -04:00
dict.rst gh-142518: annotate dict C-APIs for thread safety (#145875) 2026-04-10 18:41:12 +00:00
exceptions.rst Python 3.15.0a7 2026-03-10 14:31:15 +02:00
extension-modules.rst Python 3.15.0a3 2025-12-16 14:26:12 +02:00
file.rst gh-146056: Fix list.__repr__() for lists containing NULLs (GH-146129) 2026-03-19 09:59:48 +02:00
float.rst gh-145633: Refine notes on non-IEEE platforms (#145845) 2026-03-19 11:42:25 +00:00
frame.rst gh-144693: Clarify that PyFrame_GetBack does not raise exceptions (GH-144824) 2026-02-27 10:25:46 -05:00
function.rst gh-141004: Document PyFunction_SetKwDefaults (GH-141294) 2025-11-09 12:49:17 -05:00
gcsupport.rst gh-145921: Add "_DuringGC" functions for tp_traverse (GH-145925) 2026-04-08 09:15:11 +02:00
gen.rst gh-141004: Document several soft-deprecated C APIs (GH-141634) 2026-01-14 08:20:33 -05:00
hash.rst gh-141004: correctly document Py_HASH_* and PyHASH_* as hash_info attributes (#141233) 2025-11-08 12:22:02 +00:00
import.rst gh-148110: Resolve lazy import filter names for relative imports (#148111) 2026-04-06 22:29:02 +01:00
index.rst gh-141811: Split up init.rst into multiple pages (GH-144844) 2026-02-20 16:37:39 +00:00
init.rst gh-141811: Split up init.rst into multiple pages (GH-144844) 2026-02-20 16:37:39 +00:00
init_config.rst gh-148014: Accept a function name in -X presite option (#148015) 2026-04-07 14:05:39 +00:00
interp-lifecycle.rst gh-146302: make Py_IsInitialized() thread-safe and reflect true init completion (GH-146303) 2026-04-11 21:54:23 +00:00
intro.rst Python 3.15.0a8 2026-04-07 14:24:03 +03:00
iter.rst Fix typo in PyIter_Send docs (#140336) 2025-10-20 14:23:57 +05:30
iterator.rst gh-141004: Document PyODict* APIs (GH-141136) 2025-11-17 14:40:47 -05:00
lifecycle.dot gh-75459: Doc: C API: Improve object life cycle documentation (GH-125962) 2025-05-20 14:25:50 +02:00
lifecycle.dot.css gh-75459: Doc: C API: Improve object life cycle documentation (GH-125962) 2025-05-20 14:25:50 +02:00
lifecycle.dot.pdf gh-75459: Doc: C API: Improve object life cycle documentation (GH-125962) 2025-05-20 14:25:50 +02:00
lifecycle.dot.svg gh-75459: Doc: C API: Improve object life cycle documentation (GH-125962) 2025-05-20 14:25:50 +02:00
lifecycle.rst gh-75459: versionadded for PyObject_CallFinalizer*() API (GH-143982) 2026-01-19 11:22:16 +01:00
list.rst gh-142518: Annotate PyList_* C APIs for thread safety (#146109) 2026-03-18 17:42:20 +01:00
long.rst gh-143869: Add PEP 757 functions to the limited API (#143906) 2026-01-21 14:47:14 +01:00
mapping.rst Fix minor typos and wording in C API docs (#140955) 2025-11-03 22:04:46 +02:00
marshal.rst Fix minor typos and wording in C API docs (#140955) 2025-11-03 22:04:46 +02:00
memory.rst gh-142518: Improve mimalloc allocator docs (#145224) 2026-03-11 15:14:47 +01:00
memoryview.rst gh-141004: Document PyMemoryView_Type (GH-141034) 2025-11-05 11:45:13 -05:00
method.rst gh-107091: Fix the use of some C domain roles (#107092) 2023-07-23 12:27:05 +02:00
module.rst gh-146636: Py_mod_abi mandatory for modules created from slots array (GH-146855) 2026-04-02 13:54:21 +02:00
monitoring.rst Fix minor typos and wording in C API docs (#140955) 2025-11-03 22:04:46 +02:00
none.rst gh-110481, doc: Add "immortal" term to the glossary (#112180) 2023-11-17 15:09:19 +01:00
number.rst gh-121905: Consistently use "floating-point" instead of "floating point" (GH-121907) 2024-07-19 08:06:02 +00:00
object.rst gh-146056: Fix list.__repr__() for lists containing NULLs (GH-146129) 2026-03-19 09:59:48 +02:00
objimpl.rst gh-75459: Doc: C API: Improve object life cycle documentation (GH-125962) 2025-05-20 14:25:50 +02:00
perfmaps.rst gh-136459: Add perf trampoline support for macOS (#136461) 2025-07-22 16:47:24 +01:00
picklebuffer.rst gh-141004: Document the PyPickleBuffer_* C API (GH-141630) 2025-11-16 13:50:54 -05:00
profiling.rst gh-141811: Split up init.rst into multiple pages (GH-144844) 2026-02-20 16:37:39 +00:00
refcounting.rst gh-144277: Fix usage of free-threaded terminology in the documentation (GH-144333) 2026-02-01 20:39:26 -05:00
reflection.rst gh-127989: C API: Refer to "attached thread states" instead of the GIL (GH-127990) 2025-03-20 13:06:59 +01:00
sequence.rst gh-127896: Add missing documentation of PySequence_In (GH-127979) 2024-12-16 14:56:04 +01:00
set.rst gh-142518: add thread safety docs for set C-APIs (#146562) 2026-04-10 19:33:24 +00:00
slice.rst gh-130214: Document PyEllipsis_Type (GH-130215) 2025-02-17 13:56:33 +01:00
stable.rst Python 3.15.0a8 2026-04-07 14:24:03 +03:00
structures.rst gh-125053: Document that ob_mutex must only be used via critical section API (#144599) 2026-03-10 17:23:39 +01:00
subinterpreters.rst gh-141811: Split up init.rst into multiple pages (GH-144844) 2026-02-20 16:37:39 +00:00
synchronization.rst gh-141811: Split up init.rst into multiple pages (GH-144844) 2026-02-20 16:37:39 +00:00
sys.rst gh-141004: Document PyOS_InterruptOccurred (GH-141526) 2025-11-14 06:20:36 -05:00
threads.rst Docs: Improve the C API documentation involving threads (GH-145520) 2026-03-10 10:57:13 -04:00
time.rst gh-127989: C API: Refer to "attached thread states" instead of the GIL (GH-127990) 2025-03-20 13:06:59 +01:00
tls.rst gh-141811: Split up init.rst into multiple pages (GH-144844) 2026-02-20 16:37:39 +00:00
tuple.rst gh-142518: add thread safety annotations for tuple C-API (#148012) 2026-04-08 01:55:44 +05:30
type.rst Document that PyType_GetModule returns a borrowed ref (GH-145612) 2026-03-09 14:32:28 +01:00
typehints.rst gh-107298: Fix Sphinx warnings in the C API doc (#107302) 2023-07-27 01:41:15 +02:00
typeobj.rst gh-145921: Add "_DuringGC" functions for tp_traverse (GH-145925) 2026-04-08 09:15:11 +02:00
unicode.rst gh-148250: Mention str subclasses in PyUnicodeWriter_WriteStr() doc (#148251) 2026-04-09 13:53:10 +02:00
utilities.rst gh-110850: Add PyTime_t C API (GH-115215) 2024-02-12 18:13:10 +01:00
veryhigh.rst Fix typos and grammar errors across documentation (#144709) 2026-02-11 16:35:25 +00:00
weakref.rst gh-141004: Document PyType_SUPPORTS_WEAKREFS (GH-141408) 2025-11-12 11:38:17 -05:00