Skip to content

Commit 3e77ce9

Browse files
committed
Try fix thread-safety
1 parent be97110 commit 3e77ce9

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

include/pybind11/detail/internals.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -602,27 +602,26 @@ class internals_pp_manager {
602602
/// acquire the GIL. Will never return nullptr.
603603
std::unique_ptr<InternalsType> *get_pp() {
604604
#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
605-
if (get_num_interpreters_seen() > 1) {
606-
// Whenever the interpreter changes on the current thread we need to invalidate the
607-
// internals_pp so that it can be pulled from the interpreter's state dict. That is
608-
// slow, so we use the current PyThreadState to check if it is necessary.
609-
auto *tstate = get_thread_state_unchecked();
610-
if (!tstate || tstate->interp != last_istate_tls()) {
611-
gil_scoped_acquire_simple gil;
612-
if (!tstate) {
613-
tstate = get_thread_state_unchecked();
614-
}
615-
last_istate_tls() = tstate->interp;
616-
internals_p_tls() = get_or_create_pp_in_state_dict();
605+
// Whenever the interpreter changes on the current thread we need to invalidate the
606+
// internals_pp so that it can be pulled from the interpreter's state dict. That is
607+
// slow, so we use the current PyThreadState to check if it is necessary.
608+
auto *tstate = get_thread_state_unchecked();
609+
if (!tstate || tstate->interp != last_istate_tls()) {
610+
gil_scoped_acquire_simple gil;
611+
if (!tstate) {
612+
tstate = get_thread_state_unchecked();
617613
}
618-
return internals_p_tls();
614+
last_istate_tls() = tstate->interp;
615+
internals_p_tls() = get_or_create_pp_in_state_dict();
619616
}
620-
#endif
617+
return internals_p_tls();
618+
#else
621619
if (!internals_singleton_pp_) {
622620
gil_scoped_acquire_simple gil;
623621
internals_singleton_pp_ = get_or_create_pp_in_state_dict();
624622
}
625623
return internals_singleton_pp_;
624+
#endif
626625
}
627626

628627
/// Drop all the references we're currently holding.

0 commit comments

Comments
 (0)