Repository navigation
Conversation
67b0007 to
695f1a1
Compare
|
— codex on behalf of Ralf — Avoid writes to Generated by Codex. Ralf has yet to review these findings. |
|
🤖 AI text below 🤖 Writes to
CI does not use ThreadSanitizer, so the new test only detects this race in a local sanitizer build. |
2563686 to
05aad66
Compare
|
codex GPT-6.2-Sol ultra (I'll add a commit to fix the finding): I’d hold #6162 for one more fix. The earlier The mental model is that each interpreter owns its stored value, such as the Python exception class created by The remaining bug is that teardown can erase another interpreter’s cache entry. A subinterpreter checks that the cached pointer belongs to it. Before it finishes clearing the cache, the main interpreter can publish its own pointer and mark the cache valid. The subinterpreter then clears the pointer, leaving a null pointer marked valid. After a restart, the helper skips initialization. I reproduced this with test-only timing hooks on both GIL-enabled and free-threaded CPython. The usual chained I’d use one atomic pointer for the cache, with null meaning empty, and clear it using compare-and-exchange so teardown can only remove its own entry. A scratch version passed the same forced ordering on both builds. Separately, AddressSanitizer confirmed that the new back-pointer can access freed memory if the helper itself is destroyed before the interpreter. If static lifetime is part of the API contract, I’d state that explicitly where callers see it. GitHub CI passed. The local Catch2 suite passed all 35 cases on both CPython 3.15.2 builds, and the three new cases also passed with the GIL forced off. Local compilation needed a CPython-header warning workaround, and the free-threaded suite passed after extending the harness’s three-second timeout. CI |
|
🤖 AI text below 🤖 A review of this branch found five items. Commit 6d9f055 addresses them.
The Catch tests pass locally on Python 3.11 (no subinterpreter support) and on 3.15t. The PR description still says the bug applies "when subinterpreter support is enabled". That is now too narrow, and the changelog entry should mention the requirement from item 5. |
|
— codex on behalf of Ralf — The new commit fixes ordinary restarts on builds without subinterpreter support. I verified both the initializer and exception-registration fixes on CPython 3.9, 3.11, and free-threaded 3.15 with subinterpreter support disabled. The new A late cleanup callback can still defeat the restart fix. After one helper’s capsule is destroyed, a later finalizer can call The new reset introduces two regressions on 3.11. A plain I’d share the interpreter-owned storage path across CPython builds and prevent teardown callbacks from republishing the fast cache. I’d handle PyPy separately. Its state-dict fallback uses the calling frame’s builtins, and I reproduced a new All 36 local Catch2 cases passed on both CPython 3.15.2 builds. The four targeted cases also passed with the GIL forced off. See attached 6162-local-validation.zip |
|
@henryiii I'm a bit surprised that we're getting into more agent-back-and-forth here. The combinations of Python versions and subinterpreter support on/off appear to generate multiple tricky edge cases. I left the codex findings and evidence in the previous comment, but I'm holding off adding more commits until I get your feedback. |
The old reference was released into the new interpreter's allocator, which crashed on Python 3.12. This was also the cause of the enum test skip.
Threads of one interpreter can run the slow path of get_stored() at the same time when a subinterpreter has existed. The write to the non-atomic owner was a data race under free-threading. The owner is already set in the std::call_once initializer. Assisted-by: ClaudeCode:claude-fable-5-2
Use the cached pointer as the validity marker. Compare and clear the pointer in one operation so a subinterpreter cannot erase another interpreter's cache publication during teardown.
Without subinterpreter support, the stored result is process-global and was not reset when the interpreter was finalized. A restarted interpreter then used an object that the old interpreter owned. Also: - get_stored() fails if the interpreter has no stored value. - Add the race comment for the load order in get_cached_storage(). - Document that the fast-path cache is not published again. - Document that the helper must stay loaded until finalization. Assisted-by: ClaudeCode:claude-fable-5-2
Remove the reset on finalize for the process-global variant, and put the restart tests back inside PYBIND11_HAS_SUBINTERPRETER_SUPPORT. The other changes of 6d9f055 stay. Assisted-by: ClaudeCode:claude-fable-5-2
A callback can call the helper during finalization, after the interpreter destroyed the stored value. The interpreter does not destroy the storage which that call creates, so its pointer stayed in the fast-path cache and the next interpreter used the old value. Publish the cache only from the main interpreter, and only when Python is not finalizing. Assisted-by: ClaudeCode:claude-fable-5-2
Store a pointer to the cache atomic in the storage instead of the helper, removing the forward declaration, friend, and invalidate_cache(). Assisted-by: ClaudeCode:claude-opus-5-5
24a65e5 to
c178690
Compare
|
I ran this on high effort level instead of medium, and fixing the discovered sidetrack was too tempting. :) I've switched back to medium, reverted that part, now this is sub interpreter only again. I've also applied a fix for half of point 1, which wasn't dependent on the main interpreter restart change. I ran an Opus simplification run too, as it's quite good at writing clean comments. |
Assisted-by: ClaudeCode:claude-opus-5-5
🤖 AI text below 🤖
Description
When subinterpreter support is enabled,
gil_safe_call_once_and_storekeeps the stored value in thestate dict of the interpreter, and caches a pointer to it in the helper. Nothing cleared that cache
when the interpreter was finalized. After
finalize_interpreter(); initialize_interpreter();thefast path gave freed memory back to the caller.
py::register_exceptionuses this class, thusre-importing an embedded module which registers an exception used the freed exception object.
The per-interpreter storage now knows its owner and clears the cached pointer from its destructor,
with a compare-and-swap so that it only removes its own pointer. The interpreter destroys the
storage during finalization, thus the helper must stay loaded until then.
Also,
get_stored()now fails with an error if the current interpreter has no stored value.Before, it returned a reference to memory with no constructed object.
This PR does not change builds without subinterpreter support (Python 3.9 to 3.11, or
PYBIND11_HAS_SUBINTERPRETER_SUPPORT=0). There the value is process-global and a restart stilluses the old value.
New Catch2 tests in
tests/test_with_catch/test_interpreter.cpprestart the interpreter and thenuse the stored value. Before the fix, the callable was not called again for the new interpreter, and
the re-imported exception module had no exception attribute.
Suggested changelog entry:
py::gil_safe_call_once_and_store(and therefore inpy::register_exception) when an embedded interpreter is finalized and then initialized again,in builds with subinterpreter support. The helper must stay loaded until the interpreter is
finalized.
📚 Documentation preview 📚: https://pybind11--6162.org.readthedocs.build/