Skip to content

Commit 708ca55

Browse files
committed
Update lock scope
1 parent 33ffa8e commit 708ca55

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

include/pybind11/detail/internals.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -716,18 +716,21 @@ class internals_pp_manager {
716716
}
717717

718718
void create_pp_content_once(std::unique_ptr<InternalsType> *const pp) {
719-
std::lock_guard<std::mutex> lock(pp_set_mutex_);
720-
721-
// Detect 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 have
723-
// been destroyed, a new empty internals would be created, causing type lookup failures.
724-
// See also get_or_create_pp_in_state_dict() comments.
725-
if (pps_have_created_content_.find(pp) != pps_have_created_content_.end()) {
726-
pybind11_fail("pybind11::detail::internals_pp_manager::create_pp_content_once() "
727-
"FAILED: reentrant call detected while fetching pybind11 internals!");
719+
{
720+
std::lock_guard<std::mutex> lock(pp_set_mutex_);
721+
722+
// Detect re-creation of internals after destruction during interpreter shutdown.
723+
// If pybind11 code (e.g., tp_traverse/tp_clear calling py::cast) runs after internals
724+
// have been destroyed, a new empty internals would be created, causing type lookup
725+
// failures. See also get_or_create_pp_in_state_dict() comments.
726+
if (pps_have_created_content_.find(pp) != pps_have_created_content_.end()) {
727+
pybind11_fail(
728+
"pybind11::detail::internals_pp_manager::create_pp_content_once() "
729+
"FAILED: reentrant call detected while fetching pybind11 internals!");
730+
}
731+
// Each interpreter can only create its internals once.
732+
pps_have_created_content_.insert(pp);
728733
}
729-
// Each interpreter can only create its internals once.
730-
pps_have_created_content_.insert(pp);
731734

732735
// Assume the GIL is held here. May call back into Python.
733736
// Create the internals content.

0 commit comments

Comments
 (0)