fix: reset gil_safe_call_once_and_store cache on interpreter finalize - #6162
Open
henryiii wants to merge 2 commits into
Open
fix: reset gil_safe_call_once_and_store cache on interpreter finalize#6162henryiii wants to merge 2 commits into
henryiii wants to merge 2 commits into
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 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.