diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index dd0c2af95..b5e9d6eb7 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -602,27 +602,26 @@ public: /// acquire the GIL. Will never return nullptr. std::unique_ptr *get_pp() { #ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT - if (get_num_interpreters_seen() > 1) { - // Whenever the interpreter changes on the current thread we need to invalidate the - // internals_pp so that it can be pulled from the interpreter's state dict. That is - // slow, so we use the current PyThreadState to check if it is necessary. - auto *tstate = get_thread_state_unchecked(); - if (!tstate || tstate->interp != last_istate_tls()) { - gil_scoped_acquire_simple gil; - if (!tstate) { - tstate = get_thread_state_unchecked(); - } - last_istate_tls() = tstate->interp; - internals_p_tls() = get_or_create_pp_in_state_dict(); + // Whenever the interpreter changes on the current thread we need to invalidate the + // internals_pp so that it can be pulled from the interpreter's state dict. That is + // slow, so we use the current PyThreadState to check if it is necessary. + auto *tstate = get_thread_state_unchecked(); + if (!tstate || tstate->interp != last_istate_tls()) { + gil_scoped_acquire_simple gil; + if (!tstate) { + tstate = get_thread_state_unchecked(); } - return internals_p_tls(); + last_istate_tls() = tstate->interp; + internals_p_tls() = get_or_create_pp_in_state_dict(); } -#endif + return internals_p_tls(); +#else if (!internals_singleton_pp_) { gil_scoped_acquire_simple gil; internals_singleton_pp_ = get_or_create_pp_in_state_dict(); } return internals_singleton_pp_; +#endif } /// Drop all the references we're currently holding.