@@ -710,22 +710,26 @@ class internals_pp_manager {
710710 }
711711
712712 void create_pp_content_once (std::unique_ptr<InternalsType> *pp) {
713- #ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
714- static std::mutex mtx;
715- std::lock_guard<std::mutex> lock (mtx);
713+ // Assume the GIL is held
714+ {
715+ #ifndef Py_GIL_DISABLED
716+ const gil_scoped_release_simple gil_release{};
716717#endif
717-
718- assert (*pp == nullptr );
719-
720- // Prevent re-creation of internals after destruction during interpreter shutdown.
721- // If pybind11 code (e.g., tp_traverse/tp_clear calling py::cast) runs after internals
722- // have been destroyed, a new empty internals would be created, causing type lookup
723- // failures. See https://github.com/pybind/pybind11/pull/5958#discussion_r2717645230.
724- if (pps_have_created_content_.find (pp) != pps_have_created_content_.end ()) {
725- pybind11_fail (" Reentrant call detected while fetching pybind11 internals!" );
718+ static std::mutex mtx;
719+ std::lock_guard<std::mutex> lock (mtx);
720+
721+ // Prevent re-creation of internals after destruction during interpreter shutdown.
722+ // If pybind11 code (e.g., tp_traverse/tp_clear calling py::cast) runs after internals
723+ // have been destroyed, a new empty internals would be created, causing type lookup
724+ // failures. See https://github.com/pybind/pybind11/pull/5958#discussion_r2717645230.
725+ if (pps_have_created_content_.find (pp) != pps_have_created_content_.end ()) {
726+ pybind11_fail (" Reentrant call detected while fetching pybind11 internals!" );
727+ }
728+ // Each pp can only create its internals once.
729+ pps_have_created_content_.insert (pp);
726730 }
727- // Each pp can only create its internals once.
728- pps_have_created_content_. insert (pp);
731+
732+ // Create the internals content. May call back into Python.
729733 pp->reset (new InternalsType ());
730734 }
731735
0 commit comments