Repository navigation
Make IInteractionService unavailable during BeforeStart to prevent deadlock - #21553
Conversation
Interactions are answered by the dashboard and CLI backchannel, which are hosted services that only start after BeforeStartEvent, lifecycle hooks and the before-start pipeline step complete. IsAvailable returned true during that window, so awaiting a prompt guarded by IsAvailable deadlocked the app host. IsAvailable now returns false while before-start hooks run, and prompts throw early instead of waiting forever. Fixes #11371 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 21553Or
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 21553" |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the change intended to prevent before-start interaction prompts from blocking AppHost startup.
No open issues 🎉
What changed in this PR
Prevents the AppHost startup deadlock described in #11371 by making IInteractionService unavailable while before-start hooks run. Guarded prompts can skip interaction, while unguarded prompts fail immediately instead of blocking startup.
Changes:
- Adds a reference-counted, idempotent scope that disables interactions during before-start execution and restores availability afterward.
- Updates availability documentation and the exception message.
- Adds regression tests and a guarded-prompt example in the Stress playground.
| File | Description |
|---|---|
tests/Aspire.Hosting.Tests/InteractionServiceTests.cs |
Tests prompt rejection, availability restoration, double disposal, and before-start integration. |
src/Aspire.Hosting/InteractionService.cs |
Implements the before-start scope and updates the unavailable-service error. |
src/Aspire.Hosting/IInteractionService.cs |
Documents interaction unavailability during before-start hooks. |
src/Aspire.Hosting/DistributedApplication.cs |
Holds the interaction scope throughout before-start execution. |
playground/Stress/Stress.AppHost/AppHost.cs |
Demonstrates guarding a before-start prompt with IsAvailable. |
Review effort: Balanced
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
|
CI failures on attempt 1 are known flakes unrelated to this change. The repo's transient-failure automation reran the failed jobs once (attempt 2):
🤖 Posted by AspireAgent (AI Agent) |
|
CI on head
Its one rerun is used up, so I'm leaving this PR as a draft. A maintainer can decide whether to rerun these flakes or merge with them failing. 🤖 Posted by AspireAgent (AI Agent) |
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
|
CI on head The repo's retry automation reran only the failed jobs once more (attempt 3 of the CI run). Both jobs passed:
Neither flake touches 🤖 Posted by AspireAgent (AI Agent) |
James Newton-King (JamesNK)
left a comment
There was a problem hiding this comment.
One design question about interaction-service startup readiness.
Reshape the before-start guard into a one-way flag per review feedback: DistributedApplication marks the interaction service as pending when it's created and completes it once ExecuteBeforeStartHooksAsync finishes. This also covers code that prompts after Build() but before Run() reaches the before-start hooks. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This comment has been minimized.
This comment has been minimized.
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
Address review feedback: InteractionService now starts not ready by default and the app host calls IInteractionService.MarkConsumersReady() (internal default interface member) after before-start hooks complete, instead of casting to InteractionService and marking hooks pending in the constructor. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This comment has been minimized.
This comment has been minimized.
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
Address review feedback: undo the not-ready-by-default change from ac04b8a and replace the pending/complete pair with a single internal IInteractionService.MarkConsumersReady(bool ready). DistributedApplication marks consumers not ready when created and ready after before-start hooks, without casting to InteractionService. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This comment has been minimized.
This comment has been minimized.
|
CI note for head 594ef29: the Native hosting validation job ("Native AOT, generated SDK, Docker, DCP, CLI and Dashboard") was cancelled after reaching its 35-minute limit. It got stuck in I think this is unrelated to this PR:
I've rerun the failed job once. Hosting.MongoDB (ubuntu) also failed, which is known flake #20478. I'm leaving that one to the retry automation. 🤖 Posted by AspireAgent (AI Agent) |
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
🟡 Changes recommended
A testing wrapper can reset readiness after before-start hooks have completed without rerunning them, leaving dashboard-enabled testing apps permanently non-interactive; initialization must be limited to the original build and covered by a regression test.
1 open issue
Review effort: Balanced
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Tests selector52 / 100 PR test projects · 4 PR jobs, from 15 changed files. Selected PR test projects (52 / 100)
Selected PR jobs (4)
How these were chosen — grouped by what changed
🔧 show 45
🧪 📦 affected project 📦 affected project 📄 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 Job reasons
Selection computed for commit |
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
|
Pull request created: #2022
|
|
📝 Documentation has been drafted in microsoft/aspire.dev#2022 targeting No deterministic signals fired (docs_optional), but this is a user-visible behavior change to a documented feature: Note This draft PR needs human review before merging. |

Description
Fixes #11371
Using
IInteractionServicefrom aBeforeStartEventsubscriber deadlocked the app host, even when the prompt was guarded byinteractionService.IsAvailable.Root cause
DistributedApplication.ExecuteBeforeStartHooksAsync(BeforeStartEventsubscribers,IDistributedApplicationLifecycleHook.BeforeStartAsync, and thebefore-startpipeline step) runs before_host.StartAsync. Interactions are answered only by the dashboard (started by the orchestrator hosted service) and the CLI backchannel (BackchannelService), and both are hosted services. So during before-start nothing can respond to an interaction. However,InteractionService.IsAvailableonly checkedDisableDashboard, the non-interactive CLI scope, and the interactivity config, so it returnedtrue. An awaited prompt was added to the interaction collection and waited on aTaskCompletionSourcethat could never complete, which blocked host startup.Fix
IInteractionServicegets aninternalMarkConsumersReady(bool ready)member with a default no-op body, so the public API is unchanged.DistributedApplication's constructor callsMarkConsumersReady(false), and from then onIsAvailablereturnsfalse. This also covers code that prompts afterBuild()but beforeRun()reaches the hooks. A standaloneInteractionServicethat isn't hosted by aDistributedApplicationis unaffected.ExecuteBeforeStartHooksAsynccallsMarkConsumersReady(true)in afinallyblock after events, lifecycle hooks and before-start pipeline steps have run. This happens before_host.StartAsync, so hosted services (DCP host, parameter processing, pipeline executor, version check) see the same availability as before. There's no cast toInteractionService. The design was reshaped (ref-counted scope → one-way flag →MarkConsumersReady(bool)) after review feedback from JamesNK.EnsureServiceAvailable(), unguarded prompts during before-start now throwInvalidOperationExceptionright away instead of hanging. The exception message now mentions this case. TheIsAvailableXML docs gain a<remarks>explaining it.I used an instance-level flag instead of the existing
AsyncLocalnon-interactive scope on purpose. AnAsyncLocalwould flow into background tasks started from before-start handlers and keep them non-interactive permanently, even after the dashboard is up. With the flag, availability goes back totrueas soon as before-start completes.Before (main): real apphost,
BeforeStartEventprompt guarded byIsAvailableThe process was killed by
timeout 60, with exit code 124. The host never started, no dashboard came up, and no resources were created:After (this PR): same apphost
curl http://localhost:15999/returned302, so the dashboard was serving. Both runs used a scratch apphost on Linux, built from this repo (main vs. this branch), with one executable resource and the exact handler from the issue. The scratch apphost is not committed.Tests
New tests in
tests/Aspire.Hosting.Tests/InteractionServiceTests.cs:BeforeStartEvent_PromptIsGuardedByIsAvailable_DoesNotDeadlock: builds a real app with the dashboard enabled, subscribes toBeforeStartEvent, recordsIsAvailableand awaitsPromptMessageBoxAsync, checks thatIsAvailableisfalseand prompts fail fast right afterBuild(), then runsExecuteBeforeStartHooksAsync. It asserts thatIsAvailablewasfalseduring the event, that the prompt threwInvalidOperationExceptioninstead of hanging, and thatIsAvailableistrueagain afterwards.DistributedApplicationwiring reverted, it fails:System.TimeoutException : The operation at InteractionServiceTests.cs:238 timed out after reaching the limit of 5000ms.IsAvailable_ConsumersNotReady_ReturnsFalseAndPromptsThrow: unit coverage of the flag. It checks thatIsAvailableisfalseafterMarkConsumersReady(false), that the prompt throws early with a message namingBeforeStartEvent, that no interaction is queued, and that availability is restored afterMarkConsumersReady(true).Local run with the fix, covering
InteractionServiceTests,ParameterProcessorTests,DistributedApplicationPipelineTests,PipelineExecutorTests,ResourceCommandServiceTests,VersionCheckServiceTests,ComputeEnvironmentValidationTests,ContainerRuntimeReadinessTestsandRequiredCommandAnnotationTests: 374 of 375 passed. The one failure,ParameterProcessorTests.DeleteParameterCoreAsync_AddsParameterBackToUnresolvedAndStartsResolutionTask, is a pre-existing intermittent failure. It uses aTestInteractionServiceand doesn't touch the changed code. When rerun alone it failed 1 of 5 times.InteractionServiceTestspassed 80 of 80.Playground
playground/Stress/Stress.AppHostalready hosts the interaction samples. It now has aBeforeStartEventsubscriber that uses the issue'sIsAvailableguard and prompt pattern. With the fix, the Stress apphost gets past before-start into DCP orchestration. In my sandbox, the Stress app later failed on an unrelated, environment-specific coordinated build of its child .NET projects, so I used the scratch apphost above for the clean E2E evidence.Checklist
🤖 Posted by AspireAgent (AI Agent)