Skip to content

Commit d6b50f5

Browse files
danegstaCopilot
andauthored
[release/13.6] Fix Windows AppHost cleanup job routing (#20514)
## Description Backport of #20494 to `release/13.6` (cherry-picked patch from `e62dceba954899a33326297b4669e226c27dcdce`). On Windows, .NET AppHosts launched through `dotnet run` or `dotnet watch` do not enter the Aspire CLI's outer kill-on-close job, allowing DCP to finish removing session-scoped resources during shutdown. Direct .NET launches and TypeScript/polyglot AppHost servers retain their job protection. Includes the associated CLI tests and extension E2E test stabilization. No new dependencies. Related: #20495 (already closed by #20494). ## 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 ## Customer Impact On Windows, stopping a CLI-launched .NET AppHost can leave session-scoped containers running instead of removing them; persistent resources should remain running. ## Testing Main PR: manual C# normal/forced shutdown and TypeScript cleanup, focused CLI tests (181 passed, 3 platform-skipped), and passing Windows edge-cases CI. Release branch: targeted CLI tests (172 passed, 3 Windows platform-skipped), CLI/extension build, extension test and E2E type-check, and changed-file lint. Full VS Code E2E UI run was not performed on the release branch. ## Risk Medium. Changes Windows process ownership for `dotnet run`/`dotnet watch`; direct .NET and TypeScript/polyglot paths retain their kill-on-close protection, and CLI-parent detection remains enabled. ## Regression? Unknown. Reproduced on a 13.6 preview build; the exact introducing version/change is not established. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 95ff2d5 commit d6b50f5

5 files changed

Lines changed: 36 additions & 5 deletions

File tree

‎extension/src/test-e2e/edgeCases.e2e.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,10 @@ builder.Build().Run();`));
217217
test('process-owner cleanup stops the owned CLI and AppHost process tree', async () => {
218218
await openAspireView();
219219
await waitForRepositoryIdle();
220-
const discovered = await waitForWorkspaceAppHost();
221-
const appHostPath = discovered.state.workspaceAppHostPath ?? getPrimaryAppHostProjectPath();
220+
await waitForWorkspaceAppHost();
221+
// The previous test removes a generated AppHost; discovery can still select it while
222+
// that deletion propagates. This test targets the permanent fixture, not the selection.
223+
const appHostPath = getPrimaryAppHostProjectPath();
222224

223225
const beforeInvocation = getCommandInvocationCount('aspire-vscode.debugAppHost');
224226
await executeE2eControlCommand({ name: 'debugAppHost', appHostPath }, { waitFor: 'started' });

‎src/Aspire.Cli/Processes/WindowsConsoleProcessJob.cs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ namespace Aspire.Cli.Processes;
2828
/// <para>
2929
/// The job is also configured with <c>JOB_OBJECT_LIMIT_BREAKAWAY_OK</c> so that DCP (and
3030
/// anything else that needs to outlive the CLI for its own cleanup) can opt out by spawning
31-
/// itself with <c>CREATE_BREAKAWAY_FROM_JOB</c>. Our job only grants permission to break
32-
/// away — the breakaway itself is the responsibility of the spawning code.
31+
/// itself with <c>CREATE_BREAKAWAY_FROM_JOB</c>, provided every nested job also permits
32+
/// breakaway. .NET AppHosts launched through <c>dotnet run</c> do not use this job because
33+
/// its process reaper creates a nested job that does not permit breakaway. Directly launched
34+
/// .NET AppHosts can use it because they have no intervening job. Our job only grants permission
35+
/// to break away — the breakaway itself is the responsibility of the spawning code.
3336
/// </para>
3437
/// <para>
3538
/// IMPORTANT: do NOT dispose this service during the normal shutdown ladder. The graceful

‎src/Aspire.Cli/Projects/DotNetAppHostProject.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,10 @@ void SignalBuildCompletion()
15521552
// package add, layout, and other short-lived invocations leave these unset so
15531553
// they continue to use the shared ladder's force-kill mode.
15541554
IsolateConsole = true,
1555-
KillOnParentExit = true,
1555+
// dotnet run nests its own non-breakaway job inside the CLI job, preventing DCP
1556+
// from escaping the CLI job to finish resource cleanup. Direct launches have no
1557+
// intervening job and opt back into the CLI job below.
1558+
KillOnParentExit = false,
15561559
GracefulShutdownSignaler = _gracefulShutdownSignaler,
15571560
ShutdownService = _shutdownService,
15581561
// The bundled AppHost run hook delegates dotnet run to aspire run. The SDK passes
@@ -1616,6 +1619,7 @@ void SignalBuildCompletion()
16161619
// caller may reuse runOptions for other invocations.
16171620
var directRunOptions = runOptions.Clone();
16181621
directRunOptions.AppHostArgumentStartIndex = directRun.AppHostArgumentStartIndex;
1622+
directRunOptions.KillOnParentExit = true;
16191623

16201624
return await _runner.RunAppHostCommandAsync(
16211625
effectiveAppHostFile,

‎tests/Aspire.Cli.Tests/Projects/AppHostServerSessionTests.cs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,21 @@ public async Task Start_DoesNotMutateCallerEnvironmentVariables()
4949
Assert.Equal(session.AuthenticationToken, project.ReceivedEnvironmentVariables[KnownConfigNames.RemoteAppHostToken]);
5050
}
5151

52+
[Fact]
53+
public async Task Start_WithIsolatedConsole_RequestsKillOnParentExitForAppHostServer()
54+
{
55+
var project = new RecordingAppHostServerProject();
56+
57+
await using var session = CreateSession(
58+
project,
59+
CancellationToken.None,
60+
isolateConsole: true);
61+
await session.StartAsync();
62+
63+
Assert.True(project.ReceivedRunControl?.IsolateConsole);
64+
Assert.True(project.ReceivedRunControl?.KillOnParentExit);
65+
}
66+
5267
[Fact]
5368
public async Task Start_PropagatesProfilingContextToServerEnvironment()
5469
{
@@ -586,6 +601,8 @@ private sealed class RecordingAppHostServerProject : IAppHostServerProject
586601

587602
public Dictionary<string, string>? ReceivedEnvironmentVariables { get; private set; }
588603

604+
public AppHostServerRunControl? ReceivedRunControl { get; private set; }
605+
589606
public IProcessExecution? StartedExecution { get; private set; }
590607

591608
public string GetInstanceIdentifier() => AppDirectoryPath;
@@ -608,6 +625,7 @@ public async Task<AppHostServerRunResult> RunAsync(
608625
ReceivedEnvironmentVariables = environmentVariables is null
609626
? null
610627
: new Dictionary<string, string>(environmentVariables);
628+
ReceivedRunControl = runControl;
611629

612630
var startInfo = new ProcessStartInfo("dotnet")
613631
{

‎tests/Aspire.Cli.Tests/Projects/DotNetAppHostProjectTests.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ public async Task RunAsync_SingleFileAppHostWithoutRunJsonPassesDevelopmentEnvir
336336
Assert.True(noBuild);
337337
Assert.True(noRestore);
338338
Assert.False(options.NoLaunchProfile);
339+
Assert.True(options.IsolateConsole);
340+
Assert.False(options.KillOnParentExit);
339341
Assert.Equal("Development", env![KnownAspNetCoreConfigNames.DotNetEnvironment]);
340342
Assert.False(env.ContainsKey(KnownAspNetCoreConfigNames.Environment));
341343
Assert.Equal("https://localhost:17193;http://localhost:15069", env[KnownAspNetCoreConfigNames.Urls]);
@@ -1063,6 +1065,8 @@ public async Task RunAsync_ProjectAppHostUsesDirectCommandLaunchAndAppliesLaunch
10631065
Assert.Equal(appHostCommand.FullName, command);
10641066
Assert.Equal(runWorkingDirectory.FullName, workingDirectory.FullName);
10651067
Assert.False(options.NoLaunchProfile);
1068+
Assert.True(options.IsolateConsole);
1069+
Assert.True(options.KillOnParentExit);
10661070
Assert.Equal(
10671071
["--from-msbuild", "two words", "--explicit", "1"],
10681072
args);

0 commit comments

Comments
 (0)