Repository navigation
Start each program inside the Job Object, so a child it starts at once can't escape - #56
Merged
Merged
Conversation
…e can't escape Start-PkProcess used Process.Start and added the program to the kill-on-close Job Object just after. A program that starts a child straight away (cmd /c tool.exe) could do so before it was added, and that child kept running if the script was then killed outright. The program is now created with CreateProcess and PROC_THREAD_ATTRIBUTE_JOB_LIST, so Windows puts it in the job before it runs, and everything it starts joins too. Command line, program search, hidden window, stdin, redirected output and default encoding match Process.Start. A handle list keeps the child from inheriting other handles, and .NET's own process-start lock (found by reflection, either a plain object or a System.Threading.Lock) keeps a plain Process.Start elsewhere from inheriting the kit's pipe ends. Without a lock it recognizes, the kit falls back to Process.Start and assigning the job afterward, as before. Proc is now a Process from GetProcessById: HasExited, ExitCode, WaitForExit, Kill and Dispose work as before; its StandardOutput, StandardError and StartInfo aren't set (use StdoutTask and StderrTask). A start error now gives Windows' reason without PowerShell's "Exception calling ..." wrapper. The README no longer lists the gap under Limits. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong:
Start-PkProcessusedProcess.Startand added the program to the kit's kill-on-close Job Object just after. A program that starts a child straight away (cmd /c tool.exe) could do so before it was added, and that child kept running if the script was then killed outright (Task Manager,Stop-Process -Force). Rare: earlier testing found 1 of 600cmd /c pingchildren left on 5.1 and none of 600 on 7. Ctrl+C, aborts and timeouts were never affected (they kill the whole tree).What changed:
CreateProcessandPROC_THREAD_ATTRIBUTE_JOB_LIST, so Windows puts it in the job before it runs and everything it starts joins too. Supported on Windows 10 / Server 2016 and later, the kit's existing floor.Process.StartwithUseShellExecute = false. A handle list keeps the child from inheriting unrelated handles.Process.Startelsewhere in PowerShell would inherit them and hold the output open. So the kit takes .NET's own process-start lock (a private field, found by reflection: a plain object on 5.1 and 7.6, or aSystem.Threading.Lockif a newer .NET switches to that). Without a lock it recognizes, it falls back toProcess.Startplus assigning the job afterward, exactly as before.Procis now aProcessfromGetProcessById:HasExited,ExitCode,WaitForExit,KillandDisposework as before; itsStandardOutput,StandardErrorandStartInfoaren't set (the kit's own code uses none of them; read output fromStdoutTask/StderrTask). Noted inStart-PkProcesshelp.Exception calling "Start" with "1" argument(s):wrapper.Tested:
cmd /c pinggrandchildren are in the job, instant exits keep their exit code,Procworks as aProcess, the start error is unwrapped, output isn't held open by a plainProcess.Startrunning alongside (0 of 40 late), and the fallback still starts and assigns the job. The lock and error-message checks fail onmain. The race itself can't be forced, so the grandchild check shows the mechanism rather than failing before.cmd /c pingjobs and was killed withtaskkill /Fon the host only, 15 rounds per runtime: 0 of 300 pings survived on 5.1 and 0 of 300 on 7.6. (Onmainthe same run also left none; the race is too rare to show in 300.)Win32Exception2, stable handle counts over 100 starts and 100 failures, and stdin behaving as before.🤖 Generated with Claude Code