@@ -564,15 +564,6 @@ class internals_pp_manager {
564564 // / acquire the GIL. Will never return nullptr.
565565 std::unique_ptr<InternalsType> *get_pp () {
566566#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
567- // FIXME: We cannot use `get_num_interpreters_seen() > 1` here to create a fast path for
568- // the multi-interpreter case. The singleton may be initialized by a subinterpreter not the
569- // main interpreter.
570- //
571- // For multi-interpreter support, the subinterpreters can be initialized concurrently, and
572- // the first time this function may not be called in the main interpreter.
573- // For example, a clean main interpreter that does not import any pybind11 module and then
574- // spawns multiple subinterpreters using `InterpreterPoolExecutor` that each imports a
575- // pybind11 module concurrently.
576567 if (get_num_interpreters_seen () > 1 ) {
577568 // Whenever the interpreter changes on the current thread we need to invalidate the
578569 // internals_pp so that it can be pulled from the interpreter's state dict. That is
@@ -589,29 +580,15 @@ class internals_pp_manager {
589580 return internals_p_tls ();
590581 }
591582#endif
592- return get_pp_for_main_interpreter ();
593- }
594-
595- // / Get the pointer-to-pointer for the main interpreter, allocating it if it does not already
596- // / exist. May acquire the GIL. Will never return nullptr.
597- std::unique_ptr<InternalsType> *get_pp_for_main_interpreter () {
598- if (!seen_main_interpreter_) {
599- // The first call to this function **MUST** be from the main interpreter.
600- // Here we **ASSUME** that the current thread is running in the main interpreter.
601- // The caller is responsible for ensuring this.
602- std::call_once (seen_main_interpreter_flag_, [&] {
603- gil_scoped_acquire_simple gil;
604- internals_singleton_pp_ = get_or_create_pp_in_state_dict ();
605- seen_main_interpreter_ = true ;
606- });
583+ if (!internals_singleton_pp_) {
584+ gil_scoped_acquire_simple gil;
585+ internals_singleton_pp_ = get_or_create_pp_in_state_dict ();
607586 }
608- // This is shared between all threads and all interpreters.
609587 return internals_singleton_pp_;
610588 }
611589
612590 // / Drop all the references we're currently holding.
613591 void unref () {
614- // See comment in get_pp() above.
615592#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
616593 if (get_num_interpreters_seen () > 1 ) {
617594 last_istate_tls () = nullptr ;
@@ -623,7 +600,6 @@ class internals_pp_manager {
623600 }
624601
625602 void destroy () {
626- // See comment in get_pp() above.
627603#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
628604 if (get_num_interpreters_seen () > 1 ) {
629605 auto *tstate = get_thread_state_unchecked ();
@@ -684,10 +660,9 @@ class internals_pp_manager {
684660
685661 char const *holder_id_ = nullptr ;
686662 on_fetch_function *on_fetch_ = nullptr ;
687- // Pointer to the singleton internals for the main interpreter
663+ // Pointer-to-pointer to the singleton internals for the first seen interpreter (may not be the
664+ // main interpreter)
688665 std::unique_ptr<InternalsType> *internals_singleton_pp_;
689- std::once_flag seen_main_interpreter_flag_;
690- std::atomic_bool seen_main_interpreter_{false };
691666};
692667
693668// If We loaded the internals through `state_dict`, our `error_already_set`
0 commit comments