Add SIGKILL to PosixSignal enumeration#124256
Conversation
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
|
Tagging subscribers to this area: @dotnet/interop-contrib |
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
adamsitnik
left a comment
There was a problem hiding this comment.
@copilot please address my feedback
...es/System.Private.CoreLib/src/System/Runtime/InteropServices/PosixSignalRegistration.Unix.cs
Outdated
Show resolved
Hide resolved
Per feedback from @adamsitnik, moved SIGKILL validation from the Unix-specific Register method to the platform-agnostic Create method. The validation now checks both the SIGKILL enum value (-11) and the raw POSIX signal number (9), ensuring SIGKILL cannot be registered on any platform. Removed the s_sigkillNumber field as it's no longer needed. Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Replaced magic number 9 with SIGKILL_POSIX_SIGNAL_NUMBER constant for better code clarity and maintainability. Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Updated comment to clarify that we check both PosixSignal.SIGKILL (-11) and raw signal number 9 cast to enum type (e.g., (PosixSignal)9). Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
...braries/System.Private.CoreLib/src/System/Runtime/InteropServices/PosixSignalRegistration.cs
Outdated
Show resolved
Hide resolved
Per feedback from @jkotas and @adamsitnik: - Changed exception from ArgumentException to IOException to match OS behavior - Removed check for raw signal number (9) - let OS throw IOException naturally - Only check PosixSignal.SIGKILL enum value to provide better error message - Updated test to expect IOException instead of ArgumentException - Removed unused SIGKILL_POSIX_SIGNAL_NUMBER constant This avoids a breaking change where the exception type would differ for raw signal numbers vs enum values. Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
...braries/System.Private.CoreLib/src/System/Runtime/InteropServices/PosixSignalRegistration.cs
Outdated
Show resolved
Hide resolved
adamsitnik
left a comment
There was a problem hiding this comment.
@copilot please address my feedback
...braries/System.Private.CoreLib/src/System/Runtime/InteropServices/PosixSignalRegistration.cs
Outdated
Show resolved
Hide resolved
Per feedback from @jkotas and @adamsitnik: - Removed SIGKILL check from platform-agnostic Create method - Removed Arg_CannotRegisterHandlerForSIGKILL resource string - Let OS-specific Register methods throw naturally: - Unix: IOException from OS when trying to enable signal handling - Windows: PlatformNotSupportedException (SIGKILL not in switch) - Updated tests to be platform-specific: - Unix test expects IOException - Windows test expects PlatformNotSupportedException This allows the OS to handle validation naturally without adding extra checks that provide no value. Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
|
@adamsitnik, at the moment this doesn't add any benefit, right? It's only once we add the ability to send a PosixSignal that this becomes meaningful? And at that point we'll update consumers like Process.Kill() to use this instead of P/Invoking directly? (The correctness of the mapping / handling of this value would be easier if the sending ability were introduced first.) |
There was a problem hiding this comment.
Pull request overview
This PR extends the System.Runtime.InteropServices.PosixSignal public API to include SIGKILL and wires it through the native System.Native signal mapping so it can be converted to/from platform signal numbers, along with updating unit tests to reflect SIGKILL’s non-registrable semantics.
Changes:
- Added
PosixSignal.SIGKILL = -11(Windows-unsupported) to the public enum and reference assembly. - Updated
System.NativePAL (pal_signal.h/.c) to mapSIGKILLin both conversion directions. - Updated
PosixSignalRegistrationunit tests to ensure SIGKILL registration throws and to exclude SIGKILL from “supported/registrable” datasets.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/native/libs/System.Native/pal_signal.h | Adds PosixSignalSIGKILL = -11 to the native enum. |
| src/native/libs/System.Native/pal_signal.c | Maps SIGKILL in TryConvertSignalCodeToPosixSignal and SystemNative_GetPlatformSignalNumber. |
| src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs | Adds SIGKILL to the reference assembly enum surface area. |
| src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/PosixSignal.cs | Adds SIGKILL to the runtime enum with XML doc + platform annotation. |
| src/libraries/System.Runtime.InteropServices/tests/.../PosixSignalRegistrationTests.Unix.cs | Adds Unix-only SIGKILL throw test and removes SIGKILL from registrable test data. |
| src/libraries/System.Runtime.InteropServices/tests/.../PosixSignalRegistrationTests.Windows.cs | Adds Windows SIGKILL throw test. |
...ropServices.UnitTests/System/Runtime/InteropServices/PosixSignalRegistrationTests.Windows.cs
Outdated
Show resolved
Hide resolved
...nteropServices.UnitTests/System/Runtime/InteropServices/PosixSignalRegistrationTests.Unix.cs
Outdated
Show resolved
Hide resolved
...ropServices.UnitTests/System/Runtime/InteropServices/PosixSignalRegistrationTests.Windows.cs
Outdated
Show resolved
Hide resolved
Per @jkotas feedback: add PosixSignal.SIGKILL to UninstallableSignals() on Unix so it's covered by Create_UninstallableSignal_Throws, and delete the explicit Create_SIGKILL_Throws tests from both Unix and Windows files (the Windows test was redundant with Create_InvalidSignal_Throws). Also remove the now-unnecessary 'using System.IO;' directive from the Unix file. Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
The Process interop uses SIGKILL and SIGSTOP: While this can replace uses of SIGKILL, we do not have a SIGSTOP. Also, the hardcoded 19 for SIGSTOP Is non-portable. Should we add SIGSTOP (and SIGCONT for parity) to the public API too? |
For me the main benefit is having the other upcoming PRs smaller and easier to review. Then we will just focus on sending the signals, not ensuring that existing APIs can't sign up for SIGKILL like in this PR. I'll try to do better in upcoming PRs. |
jkotas
left a comment
There was a problem hiding this comment.
LGTM (as an incremental step towards implementing full API proposal)
I personally prefer smaller PRs like this one (when it is well understood what we are working towards) |
I do as well, but I prefer it further when they're "complete". My preference would have been to first add the send API, which could be fully tested (and possibly used in various places). Then this PR would be able to actually validate that the signal mapping is working correctly by using that send API in a test to kill a child process, and could also be consumed in places like Process. It's still smaller, but it's more end-to-end. That said, we can go ahead with this one. |
|
@copilot Failing test |
SIGKILL cannot be caught or ignored, so iterating all PosixSignal enum values to register handlers fails for SIGKILL with IOException. Skip it in the test data, consistent with how SIGKILL is excluded from SupportedSignals in PosixSignalRegistrationTests. Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
... Fixed in commit 4813de4. |
|
/ba-g the build failure is #124322, which got already fixed in main |
Description
Extends
PosixSignalwithSIGKILL = -11. SIGKILL cannot be caught or ignored per POSIX semantics - the OS enforces this naturally by throwing appropriate exceptions when registration is attempted.Changes
PosixSignal.SIGKILL = -11with[UnsupportedOSPlatform("windows")]and XML doc "Kill (cannot be caught or ignored)"SIGKILL = -11entry with[UnsupportedOSPlatformAttribute("windows")]pal_signal.{h,c}to map SIGKILL inGetPlatformSignalNumberandTryConvertSignalCodeToPosixSignalIOExceptionwhen trying to enable signal handling for SIGKILLPlatformNotSupportedException(SIGKILL not in supported signals)PosixSignal.SIGKILLtoUninstallableSignals()on Unix; excluded SIGKILL fromSupportedSignals,PosixSignalAsRawValues, andProcessTests.SignalTestData()test dataContributes to #123380
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.