Repository navigation
Stop a -ResultHandler without a closure from looping forever - #19
Merged
Merged
Conversation
A -ResultHandler built without .GetNewClosure() sees Invoke-PkRunspaceJobs' locals by dynamic scope. The worker output it was being fed sat in a local named $results, so a handler doing $results.Add($r) added to the collection the kit was looping over, and the run never ended. Under Invoke-PkRun, Ctrl+C couldn't stop it either. The abort-drain path had the same loop, and a -JobCrashedHandler doing $results.Add($err) hit the kit's $null instead of the caller's list, with the error swallowed. Rename that local in both places so these callbacks reach the caller's own $results. Harness checks cover the normal path, the abort drain and the crash handler; all three fail without the fix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merged
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
A
-ResultHandlerbuilt without.GetNewClosure()seesInvoke-PkRunspaceJobs' local variables by dynamic scope. The worker output being fed to the handler sat in a local named$results. So a handler doing the natural thing,$results.Add($r), added to the collection the kit was looping over, and the run never ended: no error, one core busy, the caller's own$resultsempty. UnderInvoke-PkRun, Ctrl+C only sets the abort flag, which that loop never checks, so it couldn't be stopped.The same loop is in the abort-drain path (
Receive-PkCompletedHandles). And a-JobCrashedHandlerdoing$results.Add($err)hit the kit's$nullinstead of the caller's list; the error was swallowed, so the caller's list just stayed empty.Reproduced on Windows PowerShell 5.1 and PowerShell 7.
What changed
ParallelKit.ps1: renamed that local to$pkWorkerOutputin both places, with a comment in each naming the other.ParallelKit.Harness.ps1: three checks, all with closure-less callbacks:-ResultHandlerdoing$results.Addmakes exactly one call per result and reaches the caller's list (a 20-call guard throws instead of hanging the harness if it regresses);-JobCrashedHandlerdoing$results.Addreaches the caller's list.This is a narrow fix for the one name that caused a hang. Callbacks without a closure can still see the kit's other locals (for example
$outcome);.GetNewClosure()remains the documented way to build them.Testing
3 FAILED, 225 passed). The drain check also fails when only the main loop is fixed, so it covers the drain copy.ALL PASSED (228 assertions)on 5.1 and 7.🤖 Generated with Claude Code