You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add aspire stop --wait to wait for resource teardown - #22583
aspire stop reported success as soon as the AppHost process exited, but DCP was still removing the instance's containers in the background. A start issued right after could adopt a still-running fixed-name container (WithContainerName(...)), which the old teardown then removed about a minute later and broke every caller. Agents following "stop, then restart" guidance hit this often.
User-facing usage
New opt-in aspire stop --wait: returns only after the AppHost's resources (containers, executables, networks) have been torn down. It waits up to 120 seconds by default (--timeout <seconds>, 1 to 4294967, changes this and requires --wait), and reports clearly when it cannot confirm the teardown. The AppHost is stopped in every case:
Timeout: error and exit code 17 (WaitTimeout).
AppHost too old to support waiting: error and exit code 9 (AppHostIncompatible).
Teardown not confirmed: error and exit code 2.
Works with --pid, --all, and --force. With --force, persistent-resource cleanup still runs when teardown is unconfirmed. Not allowed with the hidden tray protocol.
Default behavior (no --wait) has the same timing as before. After the AppHost exits, stop now prints this message once: ℹ️ Resources may still be stopping in the background. Use --wait to wait until they are torn down.
$ aspire stop --wait --apphost StopWaitRepro.AppHost.csproj --non-interactive
📦 Found running AppHost: .../StopWaitRepro.AppHost.csproj
🛑 Sending stop signal to .../StopWaitRepro.AppHost.csproj...
Waiting for the resources of .../StopWaitRepro.AppHost.csproj to be torn down...
Stopping .../StopWaitRepro.AppHost.csproj...
✅ .../StopWaitRepro.AppHost.csproj stopped successfully.
✅ The resources of .../StopWaitRepro.AppHost.csproj were torn down.
--wait Wait until the AppHost's resources, such as containers, are torn down before returning
--timeout <timeout> Maximum time in seconds to wait for resources to be torn down (requires --wait) [default: 120]
How it works
The "teardown complete" signal comes from DCP's own cleanup status, not from sleeps:
AppHost:StopAsync(StopAppHostRequest { WaitForResourceCleanup = true }) calls the new ApplicationOrchestrator/DcpExecutor.StopAndWaitForResourceCleanupAsync. That uses the existing CleanupResourcesAsync, which waits for DCP to report ResourcesCleanedUp (programs stopped, non-persistent containers deleted). Cleanup failures are reported by DCP itself through its logs, which reach the AppHost via the DCP log stream. The AppHost then responds with ResourcesCleanedUp and stops itself. It stops even if the client cancels the wait.
Capability: the AppHost advertises the new stop-resource-cleanup.v1 capability. Older AppHosts don't advertise it; for those, the CLI stops normally and reports that waiting is unsupported.
CLI:StopCommand sends the teardown request bounded by --timeout (120 seconds by default), then runs the existing process-exit monitoring and force-kill fallback. It doesn't resend the stop over a connection the AppHost may already have closed.
stop --all: with --wait, orphaned AppHosts (whose launching CLI died) go through the same teardown-aware stop instead of the up-front orphan collection, which only waits for process exit. Without --wait, AppHosts stopped by that collection also trigger the background-teardown hint.
Docs:docs/specs/cli-backchannel.md and src/Aspire.Cli/README.md are updated.
Starting an instance that is still being torn down is unchanged and out of scope here.
Validation
dotnet test --project tests/Aspire.Cli.Tests/Aspire.Cli.Tests.csproj --no-launch-profile -- --filter-class "*.StopCommandTests" --filter-not-trait "quarantined=true" --filter-not-trait "outerloop=true": passed (81/81), including the new --wait tests (success, default, custom and maximum --timeout expiry, invalid and out-of-range --timeout values, unsupported, unconfirmed, --force --wait, and the one-time hint without --wait).
dotnet test --project tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj --no-launch-profile -- --filter-class "*.DcpExecutorTests" --filter-class "*.AuxiliaryBackchannelTests" --filter-class "*.AuxiliaryBackchannelRpcTargetTests" --filter-class "*.ApplicationOrchestratorTests" --filter-not-trait "quarantined=true" --filter-not-trait "outerloop=true": 512 passed, 1 failed. The failure, CanConnectMultipleClientsToAuxiliaryBackchannel, also fails on unmodified main on this machine (an extra socket client), so it is environmental. New tests cover the executor cleanup wait and the end-to-end StopAsync RPC with real DCP.
New Docker-backed CLI E2E test StopWaitTests.StopWaitRemovesFixedNameContainerBeforeReturning: starts a fixed-name container, runs stop --wait, checks once (no polling) that the container is gone, then restarts and verifies the container stays up. It passes in CI.
Dogfood testing with the PR's CI-built CLI on macOS + Docker: 7 scenarios passed, including stop --wait followed by an immediate restart that stayed healthy for 60 seconds, and an AppHost on stable 17.0.0 getting exit code 9 (report).
Full Aspire.Cli.Tests run (same filters): 10595 passed, 8 failed. The failures are in NuGetPackageOperationConfigurationTests, PrebuiltAppHostServerTests and DotNetCliRunnerTests.CandidateRestore_* (NuGet/restore), which this change doesn't touch. I didn't rerun them on main.
Manual check with the source-built CLI and an AppHost with two WithContainerName Redis containers on macOS + Docker:
Without --wait: reproduced the bug (both containers still Up after "stopped successfully").
With --wait: returned after about 4 s, with both containers gone.
stop --wait followed immediately by start: the new containers stayed Running/Healthy after 45 s.
--pid --wait: containers removed, and the AppHost and launcher processes exited.
aspire stop returned once the AppHost process exited, while DCP was still
removing the instance's containers. A start issued right away could adopt
a fixed-name container that was then removed.
- New opt-in --wait option: the CLI asks the AppHost (via the stop RPC,
gated on the new stop-resource-cleanup.v1 capability) to have DCP clean
up its resources, waits up to 2 minutes for DCP to confirm, and reports
timeouts (exit 17), unsupported AppHosts (exit 9) or unconfirmed teardown.
- Without --wait, stop now prints that teardown may continue in the
background and suggests --wait.
Fixes#22510
Co-authored-by: Copilot App <[email protected]>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Cleanup can report success while resources remain, and stop --all --wait bypasses teardown verification for orphaned AppHosts, making immediate restart unsafe.
Adds opt-in aspire stop --wait to coordinate CLI shutdown with DCP resource cleanup, addressing the stop/restart race in #22510. This connects the CLI’s stop command to a new AppHost backchannel capability while preserving default stop timing.
Changes:
Adds cleanup waiting, timeout and compatibility reporting, plus a one-time hint when stopping without --wait.
Extends the backchannel and hosting orchestrator to request and report resource cleanup.
Adds focused tests, documentation, and localization entries.
- stop --all --wait routes orphaned AppHosts through the teardown-aware
stop instead of the process-exit-only orphan collector.
- After DCP reports cleanup complete, verify no application objects remain
before reporting resources as torn down, since DCP reports completion even
when cleanup fails.
- Add a Docker-backed CLI E2E test for stop --wait with a fixed-name container.
Co-authored-by: Copilot App <[email protected]>
Count collected orphan AppHosts for teardown warnings
src/Aspire.Cli/Commands/StopCommand.cs:595
When stop --all stops only orphaned AppHosts, CollectAsync removes their sockets before the subsequent scan, so none reach CountStoppedWithoutTeardownWait. The counter remains zero and the new teardown warning is omitted even though those AppHosts' resources may still be stopping. Include successfully collected orphans in _appHostsStoppedWithoutTeardownWait so this path also emits the one-time warning.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
When stop --all finds only orphaned AppHosts, it omits the background-teardown warning because collected hosts are not added to the count of hosts stopped without waiting for teardown.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Include collected AppHosts in the teardown-wait warning count
src/Aspire.Cli/Commands/StopCommand.cs:595
When stop --all finds only orphaned AppHosts, CollectAsync stops them and deletes their sockets before the next scan. None then reach CountStoppedWithoutTeardownWait, so the command returns successfully without the new background-teardown warning, even though collection only waits for process exit. Please add the collected count to _appHostsStoppedWithoutTeardownWait so this path also emits the one-time --wait hint.
Addressed the finding from this review in f0e5558: without --wait, stop --all now adds orphaned AppHosts it collected to the count of AppHosts stopped without waiting for teardown, so the one-time --wait hint is shown when only orphaned AppHosts were running. Covered by StopCommand_All_ReportsThatResourceTeardownMayContinueForCollectedOrphanedAppHost.
CLI changes: new aspire stop --wait option, a background-teardown hint after the default stop, and --all --wait handling for orphaned AppHosts (src/Aspire.Cli/Commands/StopCommand.cs, backchannel, resources).
Hosting changes: the stop request can now ask the AppHost to wait for resource cleanup, behind a new stop-resource-cleanup.v1 capability. DcpExecutor checks for leftover DCP objects after cleanup.
Dashboard changes
Template changes
Client/Component changes
VS Code extension changes
Test changes: CLI unit tests, hosting tests, and the new StopWaitTests E2E (passes in CI).
CI infrastructure changes
Test Scenarios Executed
All scenarios used a C# file-based AppHost (apphost.cs, the issue's setup) created with aspire new aspire-empty from the PR hive. It ran two redis:7 containers with fixed names (WithContainerName).
Scenario 1: Default aspire stop (no --wait)
Coverage Type: Happy path. Status: ✅ Passed
Exited 0, printed stopped successfully, then: ℹ️ Resources may still be stopping in the background. Use --wait to wait until they are torn down.
docker ps right after the command returned still showed both containers Up (the original bug, now surfaced to the user). They were gone 15s later.
Scenario 2: aspire stop --wait, then immediate aspire start (the issue's repro)
stop --wait exited 0 in about 4s. It printed Waiting for the resources of apphost.cs to be torn down..., stopped successfully, and The resources of apphost.cs were torn down.
docker ps -a --filter name=prtest-22583 right after the command returned: empty.
After an immediate aspire start, both containers were still Up 59 seconds 60s later, and aspire describe showed cache and cache2 Running/Healthy. No adoption-then-removal.
Printed stopped successfully, then ❌ apphost.cs does not support waiting for its resources to be torn down, so they may still be stopping in the background. Update the AppHost to a newer Aspire version to use --wait., exit 9.
The AppHost process had exited, and its container was cleaned up in the background.
CI
All 353 checks pass on f0e5558, including Cli.EndToEnd-StopWaitTests and Cli.EndToEnd-StopForceTests.
Summary
Scenario
Status
Notes
1. Default stop
✅ Passed
Hint shown; containers still up right after return
2. stop --wait + immediate restart
✅ Passed
Containers gone on return; restart healthy after 60s
3. stop --wait --pid
✅ Passed
4. stop --all --wait
✅ Passed
Unhappy: unknown --pid
✅ Passed
Exit 7, nothing stopped
Unhappy: nothing running
✅ Passed
Exit 0, no hint
Unhappy: stable 17.0.0 AppHost
✅ Passed
Exit 9, AppHost stopped
Overall Result
✅ PR VERIFIED
Recommendations
None blocking. stop --all shows a shortened path (StopWaitApp/apphost.cs) where the single-AppHost stop shows apphost.cs; this is existing display behavior, not new in this PR.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
The timeout validation accepts values beyond the timer’s supported range, causing an exception before any stop request is sent and leaving the AppHost running.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Human assessment is needed because the new tests do not cover cancellation during an in-flight teardown, leaving uncertainty about whether the AppHost stops after cleanup begins.
Updated aspire-stop.mdx to document --wait and --timeout, exit codes (17/9/2), and the new default-behavior message. Signals: cli_option_added (--wait Option in StopCommand.cs), cli_command_file_changed, cli_resource_strings_changed (StopCommandStrings.resx), pr_body_has_cli_flag_mention, pr_body_has_user_facing_section.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
aspire stopreported success as soon as the AppHost process exited, but DCP was still removing the instance's containers in the background. A start issued right after could adopt a still-running fixed-name container (WithContainerName(...)), which the old teardown then removed about a minute later and broke every caller. Agents following "stop, then restart" guidance hit this often.User-facing usage
aspire stop --wait: returns only after the AppHost's resources (containers, executables, networks) have been torn down. It waits up to 120 seconds by default (--timeout <seconds>, 1 to 4294967, changes this and requires--wait), and reports clearly when it cannot confirm the teardown. The AppHost is stopped in every case:WaitTimeout).AppHostIncompatible).--pid,--all, and--force. With--force, persistent-resource cleanup still runs when teardown is unconfirmed. Not allowed with the hidden tray protocol.--wait) has the same timing as before. After the AppHost exits, stop now prints this message once:ℹ️ Resources may still be stopping in the background. Use --wait to wait until they are torn down.How it works
The "teardown complete" signal comes from DCP's own cleanup status, not from sleeps:
StopAsync(StopAppHostRequest { WaitForResourceCleanup = true })calls the newApplicationOrchestrator/DcpExecutor.StopAndWaitForResourceCleanupAsync. That uses the existingCleanupResourcesAsync, which waits for DCP to reportResourcesCleanedUp(programs stopped, non-persistent containers deleted). Cleanup failures are reported by DCP itself through its logs, which reach the AppHost via the DCP log stream. The AppHost then responds withResourcesCleanedUpand stops itself. It stops even if the client cancels the wait.stop-resource-cleanup.v1capability. Older AppHosts don't advertise it; for those, the CLI stops normally and reports that waiting is unsupported.StopCommandsends the teardown request bounded by--timeout(120 seconds by default), then runs the existing process-exit monitoring and force-kill fallback. It doesn't resend the stop over a connection the AppHost may already have closed.stop --all: with--wait, orphaned AppHosts (whose launching CLI died) go through the same teardown-aware stop instead of the up-front orphan collection, which only waits for process exit. Without--wait, AppHosts stopped by that collection also trigger the background-teardown hint.docs/specs/cli-backchannel.mdandsrc/Aspire.Cli/README.mdare updated.Starting an instance that is still being torn down is unchanged and out of scope here.
Validation
dotnet test --project tests/Aspire.Cli.Tests/Aspire.Cli.Tests.csproj --no-launch-profile -- --filter-class "*.StopCommandTests" --filter-not-trait "quarantined=true" --filter-not-trait "outerloop=true": passed (81/81), including the new--waittests (success, default, custom and maximum--timeoutexpiry, invalid and out-of-range--timeoutvalues, unsupported, unconfirmed,--force --wait, and the one-time hint without--wait).dotnet test --project tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj --no-launch-profile -- --filter-class "*.DcpExecutorTests" --filter-class "*.AuxiliaryBackchannelTests" --filter-class "*.AuxiliaryBackchannelRpcTargetTests" --filter-class "*.ApplicationOrchestratorTests" --filter-not-trait "quarantined=true" --filter-not-trait "outerloop=true": 512 passed, 1 failed. The failure,CanConnectMultipleClientsToAuxiliaryBackchannel, also fails on unmodifiedmainon this machine (an extra socket client), so it is environmental. New tests cover the executor cleanup wait and the end-to-endStopAsyncRPC with real DCP.StopWaitTests.StopWaitRemovesFixedNameContainerBeforeReturning: starts a fixed-name container, runsstop --wait, checks once (no polling) that the container is gone, then restarts and verifies the container stays up. It passes in CI.Aspire.Cli.Testsrun (same filters): 10595 passed, 8 failed. The failures are inNuGetPackageOperationConfigurationTests,PrebuiltAppHostServerTestsandDotNetCliRunnerTests.CandidateRestore_*(NuGet/restore), which this change doesn't touch. I didn't rerun them onmain.WithContainerNameRedis containers on macOS + Docker:--wait: reproduced the bug (both containers stillUpafter "stopped successfully").--wait: returned after about 4 s, with both containers gone.stop --waitfollowed immediately bystart: the new containers stayed Running/Healthy after 45 s.--pid --wait: containers removed, and the AppHost and launcher processes exited.Fixes #22510
Part of #22511
Checklist
<remarks />and<code />elements on your triple slash comments?