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>
G
Gregory P. Smith committed
64afa947f454b0295dd08de1029da8c1d882a99f
Parent: 6e2272d
Committed by GitHub <noreply@github.com>
on 4/11/2026, 9:54:23 PM