Improve ConsoleCancellationManager with process termination timeout#17251
Conversation
Merge improvements from System.CommandLine ProcessTerminationHandler: - Add processTerminationTimeout (5s) for graceful shutdown after signals - Use PosixSignalRegistration for both SIGINT and SIGTERM - Add AppDomain.ProcessExit handler for fallback platforms - Wait for handler task completion within timeout before forced exit - Race handler task against ProcessTerminationCompletionSource via Task.WhenAny Fixes #17203 Fixes #17223
There was a problem hiding this comment.
Pull request overview
This PR updates Aspire CLI process-termination handling to more closely match System.CommandLine’s ProcessTerminationHandler, aiming to cancel gracefully on termination signals and, after a timeout, force termination with a signal-appropriate exit code.
Changes:
- Adds a 5-second graceful shutdown timeout via
ConsoleCancellationManagerand a termination completion source. - Switches to using
PosixSignalRegistrationfor SIGINT and SIGTERM (with a fallback path on some platforms). - Updates
Program.Mainto race command execution against a termination completion task and disablesSystem.CommandLine’s process-termination timeout handling.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/Aspire.Cli/Program.cs | Uses the new cancellation manager timeout behavior and races handler completion vs. termination completion for exit behavior. |
| src/Aspire.Cli/ConsoleCancellationManager.cs | Implements SIGINT/SIGTERM handling plus a graceful shutdown wait and forced termination signaling. |
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 17251Or
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 17251" |
PR Testing ReportPR Information
CLI Version Verification
Changes AnalyzedFiles Changed
Change Categories
Test Scenarios ExecutedScenario 1: Basic CLI FunctionalityObjective: Verify CLI still works normally (no regressions) Steps:
Observations:
Scenario 2: Start and Graceful StopObjective: Verify Steps:
Observations:
Scenario 3: Ctrl+C / Signal Termination (Core PR behavior)Objective: Test SIGINT (Ctrl+C) triggers graceful shutdown within the 5-second timeout Steps:
Evidence:
Observations:
Summary
Overall Result✅ PR VERIFIED The
|
|
❓ CLI E2E Tests unknown — 92 passed, 0 failed, 2 unknown (commit View all recordings
📹 Recordings uploaded automatically from CI run #26102544581 |
|
✅ No documentation update needed. docs_optional → internal_refactor No signals triggered ( |
Summary
Errors were caused by a race between System.Commandline built-in cancellaiton handling and Aspire CLIs. Disabled System.Commandline by removing its timeout, and moved timeout feature to our type.
Merges improvements from
System.CommandLine.ProcessTerminationHandlerintoConsoleCancellationManagerto provide a graceful shutdown timeout when the CLI receives termination signals.Changes
PosixSignalRegistration; now both signals use the preferred API uniformly (with fallback toConsole.CancelKeyPress+AppDomain.ProcessExiton unsupported platforms).ProcessTerminationCompletionSourceso the process exits promptly with the correct signal exit code (130 for SIGINT, 143 for SIGTERM) if the handler doesn't finish within the timeout.Fixes
Ctrl+Cto stop theaspire newwill meet 'An unexpected error occurred: A task was canceled ' #17203aspire agent initthrows an error during ctrl-c, isntead of exiting gracefully #17223