Repository navigation
Description
Problem
CODESTYLE.md "Interrupts" covers shell and Python scripts only. Nothing states how a .NET console app handles SIGINT, SIGTERM, or SIGQUIT, so each app gets whatever its entry point happened to wire up. The fleet's .NET console apps that run child tools have drifted into three different behaviors:
| App | Mechanism | Signals handled | Children on interrupt | Exit status on interrupt |
|---|---|---|---|---|
| PlexCleaner | Its own PosixSignalRegistration in Main, synchronous ParseResult.Invoke |
SIGINT, SIGTERM, SIGQUIT | Graceful: CliWrap's graceful token sends the child SIGINT | 130, 143, or 131 returned from Main, and 1 when the race below hits |
| PhotoCleaner | InvokeAsync with ProcessTerminationTimeout = null |
None | Not stopped, since the process ends at once | Ended by the signal, its "cancelled" path and log flush never run |
| AudioCleaner | InvokeAsync with the default ProcessTerminationTimeout |
SIGINT, SIGTERM | Forceful: ExecuteBufferedAsync(token) kills the child |
130 or 143 after 2 seconds, even while the action is still unwinding |
The PhotoCleaner and AudioCleaner rows are read from their source and from System.CommandLine's source, not from a run. The PlexCleaner row and everything below were measured in its Docker image, .NET 10.0.15 on Ubuntu, over roughly 120 interrupted runs.
What System.CommandLine Does and Does Not Do
From ProcessTerminationHandler and InvocationPipeline:
- The handler is created only for an asynchronous action under
InvokeAsync. ASynchronousCommandLineActionrun throughInvokegets no termination handling at all. ProcessTerminationTimeoutdefaults to 2 seconds. Past it,InvokeAsyncreturns 130 or 143 while the action is still running, soMainreturns and the cleanup is abandoned.ProcessTerminationTimeout = nullcreates no handler. The termination request is then not handled, and the process ends immediately unless the app registers its own handler.- SIGQUIT is not handled.
.NET 10 also no longer installs a SIGTERM handler of its own. So a docker stop ends an app with no registration immediately, with no ProcessExit and no log flush.
Findings from the Measured Runs
-
A terminal Ctrl-C and a container stop reach different processes. Ctrl-C, Ctrl-, and Ctrl-Z on a
docker run -itterminal signal the whole foreground process group. So the running child tool receives the signal at the same instant as the app.docker stop,docker kill --signal, and the signal a non-TTYdocker runclient proxies all reach PID 1 alone, so the app has to stop the child itself. An app tested on only one of the two paths has not shown that it handles the other. PlexCleaner's own registration handled every path below, serial and parallel, with and without--init:Stop Reaches Exit status Summary logged Ctrl-C on the terminal Process group 130 Yes Ctrl-\ on the terminal Process group 131 Yes docker stopPID 1 143 Yes docker kill --signal INT,QUIT,TERMPID 1 130, 131, 143 Yes Signal to a non-TTY docker runclientPID 1, proxied 130, 143 Yes Ctrl-D, Ctrl-Q, Ctrl-Z on the terminal Nothing that stops the run 0 Yes, as a full run -
The app can see its child die before its own handler runs. .NET dispatches a
PosixSignalRegistrationhandler asynchronously on a separate thread. On the process-group path, the child exits by the signal and the app reads that as a tool failure first. In 6 of 30 runs interrupted during startup tool verification, the app logged that the tool was not found and exited 1. The handler's "interrupted" line then appeared after the exit-code line. The exit status is decided before the handler has set it. -
A signal before registration ends the process with no summary. In 4 of 30 early runs the signal landed before
Mainregistered its handlers. The runtime's default then ended the process with status 130 and no logged summary. -
A launcher between the caller and the app can replace the status. Under
dotnet-coverage collect, 11 interrupted runs exited 0 five times, 130 five times, and 1 once. The app itself logged 130 in each. This is the same hazard the shell and Python rules already name foruv run. -
Ctrl-D, Ctrl-Q, and Ctrl-Z have no effect inside a container. Ctrl-D is end of input, Ctrl-Q is flow control, and SIGTSTP is ignored by PID 1. Each run finished normally with status 0. This is expected, and it is listed only because a past release documented Ctrl-Q and Ctrl-Z as stop keys.
A separate fault, an internal CLR error (0x80131506) on SIGINT during a tool run, prompted this investigation. It did not reproduce in any of the measured runs, and it is tracked in PlexCleaner's own issue rather than here.
Recommendation
Extend "Interrupts" to .NET console apps, with the mechanics in a "Interrupts in .NET" subsection packaged into the dotnet-codestyle Skill. The general bullets apply as written, release what is held, write atomically, and print one line naming the signal. The .NET mechanics would state:
- One handling path, registered first in
Main. The app registersPosixSignalRegistrationfor SIGINT, SIGTERM, and SIGQUIT before any other work. Each handler setscontext.Cancel = trueand cancels one process-wideCancellationTokenSource. That works for a synchronous and an asynchronous action alike. Where an app uses System.CommandLine's handler instead, it setsProcessTerminationTimeouttoTimeout.InfiniteTimeSpan, neither the 2-second default nor null. It still registers SIGQUIT itself. - A child that a signal ended is an interrupt, not a tool failure. A child exit status of 128 plus SIGINT, SIGTERM, or SIGQUIT means the stop reached the child first. The app then cancels its own token as the handler would and records that signal for the exit status. This removes the race in finding 2 without waiting on the handler thread.
- Children are stopped gracefully and waited for. On cancellation the app asks each child to stop, such as through CliWrap's graceful cancellation token, and waits for it. Forceful cancellation follows only after a bounded wait. A container stop reaches only PID 1, so this is the only path that stops the child there.
- An interrupted run exits 128 plus the signal number.
Mainreturns 130, 143, or 131 after cleanup and never 0 or 1. A .NET app has no portable way to re-raise the signal. In a container it usually runs as PID 1, which a re-raised signal does not end. So the status is returned rather than re-raised, which is the deliberate difference from the script rule. - A .NET app that runs a child process owes a test that signals it mid-run. The test covers both delivery paths, the process group and the app alone. It asserts the exit status, that the summary is logged, and that no child remains.
The open decision is whether the .NET mechanics prefer an app's own PosixSignalRegistration or System.CommandLine's handler configured as above. The recommendation is the app's own registration. It is the only one that covers a synchronous action and SIGQUIT, and it keeps the exit status in the app's hands.
Reproducing
A constructed setup reproduces findings 1 to 3 with any media tool image. It is six synthetic one-minute AVI files made with ffmpeg -f lavfi -i testsrc2, processed under docker run -it through script. It then sends \003 to the terminal after a delay, or docker stop the container. A delay under about 2 seconds lands in startup tool verification, where finding 2 shows.
Activity
ptr727 commentedon Nov 27, 2026
References for the implementer, 2026-11-26 triage (added at the maintainer's request):
develop,fcd0acc6): "Exit 130 When a Ctrl+C Ends a Tool Before the Signal Handler Runs". This is the fix for the exit-1 race this issue's table notes. Earlier signal work on the same path: Pass Cancellation into the Monitor and Add Monitor Tests PlexCleaner#1187 (cancellation passed into the monitor), Keep a File When a Signal Kills Its FfMpeg Verify PlexCleaner#1093 (keep a file when a signal kills its FfMpeg verify), and Keep a File Whose Repair Run a Signal Ended PlexCleaner#1112 (keep a file whose repair run a signal ended). The handler lives inPlexCleaner/Program.cs.PhotoCleaner/Program.cssetsProcessTerminationTimeout = nulland registers no handler of its own. The file was last changed in Complete LF Migration and Carry Review Skills PhotoCleaner#66 (2026-08-18), so it predates all of the PlexCleaner work above. Once the rule lands, adopting it there is a PhotoCleaner change, and AudioCleaner's default-timeout path is a second one.80131506cited in the body does not resolve in ptr727/PlexCleaner, so treat the PR links above as the references.ptr727 commentedon Nov 27, 2026
Design Settled, 2026-11-27
The maintainer decided the open points, and a prototype ran the stop matrix on .NET 11. The rule text follows in a pull request that links here.
Decisions
Corrections to the Text Above
PosixSignalRegistration. It usesConsole.CancelKeyPressandProcessExitonly on targets without that type.InvokeAsynctoo.Timeout.InfiniteTimeSpanis not a usable setting. The handler then blocks the signal thread until the action ends. The app's own registration withProcessTerminationTimeout = nullis the one arrangement that works.CancellationTokenSource.Cancel()runs its callbacks on the calling thread. In a signal handler that is the signal thread, so the handler callsCancelAsync().The Rules
Main. It registers the stop signals before parsing or logging. It owns a graceful stop token, a forceful kill token, and the recorded stop reason. Its token sources are never disposed.CancelAsync(), and arms the kill token withCancelAfter. The second cancels the kill token. The third cancels the kill token synchronously and leavescontext.Cancelunset. The handler does nothing else but log one line, inside a catch-all.ProcessTerminationTimeoutis null andEnableDefaultExceptionHandleris false. Actions take(ParseResult, CancellationToken), andInvokeAsyncreceives the stop token.Mainholds onetry, and itsfinallyflushes the log.StartDetachedand all standard streams redirected. Only the app then stops it, once.KillOnParentExitis set where the platform supports it. A stop that was requested means cancelled, and any other signal death is a tool failure. Anything a child starts that outlives it, such as a container, is removed by name in the stop path.Measured on .NET 11
The prototype is a console app on .NET 11.0.0, System.CommandLine 3.0.0, and CliWrap 3.10.5, in the
mcr.microsoft.com/dotnet/sdk:11.0image. Its child is a shell script that logs each signal it receives.KillOnParentExitsetbashloop, SIGINT to the groupdocker stop, app as PID 1docker kill --signal INTandHUP, PID 1docker stopunder--initdocker stop, child ignores SIGINT, PID 1docker run -itNotes for the Rule Text
env --default-signal=INT,QUITdoes.KillOnParentExithas no macOS support. A child there outlives an app that is killed.StartDetachedis left unset there, since the graceful stop attaches to the child's hidden console.A second rule set, for per-item state and parallel work, is filed as its own issue.
ptr727 commentedon Nov 27, 2026
Also Measured on musl
The same prototype ran in the
mcr.microsoft.com/dotnet/sdk:11.0-alpineimage, since an Alpine image is a shipping base in the fleet.LibraryImport("libc")resolves there, and the re-sent signal ends the app. SIGINT, SIGTERM, and SIGHUP each ended it by that signal after cleanup.KillOnParentExitholds on musl too.Extend the Interrupt Rule to .NET Console Apps (#4521)