Skip to content

Handle ProcessExit event #1458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public static void Initialize(IEnumerable<string> 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.
Expand Down Expand Up @@ -249,6 +250,11 @@ static void OnDomainUnload(object _, EventArgs __)
Shutdown();
}

static void OnProcessExit(object _, EventArgs __)
{
Shutdown();
}

/// <summary>
/// A helper to perform initialization from the context of an active
/// CPython interpreter process - this bootstraps the managed runtime
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ internal static void Shutdown(ShutdownMode mode)
{
Py_Finalize();
}
else
{
PyGILState_Release(state);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems unrelated, why did you add this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this Py_Finalize called when Python finally shuts down hangs because it can't acquire GIL.

}
}
}

Expand Down