Skip to content

Do not lock while calling signal handles #1948

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
Apr 24, 2025

Conversation

BCSharp
Copy link
Member

@BCSharp BCSharp commented Apr 21, 2025

This PR removes a lock hold that used to be held during the execution of a custom signal handler. For two main reasons:

  1. CPython does not synchronize the execution of custom handlers (although it always runs them on the main thread) — if a signal comes during the execution of a custom signal handler function, the function will be interrupted and re-entered.
  2. In theory holding a lock during the callback could create a deadlock — in IronPython any thread can set a signal handler (in CPython — only the main thread), so if the callback waits on a thread that tries to change signal registrations, it will cause a deadlock.

Point 1 would technically be a breaking change since the callback has to be now thread-safe (re-entrant, like in CPython), except that the only signal currently supported is SIGINT and it will always result in ThreadAbortException, even when handled by the custom handler. This is deadly on POSIX since thread abort is unsupported, so likely nobody tries to handle it in this way.

There are some other smaller changes in this PR as well, mostly editorial.

temp.Invoke(signum, null);
}
} catch (Exception ex) {
System.Console.WriteLine(SignalPythonContext.FormatException(ex));
Copy link
Contributor

Choose a reason for hiding this comment

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

Console.WriteLine is a bit weird, but I guess that was there before... so LGTM!

Copy link
Member Author

@BCSharp BCSharp Apr 24, 2025

Choose a reason for hiding this comment

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

Yes, it is weird and yes, it was like that before. It seems that CPython prints it on stderr and then terminates. I will revisit it later once the signals on POSIX are working.

@BCSharp BCSharp merged commit 74ce419 into IronLanguages:main Apr 24, 2025
17 checks passed
@BCSharp BCSharp deleted the signal_nolock branch April 27, 2025 03: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