Repository navigation
Conversation
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.
67b0007 to
695f1a1
Compare
|
— codex on behalf of Ralf — Avoid writes to Generated by Codex. Ralf has yet to review these findings. |
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
|
🤖 AI text below 🤖 Writes to
CI does not use ThreadSanitizer, so the new test only detects this race in a local sanitizer build. |
🤖 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 plus an "initialized" flag in the module.
Nothing cleared that cache when the interpreter was finalized. After
finalize_interpreter(); initialize_interpreter();the fast path gave freed memory back to thecaller.
py::register_exceptionuses this class, thus re-importing an embedded module whichregisters an exception used the freed exception object.
The per-interpreter storage now knows its owner and resets the cached pointer and the flag from its
destructor. The interpreter destroys the storage during finalization, thus the fast path becomes
invalid at the correct time and the next call stores the value again.
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.