Repository navigation
Kill a stopped run's programs and give back its slots - #38
Merged
Merged
Conversation
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>
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
When PowerShell stops a script,
catchblocks are skipped and onlyfinallyblocks run. That happens on Ctrl+C outsideInvoke-PkRun, on a third press inside it, and when the runspace engine force-stops a worker after a drain timeout or a-ResultHandlerthrow. The dispatcher's kill-and-release code was incatchblocks, andInvoke-PkProcessWithTimeout'sfinallyonly disposed. So a stopped run left its programs running hidden until the PowerShell process exited, and a sharedSemaphoreSlimbudget never got its slots back.Separately, a
-JobStarterthat started its program and then threw left that program running untracked while its slot went back to the budget.What changed
Invoke-PkProcessDispatchCore: the twocatchteardowns are now onefinally, 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-JobStarterwas still running when the stop hit, and it skips jobs that were already finalized and disposed.Invoke-PkProcessWithTimeout: itsfinallykills 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:
Invoke-PkProcessWithTimeout;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