Skip to content

End a program's tree through one function, and say when a failed start leaves something running - #131

Merged
HalRiveria merged 2 commits into
mainfrom
fix/one-tree-kill
Dec 3, 2026
Merged

HalRiveria merged 2 commits into
mainfrom
fix/one-tree-kill

Conversation

@HalRiveria-Bot

Copy link
Copy Markdown
Collaborator

What was wrong

Ending a program and what it started was done in several places, each with its own steps and its own reporting:

  • the time-limit kill;
  • -KillTreeOnExit;
  • Invoke-PkProcessWithTimeout's kill and cleanup;
  • the two rollbacks of a failed start: a reader that can't start, and a -JobStarter that throws after starting its program.

They had drifted apart. When Windows wouldn't end the program's own Job Object, the two rollbacks fell back to Stop-PkProcessTree, which skips a program that has exited, and said nothing. What the program had started kept running (until PowerShell exits), while the failure said the program "was stopped". Only -KillTreeOnExit warned about it.

What changed

  • One function, Stop-PkProgramTree, for every kill path for one program.

    • A running program goes to Stop-PkProcessTree: its own Job Object, else taskkill /T.
    • What a program started is ended through its own Job Object, whether the program has exited or not.
    • It reports how that went: killed, not killed, exited, state unknown, no Job Object, or Windows refused.
    • Its callers: the time-limit kill, -KillTreeOnExit (Stop-PkExitedJobTree, which keeps its once-per-session warnings), and Invoke-PkProcessWithTimeout's kill.
    • The force-stop still kills all running programs in one Stop-PkProcessTree call, so taskkill runs once.
  • The two rollbacks share Stop-PkFailedStartTree. When something may still be running, the failed start says so instead of "so it was stopped":

    • Windows wouldn't end the program's Job Object;
    • an exited program has no Job Object of its own;
    • the program couldn't be stopped;
    • its state couldn't be read.

    Example: "...its output couldn't be read (...). Its program (process N) has ended, but Windows wouldn't end its own Job Object, so what it started may still be running. That ends when PowerShell does."

  • A gap the review of this change found: a program still running at the rollback, whose Job Object Windows wouldn't end, was killed by taskkill /T. That can miss a child whose parent has already exited. It's now reported as a refusal too.

  • Invoke-PkProcessWithTimeout's cleanup uses the force-stop's steps (Stop-PkJobProcess) instead of its own copy.

No C# change.

How it was tested

Three new section AW assertions. A tagged ping is left behind by cmd /c start /b, and must be reported:

  • the reader rollback with TestFailTerminate;
  • the reader rollback with TestNoTreeJob;
  • a -JobStarter that throws after its program exits, with TestFailTerminate.

Each test kills its leftover ping by its tag.

Before (main's kit with the new harness), 5.1 and 7 alike:

  [FAIL] a failed start whose leftovers can't be ended says so (TestFailTerminate: 1 left running, error: Start-PkProcess: could not start '...\cmd.exe': its output couldn't be read (Harness: a reader thread that can't start (TestFailRunAfter).), so it was stopped.)
  [FAIL] a failed start whose leftovers can't be ended says so (TestNoTreeJob: 1 left running, error: ... so it was stopped.)
  [FAIL] dispatch: and so does a -JobStarter's (1 left running, error: ProcessDispatch: JobStarter failed to start a job: the starter failed after starting its program)
3 FAILED, 17 passed

After: the full harness passes on Windows PowerShell 5.1 and PowerShell 7 (806 assertions each). A first full run caught a regression in this change: a Process that was never started reads $null for HasExited, and was taken as exited, so the time-limit kill lost its "couldn't be stopped" warning. It's now reported as unknown, as before.

…t leaves something running

Every kill path for one program now goes through Stop-PkProgramTree: the time-limit kill, -KillTreeOnExit, Invoke-PkProcessWithTimeout's kills and a failed start's rollback. The two rollbacks (a reader that can't start, a -JobStarter that throws after starting its program) share Stop-PkFailedStartTree, and when Windows won't end the program's tree job, or an exited program has none, the start's failure now says what it started may still be running instead of that it was stopped. Invoke-PkProcessWithTimeout's cleanup uses the force-stop's steps.
@HalRiveria
HalRiveria merged commit 4fa6b60 into main Dec 3, 2026
3 checks passed
@HalRiveria
HalRiveria deleted the fix/one-tree-kill branch December 3, 2026 18:03
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