Skip to content

Fix Windows detached AppHost launcher - #16561

Merged
David Fowler (davidfowl) merged 3 commits into
mainfrom
fix/windows-detached-apphost-start
Apr 29, 2026
Merged

Fix Windows detached AppHost launcher#16561
David Fowler (davidfowl) merged 3 commits into
mainfrom
fix/windows-detached-apphost-start

Conversation

@davidfowl

@davidfowl David Fowler (davidfowl) commented Apr 29, 2026

Copy link
Copy Markdown
Collaborator

Description

Fixes #16559

aspire start and aspire run --detach on Windows already launch the detached child CLI with CREATE_NEW_PROCESS_GROUP and restricted handle inheritance, but they did not set DETACHED_PROCESS. That can leave the child attached to the launching console, so the AppHost can be terminated when the launcher shell exits.

This change adds the Windows DETACHED_PROCESS creation flag while preserving the existing CREATE_NEW_PROCESS_GROUP, Unicode environment, extended startup info, and NUL stdout/stderr handle behavior.

Validation:

.\restore.cmd
dotnet build src\Aspire.Cli\Aspire.Cli.csproj /p:SkipNativeBuild=true

# Manual local repro:
# - Old flags: CREATE_NEW_PROCESS_GROUP only -> AppHost started, then died after CTRL_BREAK_EVENT to the child process group.
# - Fixed flags: DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP -> AppHost started and stayed alive after the same signal.
# - Actual locally built aspire start path also left the AppHost and detached child CLI alive after the launcher command returned.

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
  • Does the change require an update in our Aspire docs?

Use the Windows DETACHED_PROCESS creation flag when launching the detached child CLI process so aspire start is not tied to the launching console lifetime. Keep the existing new process group and restricted handle inheritance behavior.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions

github-actions Bot commented Apr 29, 2026

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 -- 16561

Or

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

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

Fixes a Windows-specific detach bug in the Aspire CLI where the background (“detached”) AppHost could still be tied to the launching console and get terminated when the launching shell exits.

Changes:

  • Adds the Windows DETACHED_PROCESS creation flag to the CreateProcessW launch flags used for detached execution.
  • Centralizes the Windows detached creation flags into a single constant to preserve existing flag behavior while adding detach.
  • Adds a regression unit test to ensure the Windows detached creation flags include both DETACHED_PROCESS and CREATE_NEW_PROCESS_GROUP.
Show a summary per file
File Description
tests/Aspire.Cli.Tests/Processes/DetachedProcessLauncherTests.cs Adds a regression test verifying the Windows detached process creation flags include the expected bits.
src/Aspire.Cli/Processes/DetachedProcessLauncher.Windows.cs Adds DETACHED_PROCESS to the CreateProcess creation flags (via a shared constant) to ensure true console detachment on Windows.
src/Aspire.Cli/Processes/DetachedProcessLauncher.cs Updates the high-level design comment to reflect the Windows detachment approach using DETACHED_PROCESS.

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 0

@eerhardt Eric Erhardt (eerhardt) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems fine - if it works.

const uint detachedProcess = 0x00000008;
const uint createNewProcessGroup = 0x00000200;

Assert.NotEqual(0u, DetachedProcessLauncher.WindowsDetachedProcessCreationFlags & detachedProcess);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This test is a little jank.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The test just isnt required

Document that the detached Windows flag combination follows established daemonization patterns used by libuv/Node.js and GitHub CLI, without over-claiming Docker parity.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@danegsta

Copy link
Copy Markdown
Member

Only concern would be that DETACHED_PROCESS makes it so we can't send Ctrl+C/Ctrl+Break to the detached app host if we wanted to as it wouldn't have any allocated console group. CREATE_NEW_CONSOLE | CREATE_NO_WINDOW would be another option to ensure the child process isn't closed when the shell closes, while still allowing for sending signals via GenerateConsoleCtrlEvent. But that's only a concern if we think we'd have cause to send interrupt signals in some situations.

@davidfowl

Copy link
Copy Markdown
Collaborator Author

Only concern would be that DETACHED_PROCESS makes it so we can't send Ctrl+C/Ctrl+Break to the detached app host if we wanted to as it wouldn't have any allocated console group. CREATE_NEW_CONSOLE | CREATE_NO_WINDOW would be another option to ensure the child process isn't closed when the shell closes, while still allowing for sending signals via GenerateConsoleCtrlEvent. But that's only a concern if we think we'd have cause to send interrupt signals in some situations.

aspire stop does want to send ctrl+c. Though it does fallback to process kill now I believe.

@danegsta

Copy link
Copy Markdown
Member

Only concern would be that DETACHED_PROCESS makes it so we can't send Ctrl+C/Ctrl+Break to the detached app host if we wanted to as it wouldn't have any allocated console group. CREATE_NEW_CONSOLE | CREATE_NO_WINDOW would be another option to ensure the child process isn't closed when the shell closes, while still allowing for sending signals via GenerateConsoleCtrlEvent. But that's only a concern if we think we'd have cause to send interrupt signals in some situations.

aspire stop does want to send ctrl+c. Though it does fallback to process kill now I believe.

Just checked the code; it looks like we DO try to send a signal, but we do it in a way that probably won't actually work (trying to send CTRL+BREAK to a process when we don't necessarily share the same console group). Long term if we want to try for graceful shutdown, we should do CREATE_NEW_CONSOLE | CREATE_NO_WINDOW and update our GenerateConsoleCtrlEvent behavior to use a child process to join the app host's console group before sending the signal.

Remove the unit test coverage for the Windows detached process creation flags while keeping the implementation change and manual repro validation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@davidfowl
David Fowler (davidfowl) enabled auto-merge (squash) April 29, 2026 18:39
@danegsta

Copy link
Copy Markdown
Member

I added a followup issue to get aspire stop able to gracefully terminate detached app hosts on Windows again: #16568

@github-actions

Copy link
Copy Markdown
Contributor

Re-running the failed jobs in the CI workflow for this pull request because 1 job was identified as retry-safe transient failures in the CI run attempt.
GitHub was asked to rerun all failed jobs for that attempt, and the rerun is being tracked in the rerun attempt.
The job links below point to the failed attempt jobs that matched the retry-safe transient failure rules.

@davidfowl
David Fowler (davidfowl) merged commit f029cd0 into main Apr 29, 2026
562 of 567 checks passed
@microsoft-github-policy-service microsoft-github-policy-service Bot added this to the 13.4 milestone Apr 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🎬 CLI E2E Test Recordings — 76 recordings uploaded (commit 257c970)

View all recordings
Status Test Recording
AddPackageInteractiveWhileAppHostRunningDetached ▶️ View Recording
AddPackageWhileAppHostRunningDetached ▶️ View Recording
AgentCommands_AllHelpOutputs_AreCorrect ▶️ View Recording
AgentInitCommand_DefaultSelection_InstallsSkillOnly ▶️ View Recording
AgentInitCommand_MigratesDeprecatedConfig ▶️ View Recording
AspireAddPackageVersionToDirectoryPackagesProps ▶️ View Recording
AspireUpdateRemovesAppHostPackageVersionFromDirectoryPackagesProps ▶️ View Recording
Banner_DisplayedOnFirstRun ▶️ View Recording
Banner_DisplayedWithExplicitFlag ▶️ View Recording
Banner_NotDisplayedWithNoLogoFlag ▶️ View Recording
CertificatesClean_RemovesCertificates ▶️ View Recording
CertificatesTrust_WithNoCert_CreatesAndTrustsCertificate ▶️ View Recording
CertificatesTrust_WithUntrustedCert_TrustsCertificate ▶️ View Recording
ConfigSetGet_CreatesNestedJsonFormat ▶️ View Recording
CreateAndRunAspireStarterProject ▶️ View Recording
CreateAndRunAspireStarterProjectWithBundle ▶️ View Recording
CreateAndRunEmptyAppHostProject ▶️ View Recording
CreateAndRunJavaEmptyAppHostProject ▶️ View Recording
CreateAndRunJsReactProject ▶️ View Recording
CreateAndRunPythonReactProject ▶️ View Recording
CreateAndRunTypeScriptEmptyAppHostProject ▶️ View Recording
CreateAndRunTypeScriptStarterProject ▶️ View Recording
CreateJavaAppHostWithViteApp ▶️ View Recording
CreateTypeScriptAppHostWithViteApp_UsesConfiguredToolchain ▶️ View Recording
DashboardRunWithOtelTracesReturnsNoTraces ▶️ View Recording
DeployK8sBasicApiService ▶️ View Recording
DeployK8sWithGarnet ▶️ View Recording
DeployK8sWithMongoDB ▶️ View Recording
DeployK8sWithMySql ▶️ View Recording
DeployK8sWithPostgres ▶️ View Recording
DeployK8sWithRabbitMQ ▶️ View Recording
DeployK8sWithRedis ▶️ View Recording
DeployK8sWithSqlServer ▶️ View Recording
DeployK8sWithValkey ▶️ View Recording
DeployTypeScriptAppToKubernetes ▶️ View Recording
DescribeCommandResolvesReplicaNames ▶️ View Recording
DescribeCommandShowsRunningResources ▶️ View Recording
DetachFormatJsonProducesValidJson ▶️ View Recording
DetachFormatJsonProducesValidJsonWhenRestartingExistingInstance ▶️ View Recording
DoListStepsShowsPipelineSteps ▶️ View Recording
DocsCommand_RendersInteractiveMarkdownFromLocalSource ▶️ View Recording
DoctorCommand_DetectsDeprecatedAgentConfig ▶️ View Recording
DoctorCommand_TypeScriptAppHostReportsMissingConfiguredToolchain ▶️ View Recording
DoctorCommand_WithSslCertDir_ShowsTrusted ▶️ View Recording
DoctorCommand_WithoutSslCertDir_ShowsPartiallyTrusted ▶️ View Recording
GlobalMigration_HandlesCommentsAndTrailingCommas ▶️ View Recording
GlobalMigration_HandlesMalformedLegacyJson ▶️ View Recording
GlobalMigration_PreservesAllValueTypes ▶️ View Recording
GlobalMigration_SkipsWhenNewConfigExists ▶️ View Recording
GlobalSettings_MigratedFromLegacyFormat ▶️ View Recording
InitTypeScriptAppHost_AugmentsExistingViteRepoAtRoot ▶️ View Recording
InteractiveCSharpInitCreatesExpectedFiles ▶️ View Recording
InvalidAppHostPathWithComments_IsHealedOnRun ▶️ View Recording
LegacySettingsMigration_AdjustsRelativeAppHostPath ▶️ View Recording
LogsCommandShowsResourceLogs ▶️ View Recording
OtelLogsReturnsStructuredLogsFromStarterAppCore ▶️ View Recording
PsCommandListsRunningAppHost ▶️ View Recording
PsFormatJsonOutputsOnlyJsonToStdout ▶️ View Recording
PublishWithConfigureEnvFileUpdatesEnvOutput ▶️ View Recording
PublishWithDockerComposeServiceCallbackSucceeds ▶️ View Recording
PublishWithoutOutputPathUsesAppHostDirectoryDefault ▶️ View Recording
RestoreGeneratesSdkFiles ▶️ View Recording
RestoreGeneratesSdkFiles_WithConfiguredToolchain ▶️ View Recording
RestoreRefreshesGeneratedSdkAfterAddingIntegration ▶️ View Recording
RestoreSupportsConfigOnlyHelperPackageAndCrossPackageTypes ▶️ View Recording
RunFromParentDirectory_UsesExistingConfigNearAppHost ▶️ View Recording
SecretCrudOnDotNetAppHost ▶️ View Recording
SecretCrudOnTypeScriptAppHost ▶️ View Recording
StagingChannel_ConfigureAndVerifySettings_ThenSwitchChannels ▶️ View Recording
StartAndWaitForTypeScriptSqlServerAppHostWithNativeAssets ▶️ View Recording
StopAllAppHostsFromAppHostDirectory ▶️ View Recording
StopAllAppHostsFromUnrelatedDirectory ▶️ View Recording
StopNonInteractiveMultipleAppHostsShowsError ▶️ View Recording
StopNonInteractiveSingleAppHost ▶️ View Recording
StopWithNoRunningAppHostExitsSuccessfully ▶️ View Recording
UnAwaitedChainsCompileWithAutoResolvePromises ▶️ View Recording

📹 Recordings uploaded automatically from CI run #25126570121

@aspire-repo-bot

Copy link
Copy Markdown
Contributor

No documentation PR is required for this change.

This is an internal bug fix that adds the DETACHED_PROCESS Windows creation flag to prevent the AppHost from being terminated when the launcher shell exits. It has no new public APIs, no configuration changes, and no user-visible behavior differences beyond the fix itself working correctly. The existing docs for aspire start and aspire run --detach do not need updates.

Generated by PR Documentation Check for issue #16561 · ● 105.3K ·

@davidfowl

Copy link
Copy Markdown
Collaborator Author

/backport to release/13.3

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/13.3 (link to workflow run)

Nell Shamrell-Harrington (nellshamrell) pushed a commit to nellshamrell/aspire that referenced this pull request May 18, 2026
* Fix Windows detached AppHost launcher

Use the Windows DETACHED_PROCESS creation flag when launching the detached child CLI process so aspire start is not tied to the launching console lifetime. Keep the existing new process group and restricted handle inheritance behavior.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Clarify Windows detach launcher comment

Document that the detached Windows flag combination follows established daemonization patterns used by libuv/Node.js and GitHub CLI, without over-claiming Docker parity.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Remove Windows detach unit test

Remove the unit test coverage for the Windows detached process creation flags while keeping the implementation change and manual repro validation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot locked and limited conversation to collaborators May 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

aspire start AppHost is killed when launching shell exits on Windows (not truly detached)

5 participants