Skip to content

Kill each program's whole tree through a Job Object of its own - #77

Merged
HalRiveria-Bot merged 5 commits into
mainfrom
fix/tree-job
Nov 19, 2026
Merged

HalRiveria-Bot merged 5 commits into
mainfrom
fix/tree-job

Conversation

@HalRiveria-Bot

Copy link
Copy Markdown
Collaborator

What was wrong

A time-limit kill, an abort's force-stop and Stop-PkProcessTree used taskkill /T, which finds a tree by parent PIDs. A program's descendant whose parent had already exited (a start /b helper, or anything an exited intermediate started) has nothing linking it back, so it ran on until PowerShell exited.

Reproduced on 5.1 and 7.6 with a program that runs an inner cmd that starts a ping and exits, then pings itself:

  • Stop-PkProcessTree killed the program and its own ping and left the orphan running.
  • A batch command with a 2 s -TimeoutSeconds took 17.3 s, because the surviving orphan held its stdout for the full 15 s stream wait after the kill, and the orphan was still running afterwards.

What changed

  • Each program the kit starts gets a Job Object of its own (a tree job), nested in the kit's.
    • TryStart lists it after the kit's job in PROC_THREAD_ATTRIBUTE_JOB_LIST, so the program is in it from its first instruction.
    • The Process.Start fallback assigns it right after the kit's job.
    • It's kept in a ConditionalWeakTable keyed by the Process, as a SafeHandle.
  • Stop-PkProcessTreeCore ends a program that has a tree job with TerminateJobObject: the program and everything in its tree, orphans included, at once. Only programs without one go to taskkill. That's programs the kit didn't start, StartMode NoJob, or one Windows wouldn't end that way.
  • The tree job's handle is released once a kill has ended the tree, or when Complete-PkProcess runs. Failing both, it goes when the Process is collected.
  • If Windows refuses the nested job, TryStart retries the start once without it. The program still starts, and a kill goes to taskkill. A program that can't start at all fails again with its own error.
  • Unchanged: a program that exits on its own leaves what it started running, in the kit's job until PowerShell exits.
  • C#: TypesVersion 11. [PkState]::ThreadPoolRaised, unused since Leave the thread pool's minimum alone #76, is removed. New test-only hooks: TestNoTreeJob, TestRefuseTreeJob, TestHasTree.
  • Help text and README updated: Stop-PkProcessTree and the FAQ answer on stopping a program.

How it was tested

  • New harness section BF, 7 assertions, on 5.1 and 7:

    • Stop-PkProcessTree kills the orphan and releases the tree job, on both start paths.
    • A batch time-limit kill takes the orphan, and the job ends at 3.1 s.
    • An abort's force-stop takes the orphan.
    • A start where the tree job is refused runs without one, and taskkill still kills its tree.
    • A child that denies PROCESS_TERMINATE is killed with the tree job.
    • A leftover from a program that exits on its own keeps running, and its tree job is released.
  • The existing taskkill-path tests now start their programs with TestNoTreeJob, so they still reach taskkill. Those are the 40-tree kill, AJ's protected child, AV's stand-in taskkill and the one-PID-per-call test. Without the hook, the argument-splitting test failed and the hang test passed without reaching taskkill.

  • The same check script against the old code and this branch, on both runtimes:

    Old code This branch
    Stop-PkProcessTree orphan left running orphan killed in 21-26 ms
    Time-limited batch command 17.3 s, orphan left 2.2 s, nothing left

    300 quick programs took the same time on both, with no handle growth after a GC.

  • The full harness passed on 5.1 and then 7: 601 assertions each.

  • Not covered: a program that has a tree job where TerminateJobObject itself fails, which falls back to taskkill. I found no way to make Windows refuse that from a test.

🤖 Generated with Claude Code

HalRiveria and others added 5 commits November 19, 2026 02:15
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…once a kill ends the tree

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@HalRiveria-Bot
HalRiveria-Bot merged commit bd92fcb into main Nov 19, 2026
2 checks passed
@HalRiveria-Bot
HalRiveria-Bot deleted the fix/tree-job branch November 19, 2026 08:15
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