Skip to content

Start each program inside the Job Object, so a child it starts at once can't escape - #56

Merged
HalRiveria merged 1 commit into
mainfrom
feat/job-at-creation
Nov 17, 2026
Merged

HalRiveria merged 1 commit into
mainfrom
feat/job-at-creation

Conversation

@HalRiveria-Bot

Copy link
Copy Markdown
Collaborator

What was wrong: Start-PkProcess used Process.Start and 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 600 cmd /c ping children 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:

  • The program is 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. Supported on Windows 10 / Server 2016 and later, the kit's existing floor.
  • Command line, program search (bare names), hidden window, stdin, redirected stdout/stderr and the default encoding match Process.Start with UseShellExecute = false. A handle list keeps the child from inheriting unrelated handles.
  • The pipe ends the child writes to must be inheritable while it's created, and a plain Process.Start elsewhere 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 a System.Threading.Lock if a newer .NET switches to that). Without a lock it recognizes, it falls back to Process.Start plus assigning the job afterward, exactly 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 (the kit's own code uses none of them; read output from StdoutTask/StderrTask). Noted in Start-PkProcess help.
  • A start error now gives Windows' reason without PowerShell's Exception calling "Start" with "1" argument(s): wrapper.
  • Compiled types bumped to version 5. README: the gap is gone from Limits and the force-kill answer.

Tested:

  • Harness passes on Windows PowerShell 5.1 and PowerShell 7.6 (476 assertions each). New section AM: .NET's start lock is found on the runtime, 40 of 40 cmd /c ping grandchildren are in the job, instant exits keep their exit code, Proc works as a Process, the start error is unwrapped, output isn't held open by a plain Process.Start running alongside (0 of 40 late), and the fallback still starts and assigns the job. The lock and error-message checks fail on main. The race itself can't be forced, so the grandchild check shows the mechanism rather than failing before.
  • Live: a host script started 20 cmd /c ping jobs and was killed with taskkill /F on the host only, 15 rounds per runtime: 0 of 300 pings survived on 5.1 and 0 of 300 on 7.6. (On main the same run also left none; the race is too rare to show in 300.)
  • Before the change, a standalone version of the same code was also checked for: 20,000 lines of output without a stall, encodings matching the kit, program paths with spaces, a missing program giving Win32Exception 2, stable handle counts over 100 starts and 100 failures, and stdin behaving as before.

🤖 Generated with Claude Code

…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>
@HalRiveria
HalRiveria merged commit 6fff379 into main Nov 17, 2026
2 checks passed
@HalRiveria
HalRiveria deleted the feat/job-at-creation branch November 17, 2026 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants