Skip to content

Deliver Posix signals in reverse order of their registration #116557

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 5 commits into from
Jun 12, 2025

Conversation

jkotas
Copy link
Member

@jkotas jkotas commented Jun 11, 2025

Fixes #116556

Copy link
Contributor

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

@@ -8,23 +8,20 @@ namespace System.Runtime.InteropServices
{
public sealed partial class PosixSignalRegistration
{
private static readonly HashSet<Token> s_registrations = new();
private static readonly Dictionary<int, List<Token>> s_registrations = new();
Copy link
Member Author

Choose a reason for hiding this comment

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

This is structured the same way as the non-Windows signal registrations.

@@ -9,9 +9,9 @@ namespace System.Runtime.InteropServices
{
public sealed partial class PosixSignalRegistration
{
private static readonly Dictionary<int, HashSet<Token>> s_registrations = Initialize();
private static readonly Dictionary<int, List<Token>> s_registrations = Initialize();
Copy link
Member Author

Choose a reason for hiding this comment

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

List<Token> will require O(N) linear search for unregistration. I do not expect it to be a problem since the number of these registrations is expected to be fairly small.

…ime.InteropServices.UnitTests/System/Runtime/InteropServices/PosixSignalRegistrationTests.Unix.cs
…ime.InteropServices.UnitTests/System/Runtime/InteropServices/PosixSignalRegistrationTests.Unix.cs
@tmat
Copy link
Member

tmat commented Jun 11, 2025

Would you mind adding a note to documentation of the registration API that specifies the order of execution of the handlers? So that we don't rely on undocumented behavior in dotnet-watch (or elsewhere).

@jkotas jkotas marked this pull request as ready for review June 11, 2025 22:22
@jkotas jkotas requested review from tmat and ericstj June 11, 2025 22:22
It is unnecessary behavior change
@@ -24,6 +24,7 @@ public sealed partial class PosixSignalRegistration : IDisposable
/// <exception cref="PlatformNotSupportedException"><paramref name="signal"/> is not supported by the platform.</exception>
/// <exception cref="IOException">An error occurred while setting up the signal handling or while installing the handler for the specified signal.</exception>
/// <remarks>
/// The handlers are executed in reverse order of their registration.
Copy link
Member Author

Choose a reason for hiding this comment

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

I will submit this to the official docs too once this is merged.

Copy link
Member

@agocke agocke left a comment

Choose a reason for hiding this comment

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

LGTM

// Throw for everything else.
int error = Marshal.GetLastPInvokeError();
if (error != Interop.Errors.ERROR_INVALID_PARAMETER)
tokens.Remove(token);
Copy link
Member

Choose a reason for hiding this comment

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

If I understand this correctly, token will almost always be at the end of this list. Do we want to search from the end to avoid quadratic behavior when removing all the tokens? It probably won't matter since there are likely < 10 signal handlers registered in the average application.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, nvm, I see no reason to assume that an arbitrary call to Unregister should be trying to remove the most recently registered signal.

@jkotas
Copy link
Member Author

jkotas commented Jun 12, 2025

/ba-g runtime (Build android-x64 Release AllSubsets_CoreCLR) timing out on many PRs

@github-actions github-actions bot locked and limited conversation to collaborators Jul 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Posix signal handlers run in non-determinstic order
4 participants