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

Conversation

lostmsu
Copy link
Member

@lostmsu lostmsu commented May 21, 2021

This avoids segfault when CLR (in particular Mono) is unloaded before Python stops.

What does this implement/fix? Explain your changes.

  1. Added a handler to AppDomain.ProcessExit, that fires in the situations like this instead of AppDomain.DomainUnload. This allows Python.NET to remove all slots, that are implemented in C#.
  2. When initialized as extension (e.g. Python hosts CLR), release GIL acquired during shutdown so that Python finalization can resume.

Does this close any currently open issues?

This is (at least one of) the reason of CI test crashes in #1134

@@ -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.

@lostmsu
Copy link
Member Author

lostmsu commented May 22, 2021

I debugged the test failures, and they appear to be caused by gaps in domain reload support: when .NET objects that Python sees get severed from CLR, subsequent calls to their tp_dealloc fail because GetManagedObject returns null.

@lostmsu lostmsu force-pushed the bugs/PyCrashOnExit branch from 914ff18 to 366f548 Compare May 23, 2021 07:42
@lostmsu
Copy link
Member Author

lostmsu commented May 23, 2021

Found a way to solve the same problem and more without handling ProcessExit

@lostmsu lostmsu closed this May 23, 2021
@lostmsu
Copy link
Member Author

lostmsu commented May 29, 2021

Or maybe not. Seems to still be essential for #1134

@lostmsu lostmsu reopened this May 29, 2021
@lostmsu lostmsu requested a review from filmor May 29, 2021 23:10
@lostmsu lostmsu merged commit 7eac886 into pythonnet:master May 29, 2021
@lostmsu lostmsu deleted the bugs/PyCrashOnExit branch May 29, 2021 23:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants