From 15943963b3c3294cfe8073a9b589676f3117be02 Mon Sep 17 00:00:00 2001 From: daltairwalter <31971208+daltairwalter@users.noreply.github.com> Date: Thu, 16 Oct 2025 10:40:50 -0500 Subject: [PATCH] fix dangling thread state issue (#5870) * fix dangling thread state issue * formatting rules * use tstate .set(nullptr) to pass clang-tidy check * fix spelling mistake * improve comments for maintainability --- include/pybind11/detail/internals.h | 2 +- include/pybind11/subinterpreter.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 4f8de120f..003ee6e15 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -286,8 +286,8 @@ struct internals { internals() : static_property_type(make_static_property_type()), default_metaclass(make_default_metaclass()) { + tstate.set(nullptr); // See PR #5870 PyThreadState *cur_tstate = PyThreadState_Get(); - tstate = cur_tstate; istate = cur_tstate->interp; registered_exception_translators.push_front(&translate_exception); diff --git a/include/pybind11/subinterpreter.h b/include/pybind11/subinterpreter.h index 4b208ed9b..5d2f0a839 100644 --- a/include/pybind11/subinterpreter.h +++ b/include/pybind11/subinterpreter.h @@ -271,7 +271,7 @@ inline subinterpreter_scoped_activate::subinterpreter_scoped_activate(subinterpr // make the interpreter active and acquire the GIL old_tstate_ = PyThreadState_Swap(tstate_); - // save this in internals for scoped_gil calls + // save this in internals for scoped_gil calls (see also: PR #5870) detail::get_internals().tstate = tstate_; }