There were a number of issues related to python shutdown. These issues appear to be present on every platform, but are benign except for Windows. On Windows they present because Windows must build against a debug python interpreter, which asserts when these issues arise. The issues, along with their respective fixes, are:
- decrementing the reference count of dead objects. This happens because we store the session dictionary in the ScriptInterpreterPython class, and when these are destroyed on shutdown, they get decref'ed. However, this happens during or after a Py_Finalize, so Python has already destroyed these objects. The fix employed here is to add a Release() method to PythonObject() which simply drops the underlying object.
- Attempting to execute embedded python during Py_Finalize. This results in error messages "NameError: module 'lldb' not found" after tests run. The fix here is to only execute these commands if Py_IsInitialized()
- Use of sys.atexit(). atexit handlers are run as part of Py_Finalize, and at this point it is unspecified which, if any modules have been destroyed. So no meaningful code can run here. The fix employed here is to explicitly call lldb.SBDebugger.Terminate() before calling sys.exit(). A better fix would be to not use sys.exit() at all, but instead have a clean shutdown path, perhaps by way of raising an exception that is caught at the top-level of the python script.