diff --git a/src/runtime/pythonengine.cs b/src/runtime/pythonengine.cs index 5925880c0..bab5db1d8 100644 --- a/src/runtime/pythonengine.cs +++ b/src/runtime/pythonengine.cs @@ -192,6 +192,7 @@ public static void Initialize(IEnumerable args, bool setSysArgv = true, // Make sure we clean up properly on app domain unload. AppDomain.CurrentDomain.DomainUnload += OnDomainUnload; + AppDomain.CurrentDomain.ProcessExit += OnProcessExit; // The global scope gets used implicitly quite early on, remember // to clear it out when we shut down. @@ -249,6 +250,11 @@ static void OnDomainUnload(object _, EventArgs __) Shutdown(); } + static void OnProcessExit(object _, EventArgs __) + { + Shutdown(); + } + /// /// A helper to perform initialization from the context of an active /// CPython interpreter process - this bootstraps the managed runtime @@ -319,6 +325,7 @@ public static void Shutdown(ShutdownMode mode) // If the shutdown handlers trigger a domain unload, // don't call shutdown again. AppDomain.CurrentDomain.DomainUnload -= OnDomainUnload; + AppDomain.CurrentDomain.ProcessExit -= OnProcessExit; PyScopeManager.Global.Clear(); ExecuteShutdownHandlers(); diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index 8a3ad9231..69d4460a7 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -393,6 +393,10 @@ internal static void Shutdown(ShutdownMode mode) { Py_Finalize(); } + else + { + PyGILState_Release(state); + } } }