Skip to content

Fix waiting resource start and stop command behavior#18795

Draft
afscrome wants to merge 1 commit into
microsoft:mainfrom
afscrome:afscrome-fix-resource-start-commands
Draft

Fix waiting resource start and stop command behavior#18795
afscrome wants to merge 1 commit into
microsoft:mainfrom
afscrome:afscrome-fix-resource-start-commands

Conversation

@afscrome

Copy link
Copy Markdown
Collaborator

Description

This fixes two related resource lifecycle issues around dependency waits and force start behavior. In the previous behavior, a resource in Waiting could not be canceled with Stop, and after force starting once, a subsequent Waiting run could no longer be force started.

The change updates lifecycle command handling so:

  • Stop is enabled for Waiting resources and cancels the pending startup by moving the resource back to NotStarted.
  • user-triggered Start is treated as a start request that does not block command execution while dependency waits are in progress, so force start remains usable on subsequent runs.
  • startup cancellation while waiting is handled explicitly in orchestration/DCP flow without surfacing a generic failure.

Targeted tests were updated and added to cover command state behavior and waiting-resource start/stop orchestration paths.

Fixes #14812
Fixes #14813

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 15, 2026 20:50
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 18795

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 18795"

@github-actions github-actions Bot added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Jul 15, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates Aspire Hosting resource lifecycle command handling to improve behavior for resources in the Waiting state—allowing Stop to cancel a pending dependency-blocked start and ensuring user-triggered Start remains usable across subsequent runs by treating it as a non-blocking start request.

Changes:

  • Enable Stop for Waiting resources and implement cancellation by moving the resource back to NotStarted without calling DCP stop.
  • Introduce RequestStartResourceAsync to decouple user start requests from waiting/creation completion, observing failures in the background.
  • Add orchestration and command-state unit tests for waiting-resource start/stop paths.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/Aspire.Hosting.Tests/ResourceCommandAnnotationTests.cs Updates expected command state so stop is enabled while a resource is Waiting.
tests/Aspire.Hosting.Tests/Orchestrator/ApplicationOrchestratorTests.cs Adds tests for waiting-stop cancellation behavior and non-blocking start requests; allows injecting a tracking DCP executor.
src/Aspire.Hosting/Orchestrator/ApplicationOrchestrator.cs Implements non-blocking start requests, waiting-stop cancellation to NotStarted, and explicit startup-abort detection during dependency waits.
src/Aspire.Hosting/Dcp/DcpExecutor.cs Handles ResourceStartAbortedException as a non-failure (debug log only).
src/Aspire.Hosting/ApplicationModel/ResourceStartAbortedException.cs Adds an internal exception type to represent explicit startup abort while waiting.
src/Aspire.Hosting/ApplicationModel/CommandsConfigurationExtensions.cs Switches Start command to call RequestStartResourceAsync and adjusts Stop enablement behavior for Waiting.

Comment on lines +124 to +134
// If the wait ended because the state was manually moved out of Waiting to anything
// other than Starting, treat that as an explicit cancellation of this startup attempt.
// This is used by the Stop command to abort a dependency-blocked start.
if (completedTask == waitForNonWaitingStateTask && waitForNonWaitingStateTask.IsCompletedSuccessfully)
{
var state = waitForNonWaitingStateTask.Result.Snapshot.State?.Text;
if (!string.Equals(state, KnownResourceStates.Starting, StringComparisons.ResourceState))
{
throw new ResourceStartAbortedException(@event.Resource.Name, state);
}
}
Comment on lines 26 to 32
executeCommand: async context =>
{
var orchestrator = context.Services.GetRequiredService<ApplicationOrchestrator>();

await orchestrator.StartResourceAsync(context.ResourceName, context.CancellationToken).ConfigureAwait(false);
await orchestrator.RequestStartResourceAsync(context.ResourceName, context.CancellationToken).ConfigureAwait(false);
return new ExecuteCommandResult { Success = true, Message = string.Format(CultureInfo.InvariantCulture, CommandStrings.ResourceStarted, resource.GetResolvedDisplayResourceName(context.ResourceName)) };
},
@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should be able to cancel a Waiting resource's startup Can't force start a resource for a second time

2 participants