@@ -627,19 +627,23 @@ class internals_pp_manager {
627627 // / Get the pointer-to-pointer for the main interpreter, allocating it if it does not already
628628 // / exist. May acquire the GIL. Will never return nullptr.
629629 std::unique_ptr<InternalsType> *get_pp_for_main_interpreter () {
630- // This function **assumes** that the current thread is running in the main interpreter.
631630 if (!seen_main_interpreter_) {
631+ // The first call to this function **MUST** be from the main interpreter.
632+ // Here we **ASSUME** that the current thread is running in the main interpreter.
633+ // The caller is responsible for ensuring this.
632634 std::call_once (seen_main_interpreter_flag_, [&] {
633635 gil_scoped_acquire_simple gil;
634636 internals_singleton_pp_ = get_or_create_pp_in_state_dict ();
635637 seen_main_interpreter_ = true ;
636638 });
637639 }
640+ // This is shared between all threads and all interpreters.
638641 return internals_singleton_pp_;
639642 }
640643
641644 // / Drop all the references we're currently holding.
642645 void unref () {
646+ // See comment in get_pp() above.
643647#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
644648 if (get_num_interpreters_seen () > 1 ) {
645649 last_istate_tls () = nullptr ;
@@ -651,6 +655,7 @@ class internals_pp_manager {
651655 }
652656
653657 void destroy () {
658+ // See comment in get_pp() above.
654659#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
655660 if (get_num_interpreters_seen () > 1 ) {
656661 auto *tstate = get_thread_state_unchecked ();
@@ -711,8 +716,8 @@ class internals_pp_manager {
711716
712717 char const *holder_id_ = nullptr ;
713718 on_fetch_function *on_fetch_ = nullptr ;
719+ // Pointer to the singleton internals for the main interpreter
714720 std::unique_ptr<InternalsType> *internals_singleton_pp_;
715-
716721 std::once_flag seen_main_interpreter_flag_;
717722 std::atomic_bool seen_main_interpreter_{false };
718723};
0 commit comments