Skip to content

Kill a stopped run's programs and give back its slots - #38

Merged
HalRiveria-Bot merged 2 commits into
mainfrom
fix/stop-leaves-programs-running
Nov 16, 2026
Merged

HalRiveria-Bot merged 2 commits into
mainfrom
fix/stop-leaves-programs-running

Conversation

@HalRiveria-Bot

Copy link
Copy Markdown
Collaborator

What was wrong

When PowerShell stops a script, catch blocks are skipped and only finally blocks run. That happens on Ctrl+C outside Invoke-PkRun, on a third press inside it, and when the runspace engine force-stops a worker after a drain timeout or a -ResultHandler throw. The dispatcher's kill-and-release code was in catch blocks, and Invoke-PkProcessWithTimeout's finally only disposed. So a stopped run left its programs running hidden until the PowerShell process exited, and a shared SemaphoreSlim budget never got its slots back.

Separately, a -JobStarter that started its program and then threw left that program running untracked while its slot went back to the budget.

What changed

  • Invoke-PkProcessDispatchCore: the two catch teardowns are now one finally, guarded by a flag set when the loop and the abort wait finish normally. An error still propagates as before. The teardown also covers a job whose -JobStarter was still running when the stop hit, and it skips jobs that were already finalized and disposed.
  • The start-failure path kills a program the starter had already started before giving its slot back.
  • Invoke-PkProcessWithTimeout: its finally kills the program, and anything it started, if it's still running, then disposes it.

How it was tested

New harness section AG. Each scenario is stopped while its programs are running, and checks that none are left running and that shared slots are all free:

  • a batch;
  • a dispatcher on a shared budget;
  • a dispatcher stopped mid-start;
  • Invoke-PkProcessWithTimeout;
  • a worker force-stopped by Invoke-PkRunspaceJobs.

There are also tests for a starter that throws after starting its program, and for an error raised after another job in the same pass was finalized. The five stop tests and the throwing-starter test failed before the fix. The harness passes on Windows PowerShell 5.1 and PowerShell 7 (399 assertions each).

🤖 Generated with Claude Code

HalRiveria and others added 2 commits November 16, 2026 13:16
When PowerShell stops a script (Ctrl+C outside Invoke-PkRun, a third press inside it, or the runspace engine force-stopping a worker), catch blocks are skipped and only finally blocks run. The dispatcher's teardown lived in catch blocks, so a stopped run left its programs running hidden until PowerShell exited, and never released its shared-budget slots. Invoke-PkProcessWithTimeout's finally only disposed.

The dispatcher's teardown is now one finally block, guarded by a flag set when the loop and abort wait finish normally. It also covers a job whose -JobStarter was still running, and skips jobs already finalized. Invoke-PkProcessWithTimeout's finally kills a program that's still running before disposing it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A -JobStarter that set .Proc and then threw left that program running untracked while its slot went back to the budget. The start-failure path now kills it first. Also cross-references the two copies of the per-job teardown steps, and adds a test for an error after another job in the same pass was finalized.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@HalRiveria-Bot
HalRiveria-Bot merged commit 6ca98ee into main Nov 16, 2026
2 checks passed
@HalRiveria-Bot
HalRiveria-Bot deleted the fix/stop-leaves-programs-running branch November 16, 2026 18:29
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