From dfa2c0c81255e2a4d01864a62b762a622cf49fa2 Mon Sep 17 00:00:00 2001 From: bogdan-lab Date: Tue, 20 Sep 2022 22:58:36 +0300 Subject: [PATCH 1/2] Avoid local_internals destruction It allows to avoid possible static deinitialization fiasco. --- include/pybind11/detail/internals.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 86991955b5..abed610d73 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -514,8 +514,12 @@ struct local_internals { /// Works like `get_internals`, but for things which are locally registered. inline local_internals &get_local_internals() { - static local_internals locals; - return locals; + // Current static can be created in the interpreter finalization routine. If the later will be + // destroyed in another static variable destructor, creation of this static there will cause + // static deinitialization fiasco. In order to avoid it we avoid destruction of the + // local_internals static. + static auto *locals = new local_internals(); + return *locals; } /// Constructs a std::string with the given arguments, stores it in `internals`, and returns its From c296ce9997c997d0ce8c534473ce8adbc8b10c9b Mon Sep 17 00:00:00 2001 From: bogdan-lab Date: Wed, 21 Sep 2022 20:25:14 +0300 Subject: [PATCH 2/2] Add link to relevant google style guide discussion --- include/pybind11/detail/internals.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index abed610d73..d47084e263 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -517,7 +517,8 @@ inline local_internals &get_local_internals() { // Current static can be created in the interpreter finalization routine. If the later will be // destroyed in another static variable destructor, creation of this static there will cause // static deinitialization fiasco. In order to avoid it we avoid destruction of the - // local_internals static. + // local_internals static. One can read more about the problem and current solution here: + // https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables static auto *locals = new local_internals(); return *locals; }