Skip to content

Commit 75bd292

Browse files
committed
Avoid recreating internals during type deallocation at shutdown.
1 parent 2848fd6 commit 75bd292

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

include/pybind11/detail/class.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, P
207207

208208
/// Cleanup the type-info for a pybind11-registered type.
209209
extern "C" inline void pybind11_meta_dealloc(PyObject *obj) {
210-
with_internals([obj](internals &internals) {
210+
with_internals_if_internals([obj](internals &internals) {
211211
auto *type = (PyTypeObject *) obj;
212212

213213
// A pybind11-registered type will:

include/pybind11/detail/internals.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,9 @@ struct internals {
322322
// completely shut down, In that case, we should not decref these objects because pymalloc
323323
// is gone.
324324
if (is_interpreter_alive()) {
325-
Py_CLEAR(static_property_type);
325+
Py_CLEAR(instance_base);
326326
Py_CLEAR(default_metaclass);
327+
Py_CLEAR(static_property_type);
327328
}
328329
}
329330
};
@@ -870,6 +871,17 @@ inline auto with_internals(const F &cb) -> decltype(cb(get_internals())) {
870871
return cb(internals);
871872
}
872873

874+
template <typename F>
875+
inline void with_internals_if_internals(const F &cb) {
876+
auto &ppmgr = get_internals_pp_manager();
877+
auto &internals_ptr = *ppmgr.get_pp();
878+
if (internals_ptr) {
879+
auto &internals = *internals_ptr;
880+
PYBIND11_LOCK_INTERNALS(internals);
881+
cb(internals);
882+
}
883+
}
884+
873885
template <typename F>
874886
inline auto with_exception_translators(const F &cb)
875887
-> decltype(cb(get_internals().registered_exception_translators,

0 commit comments

Comments
 (0)