pybind11: segfault on process exit with static py::object
Two possible solutions:
Instead of a local static object, you can define a static member of a pybind11 module or class. Then the object's lifetime is tied to the bindings, which are managed by the python interpreter and destructed correctly.
Another way is to manually destruct the object using a pythonic atexit
callback (Here's an example).
You're right that C++ objects are guaranteed to destruct in LIFO order, but that doesn't constrain the Python interpreter. The python interpreter is shutdown using the function Py_FinalizeEx
before the C/C++ unwinding happens.