Implement StartDetached for SafeChildProcessHandle#215
Conversation
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
adamsitnik
left a comment
There was a problem hiding this comment.
@copilot please address my feedback
| #ifdef POSIX_SPAWN_SETSID | ||
| flags |= POSIX_SPAWN_SETSID; | ||
| #else | ||
| // POSIX_SPAWN_SETSID is not available on this platform |
There was a problem hiding this comment.
handle this case at the beginning of the method, so we don't need to close anything. Just like we do for HAVE_POSIX_SPAWN_START_SUSPENDED here:
ProcessPlayground/Library/native/pal_process.c
Lines 170 to 176 in 59e6d6f
There was a problem hiding this comment.
Done in 6f6d450. Moved the POSIX_SPAWN_SETSID check to the beginning of the function alongside POSIX_SPAWN_START_SUSPENDED.
| throw new InvalidOperationException("A detached process cannot inherit handles."); | ||
| } | ||
|
|
||
| return StartDetachedCore(options); |
There was a problem hiding this comment.
Don't introduce a new helper method, just extend StartCore with new boolean flag just like we did for StartSuspended.
There was a problem hiding this comment.
Done in 6f6d450. Removed StartDetachedCore methods and extended StartCore with a detached boolean parameter, following the same pattern as createSuspended.
| ProcessStartOptions options; | ||
| if (OperatingSystem.IsWindows()) | ||
| { | ||
| options = new("cmd.exe") { Arguments = { "/c", "exit 0" } }; | ||
| } | ||
| else | ||
| { | ||
| options = new("sh") { Arguments = { "-c", "exit 0" } }; | ||
| } |
There was a problem hiding this comment.
Use following pattern for this and other new tests:
| ProcessStartOptions options; | |
| if (OperatingSystem.IsWindows()) | |
| { | |
| options = new("cmd.exe") { Arguments = { "/c", "exit 0" } }; | |
| } | |
| else | |
| { | |
| options = new("sh") { Arguments = { "-c", "exit 0" } }; | |
| } | |
| ProcessStartOptions options = OperatingSystem.IsWindows() | |
| ? new("cmd.exe") { Arguments = { "/c", "exit 0" } } | |
| : new("sh") { Arguments = { "-c", "exit 0" } }; |
There was a problem hiding this comment.
Done in 6f6d450. Updated test to use the suggested ternary operator pattern.
…hods Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
adamsitnik
left a comment
There was a problem hiding this comment.
@copilot It's much better, but still plenty of space for improvement. Address my feedback
| PrepareHandleAllowList(options, handlesToInherit, ref handleCount, inputPtr, outputPtr, errorPtr); | ||
| // For detached processes, add stdio handles directly | ||
| // For normal processes, use PrepareHandleAllowList which also adds user handles | ||
| if (detached) |
There was a problem hiding this comment.
We don't need that logic here as the caller ensures that options.InheritedHandles.Count == 0
There was a problem hiding this comment.
Done in 434f27e. Removed the unnecessary conditional logic since StartDetached ensures InheritedHandles.Count == 0.
| ProcessStartOptions options; | ||
|
|
||
| if (OperatingSystem.IsWindows()) | ||
| { | ||
| options = new("powershell") { Arguments = { "-Command", "Start-Sleep 1" } }; | ||
| } | ||
| else | ||
| { | ||
| options = new("sleep") { Arguments = { "1" } }; | ||
| } |
There was a problem hiding this comment.
Use the ProcessStartOptions options = OperatingSystem.IsWindows() ? new(X) : new(Y) pattern for all the tests.
| ProcessStartOptions options; | |
| if (OperatingSystem.IsWindows()) | |
| { | |
| options = new("powershell") { Arguments = { "-Command", "Start-Sleep 1" } }; | |
| } | |
| else | |
| { | |
| options = new("sleep") { Arguments = { "1" } }; | |
| } | |
| ProcessStartOptions options = OperatingSystem.IsWindows() | |
| ? new("powershell") { Arguments = { "-Command", "Start-Sleep 1" } } | |
| : new("sleep") { Arguments = { "1" } }; |
There was a problem hiding this comment.
Done in 434f27e. Converted all remaining tests to use the ternary operator pattern.
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Implementation Complete ✅
Successfully implemented StartDetached for SafeChildProcessHandle with all PR feedback addressed:
Latest Changes (addressing new feedback)
Implementation Details
StartDetached(ProcessStartOptions options)APIDETACHED_PROCESS | CREATE_NEW_PROCESS_GROUPflagsPOSIX_SPAWN_SETSIDflag (checked at start)setsid()after fork, before execCode Changes
Verification
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.