Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Aspire.Cli/Processes/DetachedProcessLauncher.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ namespace Aspire.Cli.Processes;
internal static partial class DetachedProcessLauncher
{
/// <summary>
/// Windows implementation using CreateProcess with STARTUPINFOEX and
/// PROC_THREAD_ATTRIBUTE_HANDLE_LIST to prevent handle inheritance to grandchildren.
/// Windows implementation using CreateProcess with DETACHED_PROCESS,
/// STARTUPINFOEX, and PROC_THREAD_ATTRIBUTE_HANDLE_LIST to detach from
/// the launching console and prevent handle inheritance to grandchildren.
/// </summary>
[SupportedOSPlatform("windows")]
private static Process StartWindows(string fileName, IReadOnlyList<string> arguments, string workingDirectory, Func<string, bool>? shouldRemoveEnvironmentVariable, IReadOnlyDictionary<string, string>? additionalEnvironmentVariables)
Expand Down Expand Up @@ -87,7 +88,7 @@ private static Process StartWindows(string fileName, IReadOnlyList<string> argum
// Build the command line string: "fileName" arg1 arg2 ...
var commandLine = BuildCommandLine(fileName, arguments);

var flags = CreateUnicodeEnvironment | ExtendedStartupInfoPresent | CreateNewProcessGroup;
var flags = WindowsDetachedProcessCreationFlags;

// Build a custom environment block if variables need to be removed or added.
// CreateProcessW with lpEnvironment=nint.Zero inherits the parent's
Expand Down Expand Up @@ -307,7 +308,10 @@ private static nint BuildCustomEnvironmentBlock(Func<string, bool>? shouldRemove
private const uint StartfUseShowWindow = 0x00000001;
private const uint CreateUnicodeEnvironment = 0x00000400;
private const uint ExtendedStartupInfoPresent = 0x00080000;
private const uint DetachedProcess = 0x00000008;
private const uint CreateNewProcessGroup = 0x00000200;
internal const uint WindowsDetachedProcessCreationFlags =
CreateUnicodeEnvironment | ExtendedStartupInfoPresent | CreateNewProcessGroup | DetachedProcess;
private const ushort ShowWindowHide = 0x0000;
private static readonly nint s_procThreadAttributeHandleList = (nint)0x00020002;

Expand Down
13 changes: 8 additions & 5 deletions src/Aspire.Cli/Processes/DetachedProcessLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ namespace Aspire.Cli.Processes;
// The solution is platform-specific:
//
// ┌─────────┬────────────────────────────────────────────────────────────────┐
// │ Windows │ P/Invoke CreateProcess with STARTUPINFOEX and an explicit │
// │ │ PROC_THREAD_ATTRIBUTE_HANDLE_LIST. This lets us set │
// │ Windows │ P/Invoke CreateProcess with DETACHED_PROCESS, │
// │ │ STARTUPINFOEX, and an explicit │
// │ │ PROC_THREAD_ATTRIBUTE_HANDLE_LIST. This detaches the child │
// │ │ from the launching console while still letting us set │
// │ │ bInheritHandles=TRUE (required to assign hStdOutput to NUL) │
// │ │ while restricting inheritance to ONLY the NUL handle — so the │
// │ │ and restrict inheritance to ONLY the NUL handle — so the │
// │ │ grandchild inherits nothing useful. Child stdout/stderr go to │
// │ │ the NUL device. This is the same approach used by Docker's │
// │ │ Windows container runtime (microsoft/hcsshim). │
// │ │ the NUL device. The detached flag combination matches │
// │ │ established Windows daemonization patterns used by tools such │
// │ │ as libuv/Node.js and GitHub CLI. │
// │ │ │
// │ Linux / │ Process.Start with RedirectStandard{Output,Error} = true, │
// │ macOS │ then immediately close the parent's read-end pipe streams. │
Expand Down
19 changes: 19 additions & 0 deletions tests/Aspire.Cli.Tests/Processes/DetachedProcessLauncherTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Cli.Processes;

namespace Aspire.Cli.Tests.Processes;

public class DetachedProcessLauncherTests
{
[Fact]
public void WindowsCreationFlags_DetachProcessFromLaunchingConsole()
{
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

Assert.NotEqual(0u, DetachedProcessLauncher.WindowsDetachedProcessCreationFlags & createNewProcessGroup);
}
}
Loading