Directly check if/which interpreter is active before doing CLEAR in destructor#5965
Merged
b-pass merged 4 commits intopybind:masterfrom Jan 20, 2026
Merged
Conversation
…estructors. Py_IsFinalizing only applies to the main interpreter.
rwgk
added a commit
to rwgk/pybind11
that referenced
this pull request
Jan 19, 2026
Fold b-pass PR pybind#5965's per-interpreter ownership checks into internals and local_internals while keeping leak_detach for post-teardown destroy() paths. Add a non-null guard so a nullptr return from get_interpreter_state_unchecked() late in shutdown can't match nullptr istate and trigger Py_CLEAR without an active interpreter (avoid UB). Keep helper placement aligned with PR pybind#5965.
Collaborator
|
@b-pass, I worked on a "best of both" version under PR #5966 (commit 8756fb2 there) You can get the suggested diff like this: Copy-pasting for easy reference: diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h
index 2811077e..2cd3ea4a 100644
--- a/include/pybind11/detail/internals.h
+++ b/include/pybind11/detail/internals.h
@@ -337,13 +337,24 @@ struct internals {
internals(internals &&other) = delete;
internals &operator=(const internals &other) = delete;
internals &operator=(internals &&other) = delete;
+
+ void leak_detach() noexcept {
+ // Used when internals must be destroyed after interpreter teardown.
+ // Avoid touching the Python C-API by clearing pointers only.
+ instance_base = nullptr;
+ default_metaclass = nullptr;
+ static_property_type = nullptr;
+ istate = nullptr;
+ }
+
~internals() {
// Normally this destructor runs during interpreter finalization and it may DECREF things.
// In odd finalization scenarios it might end up running after the interpreter has
// completely shut down, In that case, we should not decref these objects because pymalloc
// is gone. This also applies across sub-interpreters, we should only DECREF when the
// original owning interpreter is active.
- if (get_interpreter_state_unchecked() == istate) {
+ auto *cur_istate = get_interpreter_state_unchecked();
+ if (cur_istate && cur_istate == istate) {
Py_CLEAR(instance_base);
Py_CLEAR(default_metaclass);
Py_CLEAR(static_property_type);
@@ -369,13 +380,20 @@ struct local_internals {
PyTypeObject *function_record_py_type = nullptr;
PyInterpreterState *istate = nullptr;
+ void leak_detach() noexcept {
+ // Used when local internals must be destroyed after interpreter teardown.
+ function_record_py_type = nullptr;
+ istate = nullptr;
+ }
+
~local_internals() {
// Normally this destructor runs during interpreter finalization and it may DECREF things.
// In odd finalization scenarios it might end up running after the interpreter has
// completely shut down, In that case, we should not decref these objects because pymalloc
// is gone. This also applies across sub-interpreters, we should only DECREF when the
// original owning interpreter is active.
- if (get_interpreter_state_unchecked() == istate) {
+ auto *cur_istate = get_interpreter_state_unchecked();
+ if (cur_istate && cur_istate == istate) {
Py_CLEAR(function_record_py_type);
}
}
@@ -711,13 +729,24 @@ public:
// this could be called without an active interpreter, just use what was cached
if (!tstate || tstate->interp == last_istate_tls()) {
auto tpp = internals_p_tls();
-
+ if (tpp && tpp->get()) {
+ auto *cur_istate = get_interpreter_state_unchecked();
+ if (!cur_istate || cur_istate != tpp->get()->istate) {
+ tpp->get()->leak_detach();
+ }
+ }
delete tpp;
}
unref();
return;
}
#endif
+ if (internals_singleton_pp_ && internals_singleton_pp_->get()) {
+ auto *cur_istate = get_interpreter_state_unchecked();
+ if (!cur_istate || cur_istate != internals_singleton_pp_->get()->istate) {
+ internals_singleton_pp_->get()->leak_detach();
+ }
+ }
delete internals_singleton_pp_;
unref();
} |
rwgk
approved these changes
Jan 20, 2026
Collaborator
rwgk
left a comment
There was a problem hiding this comment.
Looks good to me.
Do you want to leave the leak_detach() change for a follow-on PR? Or do you think they are not needed?
Collaborator
Author
It looks to me like |
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.
Description
Fix of the fix from #5958 ...
Py_IsFinalizingandPy_IsInitializedonly apply to the main interpreter.