Skip to content

Commit a29a591

Browse files
committed
Keep registered types until after Py_Finalize(). Fix pybind#4459
1 parent 3efe9d4 commit a29a591

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

include/pybind11/embed.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,15 @@ inline void finalize_interpreter() {
254254
if (builtins.contains(id) && isinstance<capsule>(builtins[id])) {
255255
internals_ptr_ptr = capsule(builtins[id]);
256256
}
257+
258+
Py_Finalize();
259+
257260
// Local internals contains data managed by the current interpreter, so we must clear them to
258261
// avoid undefined behaviors when initializing another interpreter
262+
// Must be cleared after Py_Finalize() so atexit and other hooks can use registered_types
259263
detail::get_local_internals().registered_types_cpp.clear();
260264
detail::get_local_internals().registered_exception_translators.clear();
261265

262-
Py_Finalize();
263-
264266
if (internals_ptr_ptr) {
265267
delete *internals_ptr_ptr;
266268
*internals_ptr_ptr = nullptr;

0 commit comments

Comments
 (0)