Skip to content

Commit

Permalink
bugfix: Keep registered types until after Py_Finalize(). Fix #4459 (#…
Browse files Browse the repository at this point in the history
…4486)

* Keep registered types until after Py_Finalize(). Fix #4459

* Address reviewer comments
  • Loading branch information
Skylion007 authored Feb 6, 2023
1 parent 9ef65ce commit b2c1978
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions include/pybind11/embed.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,16 @@ inline void finalize_interpreter() {
if (builtins.contains(id) && isinstance<capsule>(builtins[id])) {
internals_ptr_ptr = capsule(builtins[id]);
}

Py_Finalize();

// Local internals contains data managed by the current interpreter, so we must clear them to
// avoid undefined behaviors when initializing another interpreter
// Must be cleared only after Py_Finalize() so atexit and other hooks can still use
// registered_types
detail::get_local_internals().registered_types_cpp.clear();
detail::get_local_internals().registered_exception_translators.clear();

Py_Finalize();

if (internals_ptr_ptr) {
delete *internals_ptr_ptr;
*internals_ptr_ptr = nullptr;
Expand Down

0 comments on commit b2c1978

Please sign in to comment.