From def610df2e17c14c1d3e1549ef501ed73a557f61 Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:23:29 -0500 Subject: [PATCH 1/7] Use American spelling throughout canceled, canceling, signaled, license, behavior, "right away" and "goes around again", in comments, test descriptions and the README. No public names change; the harness's local OwnCancelled key becomes OwnCanceled. Co-Authored-By: Claude Opus 5.5 --- ParallelKit.Harness.ps1 | 20 ++++++++++---------- ParallelKit.ps1 | 26 +++++++++++++------------- README.md | 20 ++++++++++---------- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index f74bf3f..aaa036e 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -703,7 +703,7 @@ Write-Host "" # ============================================================================== Write-Host "=== O: aborts and the caller's own tokens ===" -ForegroundColor Cyan Reset-Harness -# A caller-supplied source is cancelled by an abort, and left registered nowhere. +# A caller-supplied source is canceled by an abort, and left registered nowhere. $mine = [System.Threading.CancellationTokenSource]::new() $poll = @{ Count = 0 } $results = [System.Collections.Generic.List[object]]::new() @@ -727,7 +727,7 @@ $d = Invoke-PkProcessDispatch -MaxConcurrent 1 -AbortTimeoutSeconds 1 -CancelTok -PollCallback { param($running) $ticks.Count++; if ($ticks.Count -eq 5) { Request-PkAbort } }.GetNewClosure() $sw.Stop() Test-Assert ($d.Aborted -and $sw.Elapsed.TotalSeconds -lt 15) "dispatch: an abort stops a run that was given the caller's own -CancelToken" -Test-Assert (-not $mine.IsCancellationRequested) "without cancelling the caller's own token" +Test-Assert (-not $mine.IsCancellationRequested) "without canceling the caller's own token" Test-Assert ([PkState]::SourceCount -eq 0) "and nothing is left registered" # ...and the caller's token still works as a cancel. @@ -738,7 +738,7 @@ $d = Invoke-PkProcessDispatch -MaxConcurrent 1 -AbortTimeoutSeconds 1 -CancelTok -JobBuilder { param($g) return @(@{ FilePath = $pingExe; Arguments = '-n 30 127.0.0.1' }) }.GetNewClosure() ` -JobFinalize { param($job, $pending) $null = Complete-PkProcess -Job $job } ` -PollCallback { param($running) $ticks.Count++; if ($ticks.Count -eq 5) { $mine.Cancel() } }.GetNewClosure() -Test-Assert ($d.Aborted -and $d.ForceKilledCount -eq 1) "dispatch: cancelling the caller's own token still stops the run" +Test-Assert ($d.Aborted -and $d.ForceKilledCount -eq 1) "dispatch: canceling the caller's own token still stops the run" $mine.Dispose() # An abort stays in force until it is reset. @@ -1152,7 +1152,7 @@ $d = Invoke-PkProcessDispatch -MaxConcurrent 1 ` $watches.Add($job.Stopwatch) if (-not $state.Retried) { $state.Retried = $true; $pending.Enqueue($job) } }.GetNewClosure() -Test-Assert ($watches.Count -eq 2 -and $null -ne $watches[1] -and -not [object]::ReferenceEquals($watches[0], $watches[1])) "a job that goes round again gets a fresh stopwatch" +Test-Assert ($watches.Count -eq 2 -and $null -ne $watches[1] -and -not [object]::ReferenceEquals($watches[0], $watches[1])) "a job that goes around again gets a fresh stopwatch" # A large batch isn't slowed by per-job queue walks. No processes: the # starter/poll are stubs, so this times the dispatcher's own bookkeeping. @@ -1314,7 +1314,7 @@ $ownTokenEngine = { -GroupProvider { if ($feed.Given) { return $null }; $feed.Given = $true; return 'g' }.GetNewClosure() ` -JobBuilder { param($g) return @(@{ FilePath = $PingExe; Arguments = '-n 30 127.0.0.1' }, @{ FilePath = $PingExe; Arguments = '-n 30 127.0.0.1' }) }.GetNewClosure() ` -JobFinalize { param($job, $pending) $null = Complete-PkProcess -Job $job } - return @{ Aborted = $d.Aborted; OwnCancelled = $mineInWorker.IsCancellationRequested } + return @{ Aborted = $d.Aborted; OwnCanceled = $mineInWorker.IsCancellationRequested } } $results = [System.Collections.Generic.List[hashtable]]::new() $poll = @{ Count = 0 } @@ -1324,7 +1324,7 @@ $out = Invoke-PkRunspaceJobs -WorkerScript $ownTokenEngine -JobParams @{ PingExe -PollCallback { $poll.Count++; if ($poll.Count -eq 15) { Request-PkAbort } }.GetNewClosure() $sw.Stop() Test-Assert ($out.ForceStoppedCount -eq 0 -and $results.Count -eq 1 -and $results[0].Aborted) ("a dispatcher in a worker with its own -CancelToken stops on an abort (force-stopped " + $out.ForceStoppedCount + ", " + [int]$sw.Elapsed.TotalSeconds + "s)") -Test-Assert ($results.Count -eq 1 -and -not $results[0].OwnCancelled) "without cancelling the worker's own token" +Test-Assert ($results.Count -eq 1 -and -not $results[0].OwnCanceled) "without canceling the worker's own token" Test-Assert ([PkState]::SourceCount -eq 0) "and nothing is left registered" # Every batch result has every documented field, so a strict-mode caller @@ -1483,7 +1483,7 @@ $notice = @($script:messages | Where-Object { $_.Message -like '*Abort requested Test-Assert ($notice.Count -eq 1 -and (Get-PkAbortTriggerLabel) -eq 'Request-PkAbort') ("an abort from code isn't announced as Ctrl+C (label: " + (Get-PkAbortTriggerLabel) + ")") # Once an abort from code is under way, a single press skips the wait. $null = [PkConsoleCtrl]::OnInterrupt('Ctrl+C') -Test-Assert ([PkConsoleCtrl]::SkipDrain) "a press during an abort started from code skips the drain straight away" +Test-Assert ([PkConsoleCtrl]::SkipDrain) "a press during an abort started from code skips the drain right away" # The force-stop notice says why. Reset-Harness @@ -1517,7 +1517,7 @@ $batch = Invoke-PkRun { }.GetNewClosure() $sw.Stop() $null = $presser.PS.EndInvoke($presser.Handle); $presser.PS.Dispose() -Test-Assert ($batch.Aborted -and $batch.ForceKilledCount -eq 3 -and $sw.Elapsed.TotalSeconds -lt 8 -and $after.Ran) ("batch: a second Ctrl+C force-stops running programs straight away (" + [math]::Round($sw.Elapsed.TotalSeconds, 1) + "s, killed " + $batch.ForceKilledCount + "; the programs run 15s)") +Test-Assert ($batch.Aborted -and $batch.ForceKilledCount -eq 3 -and $sw.Elapsed.TotalSeconds -lt 8 -and $after.Ran) ("batch: a second Ctrl+C force-stops running programs right away (" + [math]::Round($sw.Elapsed.TotalSeconds, 1) + "s, killed " + $batch.ForceKilledCount + "; the programs run 15s)") Test-Assert (@($script:messages | Where-Object { $_.Message -like '*force-stopping 3 job(s) now: Ctrl+C was pressed again*' }).Count -eq 1) "and the force-stop says why" # Runspace jobs: the same, and the workers are force-stopped. @@ -1530,7 +1530,7 @@ $rsOut = Invoke-PkRun { }.GetNewClosure() $sw.Stop() $null = $presser.PS.EndInvoke($presser.Handle); $presser.PS.Dispose() -Test-Assert ($rsOut.Aborted -and $rsOut.ForceStoppedCount -eq 3 -and $sw.Elapsed.TotalSeconds -lt 8) ("runspace jobs: a second Ctrl+C force-stops running workers straight away (" + [math]::Round($sw.Elapsed.TotalSeconds, 1) + "s, stopped " + $rsOut.ForceStoppedCount + "; the workers run 15s)") +Test-Assert ($rsOut.Aborted -and $rsOut.ForceStoppedCount -eq 3 -and $sw.Elapsed.TotalSeconds -lt 8) ("runspace jobs: a second Ctrl+C force-stops running workers right away (" + [math]::Round($sw.Elapsed.TotalSeconds, 1) + "s, stopped " + $rsOut.ForceStoppedCount + "; the workers run 15s)") # The handler thread a window close parks stays parked until the run ends, # not just the drain, so the rest of the run gets Windows' few seconds. @@ -2295,7 +2295,7 @@ for ($try = 0; $try -lt 3 -and $bestPerJob -ge 70; $try++) { Test-Assert ($bestPerJob -lt 70) ("short workers aren't held back by the poll (best average " + [int]$bestPerJob + " ms each)") # A -JobPollCompleted that keeps saying "not done" after a process exits must -# not leave that process's signalled handle in the wait, or the loop spins: +# not leave that process's signaled handle in the wait, or the loop spins: # first while the second program still runs, then with both exited. $spinState = @{ Given = $false; Polls = 0 } $spinRun = Invoke-PkProcessDispatch -MaxConcurrent 2 ` diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index 53f81cc..7dd5105 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -537,14 +537,14 @@ function Register-PkCancellationSource { # .SYNOPSIS # Adds your own CancellationTokenSource to the set an abort cancels. # .DESCRIPTION - # Every registered source is cancelled the moment an abort is detected. If an + # Every registered source is canceled the moment an abort is detected. If an # abort is still in force when you register (see Reset-PkAbortState), the source - # is cancelled straight away. A -Cts passed to Invoke-PkRunspaceJobs is + # is canceled right away. A -Cts passed to Invoke-PkRunspaceJobs is # registered for you. Remove it with Unregister-PkCancellationSource when done. # .PARAMETER Cts # The source to cancel on an abort. - # Cancelling a source registered while an old abort is still in force + # Canceling a source registered while an old abort is still in force # matters: otherwise a later run would start normally while its workers, # seeing the old flag, quietly did nothing. param([Parameter(Mandatory)][System.Threading.CancellationTokenSource]$Cts) @@ -568,7 +568,7 @@ function Reset-PkAbortState { # Clears a finished abort so the engines run again. # .DESCRIPTION # An abort stays in force until this is called: until then every engine returns - # straight away with Aborted = $true. Invoke-PkRun calls it at the start of each + # right away with Aborted = $true. Invoke-PkRun calls it at the start of each # run, so you only need it to call an engine again outside Invoke-PkRun after an # abort. @@ -632,7 +632,7 @@ function Test-PkAbortSignal { function Test-PkCancelRequested { # .SYNOPSIS - # For worker code: returns $true once this run is being cancelled or aborted. + # For worker code: returns $true once this run is being canceled or aborted. # .DESCRIPTION # Check it before any step that can't be undone (deleting a source file, say) # and skip the step if it's true: a worker that was force-stopped isn't killed, @@ -1165,9 +1165,9 @@ function Wait-PkProcess { function Wait-PkRunningJobs { # The dispatch loop's pause: until a running program exits, the token is - # cancelled, or TimeoutMs passes. Only programs that haven't exited are waited + # canceled, or TimeoutMs passes. Only programs that haven't exited are waited # on: a -JobPollCompleted can keep saying "not done" after the exit, and the - # signalled handle would make every wait return at once and spin a core. + # signaled handle would make every wait return at once and spin a core. # A job with no real process, or past the 63 handles WaitAny can hold on an STA # thread (one of them the token), is only noticed when the time is up. With # nothing to wait on, a plain sleep. Same handle wrapping as Wait-PkProcess. @@ -1260,7 +1260,7 @@ function Invoke-PkProcessWithTimeout { # Runs one program to completion, with a hard time limit. # .DESCRIPTION # Throws if the program had to be killed: because it ran past -TimeoutMs, or - # because the run was aborted or cancelled while it was going. Anything the + # because the run was aborted or canceled while it was going. Anything the # program started is killed with it. # .PARAMETER FilePath # The program to run. @@ -1373,8 +1373,8 @@ function Invoke-PkProcessDispatch { # { param($runningJobs) } run on every loop pass, with the jobs currently running. # -AbortPollCallback (its name before 3.0) also works. # .PARAMETER CancelToken - # Stops the dispatcher when cancelled. An abort stops it either way, without - # cancelling this token. + # Stops the dispatcher when canceled. An abort stops it either way, without + # canceling this token. # .PARAMETER AbortTimeoutSeconds # How long an abort waits for running jobs before force-stopping them # (default 60). A force-stopped program is killed along with anything it @@ -1486,7 +1486,7 @@ function Invoke-PkProcessDispatch { # The caller's own token: nothing would cancel it on Ctrl+C. Run on # a linked source that an abort DOES cancel (and that still follows # the caller's token). In a worker it also follows the run's token, - # so the run being cancelled for any reason stops this dispatcher. + # so the run being canceled for any reason stops this dispatcher. $pkLinkTokens = [System.Collections.Generic.List[System.Threading.CancellationToken]]::new() $pkLinkTokens.Add($pkCancelToken) if ($null -ne $global:PkCancelToken) { $pkLinkTokens.Add($global:PkCancelToken) } @@ -2001,7 +2001,7 @@ function Invoke-PkProcessBatch { # TimeoutSeconds (overrides -TimeoutSeconds) and any keys of your own, which # come back on the result's Command. Inside # Invoke-PkRun, Ctrl+C lets running programs finish (up to -AbortTimeoutSeconds) - # and starts no new ones; a second Ctrl+C kills them straight away. Every + # and starts no new ones; a second Ctrl+C kills them right away. Every # command is tried, and one that can't start gets a StartError on its result. # Repeats of the same failure for the same program are reported once, with a # count at the end. @@ -2621,7 +2621,7 @@ function Invoke-PkRunspaceJobs { } $handles = $stillPending if ($handles.Count -gt 0 -and ($scanExhausted -or $handles.Count -ge $MaxParallel)) { - # Until a worker finishes or the token is cancelled. The 100 ms cap + # Until a worker finishes or the token is canceled. The 100 ms cap # keeps worker messages, -PollCallback and the abort flag moving. # WaitAny holds 63 handles at most on an STA thread, one of them the # token; a worker past that is only noticed when the time is up. diff --git a/README.md b/README.md index 99b1f13..1252486 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Run thousands of command-line programs, or blocks of PowerShell code, in paralle - **Process dispatch** - run many external programs, a set number at a time, with their output captured. - **Runspace jobs** - run a PowerShell worker script many times in parallel. -- **Graceful abort** - Ctrl+C lets in-flight work finish and stops cleanly, instead of killing everything mid-write. Closing the window stops what's left straight away, since Windows allows only a few seconds. +- **Graceful abort** - Ctrl+C lets in-flight work finish and stops cleanly, instead of killing everything mid-write. Closing the window stops what's left right away, since Windows allows only a few seconds. - **No orphans** - programs the script started die with it, even if it's force-closed. Built with Claude Code. @@ -93,7 +93,7 @@ If your machines only run signed scripts (the `AllSigned` execution policy), the It works whether or not your script uses `Set-StrictMode`. -The version is in the comment block at the top of the file, so a copy kept in your project still says which one it is. That block also carries the licence notice; keep it with the file. +The version is in the comment block at the top of the file, so a copy kept in your project still says which one it is. That block also carries the license notice; keep it with the file. Dot-source it. It loads itself as an in-memory module named `ParallelKit`: nothing is installed and nothing goes on `$env:PSModulePath`, but `Get-Module` lists it while it's loaded. Dot-sourcing it again replaces the copy already loaded. @@ -248,10 +248,10 @@ All of these callbacks run on whichever thread called the dispatcher: the main t - Supply `-JobStarter { param($job) ... }` to start jobs yourself. It must set `$job.Proc` and should return the job. A starter that is just `Start-PkProcess ...` works too: its result is folded into your job. Throw to report a job that couldn't start. - Whatever your callbacks output is thrown away, apart from the return values listed here. They can't corrupt the dispatcher's result by accident. - `-MaxConcurrent` can be a `SemaphoreSlim` shared by several dispatchers that must stay under one combined limit. It works inside workers, so two runspace jobs can each run a dispatcher against one shared budget. -- `-GroupReleaseCheck { param($group) }` makes a group with zero jobs keep its queue-ahead slot until the check returns true. Use it to stop one dispatcher racing ahead of another that it's paired with. The dispatcher doesn't return until every held group has been released, unless the run is cancelled or aborted. It runs on every loop pass, so keep it cheap. +- `-GroupReleaseCheck { param($group) }` makes a group with zero jobs keep its queue-ahead slot until the check returns true. Use it to stop one dispatcher racing ahead of another that it's paired with. The dispatcher doesn't return until every held group has been released, unless the run is canceled or aborted. It runs on every loop pass, so keep it cheap. - `-JobPollCompleted { param($job) }` replaces the default "has the process exited" check. Use it for a custom starter whose job isn't a plain process. - `-PollCallback { param($runningJobs) }` runs on every loop pass, with the jobs currently running (`-AbortPollCallback`, its name before 3.0, also works). That's about every 50 ms, and can be many times more often when programs finish quickly, so keep it cheap. -- `-CancelToken` stops the dispatcher when cancelled. An abort stops it either way, without cancelling your token. +- `-CancelToken` stops the dispatcher when canceled. An abort stops it either way, without canceling your token. - An exception from one of your callbacks stops the dispatcher: running processes are killed and the exception is rethrown. - Returns an object with `GroupResults`, `Aborted`, `ForceKilledCount` and `JobStarterFailedCount`. `GroupResults` holds one object with `GroupInfo`, `Completed` and `Aborted` per group, where `GroupInfo` is the group value. @@ -293,19 +293,19 @@ Inside `Invoke-PkRun { ... }`, Ctrl+C: 3. waits for in-flight work (`-AbortTimeoutSeconds`, default 60; every engine also takes it as `-DrainTimeoutSeconds`), 4. force-stops whatever is left, calling `-JobAbortCleanup` for each killed or never-started process job. -Press Ctrl+C again to skip step 3: whatever is still running is force-stopped straight away, and your script carries on. A third press stops the script the way Ctrl+C normally does. Presses less than a second apart count as one, and Ctrl+Break works the same as Ctrl+C. +Press Ctrl+C again to skip step 3: whatever is still running is force-stopped right away, and your script carries on. A third press stops the script the way Ctrl+C normally does. Presses less than a second apart count as one, and Ctrl+Break works the same as Ctrl+C. -Closing the window does the same minus step 3: Windows allows only a few seconds, so leftovers are stopped straight away. +Closing the window does the same minus step 3: Windows allows only a few seconds, so leftovers are stopped right away. Three things to know: - **Inside `Invoke-PkRun`, Ctrl+C no longer kills anything by itself.** It only raises a flag. The kit's engines watch that flag. A long step of your own in there has to check `Test-PkAbortSignal` now and then and stop when it's true, or Ctrl+C will seem to do nothing until that step ends. -- **An abort stays in force until it's reset.** `Invoke-PkRun` resets it at the start of each run. If you call an engine again outside `Invoke-PkRun` after an abort, it returns straight away with `Aborted = $true`. Call `Reset-PkAbortState` first if you mean to carry on. +- **An abort stays in force until it's reset.** `Invoke-PkRun` resets it at the start of each run. If you call an engine again outside `Invoke-PkRun` after an abort, it returns right away with `Aborted = $true`. Call `Reset-PkAbortState` first if you mean to carry on. - **Your script carries on after an abort.** The engines return with `Aborted = $true` and the rest of your script runs. If an abort should end it, say so: `if ($batch.Aborted) { exit 1 }`. `Invoke-PkRun` also turns off the console's QuickEdit mode for the length of the run (`-DisableQuickEdit $false` to opt out). With QuickEdit on in the classic console window, one stray click starts a text selection, and the script freezes at its next console write until the selection is cleared. That looks exactly like a hang. It's turned back on afterwards if it was on before. -Other pieces: `Request-PkAbort` triggers the same abort from code. `Test-PkAbortSignal` asks whether one is under way. `Register-PkCancellationSource` adds your own `CancellationTokenSource` to the set an abort cancels, and `Unregister-PkCancellationSource` takes it back out (a `-Cts` you pass to `Invoke-PkRunspaceJobs` is added automatically; a `-CancelToken` you pass to the dispatcher isn't cancelled by an abort, but the dispatcher stops either way). `Get-PkAbortTriggerLabel` says which kind of abort it was. +Other pieces: `Request-PkAbort` triggers the same abort from code. `Test-PkAbortSignal` asks whether one is under way. `Register-PkCancellationSource` adds your own `CancellationTokenSource` to the set an abort cancels, and `Unregister-PkCancellationSource` takes it back out (a `-Cts` you pass to `Invoke-PkRunspaceJobs` is added automatically; a `-CancelToken` you pass to the dispatcher isn't canceled by an abort, but the dispatcher stops either way). `Get-PkAbortTriggerLabel` says which kind of abort it was. ## Choosing limits @@ -378,7 +378,7 @@ No. Nothing it does needs elevation. **What happens to running programs when I press Ctrl+C?** -Inside `Invoke-PkRun`, no new ones start, and the running ones get up to 60 seconds by default to finish. Anything still running after that is killed, and `-JobAbortCleanup` is called for each so you can delete its partial output. Press Ctrl+C again to kill them straight away instead of waiting. See [Abort](#abort). +Inside `Invoke-PkRun`, no new ones start, and the running ones get up to 60 seconds by default to finish. Anything still running after that is killed, and `-JobAbortCleanup` is called for each so you can delete its partial output. Press Ctrl+C again to kill them right away instead of waiting. See [Abort](#abort). **And if the script is killed outright, from Task Manager say?** @@ -406,7 +406,7 @@ No. Programs start without a window, in a hidden console of their own, so Ctrl+C **Was this written by AI?** -It was built with Claude Code, as the top of this page says. The worker rules and abort behaviour come from hangs and bugs reproduced in real runs, and the test harness runs real processes and runspaces, with nothing mocked, on every change. +It was built with Claude Code, as the top of this page says. The worker rules and abort behavior come from hangs and bugs reproduced in real runs, and the test harness runs real processes and runspaces, with nothing mocked, on every change. ## Bugs and questions From effd73f703af913737453f79b2fa7f7a5e61f930 Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:32:15 -0500 Subject: [PATCH 2/7] Prune comments in ParallelKit.ps1 Drops comments that restated the code, described behavior that no longer exists, or explained a bug's history, and shortens the rest. Comments that explain a non-obvious why are kept. No code changes. Co-Authored-By: Claude Opus 5.5 --- ParallelKit.ps1 | 537 +++++++++++++++++++----------------------------- 1 file changed, 214 insertions(+), 323 deletions(-) diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index 7dd5105..fb61916 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -6,33 +6,25 @@ # the full text is in LICENSE in the repository. # # Use: . "\ParallelKit.ps1" (dot-source it; it loads itself as an in-memory module) -# Read README.md first - the two worker-code rules there are what keep this -# from hanging or crashing. +# Read README.md first: its two worker-code rules are what keep this from +# hanging or crashing. # -# Works under Set-StrictMode: job hashtables are read as $job['Name'], never -# $job.Name, because under strict mode the dot form THROWS for a key that -# isn't there yet. Keep it that way when editing. -# -# State lives in the static [PkState] class, never in $script: variables - a -# function called from inside a .GetNewClosure()'d block can see an empty -# $script: scope, and a static has no scope to lose. +# Works under Set-StrictMode: read job hashtables as $job['Name'], never +# $job.Name, which throws there for a key that isn't set yet. State lives in +# the static [PkState] class, never in $script: variables: a function called +# from a .GetNewClosure() block can see an empty $script: scope. -# Everything below needs Add-Type, which Constrained Language Mode blocks with -# an error that doesn't say why. Say why. +# Add-Type needs FullLanguage: say so, rather than fail on Add-Type's error. if ($ExecutionContext.SessionState.LanguageMode -ne 'FullLanguage') { throw ("ParallelKit: this PowerShell session runs in " + $ExecutionContext.SessionState.LanguageMode + " mode, but the kit needs FullLanguage " + "(it compiles C# with Add-Type). This is usually set by AppLocker or Windows Defender Application Control policy. " + "Ask whoever manages the machine to allow this script, or run it where that policy doesn't apply.") } -# Everything below loads as an in-memory module named ParallelKit. The kit's -# own frames then live in the module's session state, so a callback you pass -# in sees your variables, never the kit's locals, and the internal helpers -# stay out of your session. The public functions are imported globally, so -# they work from any .GetNewClosure() block and for the rest of the session. -# A copy loaded earlier in this session is dropped first, so only one is -# listed. -ErrorAction Stop: without it, a throw in the module body (the -# stale-types check below, say) wouldn't stop the script that loads the kit. +# Loads as an in-memory module, so a callback you pass in sees your +# variables, never the kit's, and the private helpers stay out of your +# session. A copy loaded earlier is removed first. -ErrorAction Stop makes a +# throw in here (the stale-types check) stop the script that loads the kit. Get-Module -Name ParallelKit | Remove-Module New-Module -Name ParallelKit -ErrorAction Stop -ScriptBlock { @@ -58,13 +50,12 @@ public static class PkConsoleCtrl public static volatile bool AbortRequested = false; public static volatile bool IsCloseEvent = false; // true = window close/logoff/shutdown, false = Ctrl+C - // The handler only intercepts while a run is active. Outside a run, - // Ctrl+C and closing the window behave exactly as they normally would. + // The handler only acts while a run is active. public static volatile bool RunActive = false; // On a window close, logoff or shutdown the handler parks on this until - // the run ends: the process dies as soon as the handler returns, and - // Windows ends it after a few seconds anyway. Ctrl+C never parks. + // the run ends, since the process dies as soon as the handler returns. + // Ctrl+C never parks. public static readonly ManualResetEventSlim DrainComplete = new ManualResetEventSlim(false); public static int DrainWaitMs = 65000; @@ -169,11 +160,8 @@ public static class PkState public static object MessageHandler = null; // a PowerShell scriptblock, or null for the default public static volatile bool ThreadPoolRaised = false; - // Bump this whenever any C# in this file changes. A PowerShell session - // keeps compiled types until it exits, so re-loading an edited kit into - // an old session would silently run the OLD C# - the check after the - // type definitions catches that. That check compares against this - // number too; bump both together. + // Bump this, and the check after the type definitions, whenever any C# + // in this file changes: a session keeps the types it compiled first. public const int TypesVersion = 4; // Locked: Register-/Unregister-PkCancellationSource are callable from @@ -291,9 +279,8 @@ public static class PkChildProcessJob [DllImport("kernel32.dll", SetLastError = true)] private static extern bool CloseHandle(IntPtr hObject); - // Deliberately never closed: the OS closes it when this process exits, - // and that close is what kills the children. Closing it early would - // kill running work. + // Never closed: Windows closes it when this process exits, and that is + // what kills the children. private static IntPtr _job = IntPtr.Zero; private static readonly object _lock = new object(); @@ -328,8 +315,8 @@ public static class PkChildProcessJob } } - // Never throws - a failed assignment only loses the force-kill cleanup - // for that one process, it must not fail the work itself. + // Never throws: a failed assignment only loses the force-kill cleanup + // for that one process. public static bool Assign(System.Diagnostics.Process process) { if (_job == IntPtr.Zero || process == null) { return false; } @@ -345,17 +332,15 @@ public static class PkChildProcessJob '@ -Language CSharp } -# A session that loaded an earlier copy of this file still has that copy's -# compiled types, and they can't be replaced. Say so, instead of failing -# later with a missing-method error somewhere unrelated. Keep this number -# in sync with [PkState]::TypesVersion above. +# A session that loaded an older copy keeps its compiled types. Say so, +# rather than fail later with a missing-method error. Keep this number in +# sync with [PkState]::TypesVersion. if ((& { try { [PkState]::TypesVersion } catch { 0 } }) -ne 4) { throw "ParallelKit: this PowerShell session already loaded an older version of the kit, and its compiled parts can't be swapped out. Close this window and start a new one." } -# How kit functions tell a worker runspace from the main one: in a worker -# these two globals are set by Invoke-PkRunspaceJobs; everywhere else they -# are null. Defined here so reading them is legal under Set-StrictMode. +# Set in a worker by Invoke-PkRunspaceJobs, null elsewhere: how kit +# functions tell where they run. Defined here for strict mode. if (-not (Test-Path -LiteralPath 'Variable:\global:PkWorkerQueue')) { $global:PkWorkerQueue = $null } if (-not (Test-Path -LiteralPath 'Variable:\global:PkCancelToken')) { $global:PkCancelToken = $null } @@ -385,22 +370,19 @@ function Invoke-PkMessageHandler { $handler = [PkState]::MessageHandler if ($null -ne $handler) { try { - # A `continue` or `break` in the handler stops at this do/while - # instead of in whichever kit loop wrote the message, and counts - # as `return`. The same idea as Invoke-PkCallback, which can't be - # used here: its warning is itself a message. This one goes to - # the console, once per handler (a note on the scriptblock). + # Invoke-PkCallback's do/while, inlined: its warning is itself a + # message. Warns on the console, once per handler (a note on the + # scriptblock). $handlerDone = $false - do { $null = & $handler $Message $Level; $handlerDone = $true } while ($false) # a handler's stray output must not become our caller's output + do { $null = & $handler $Message $Level; $handlerDone = $true } while ($false) if (-not $handlerDone -and $null -eq $handler.PSObject.Properties['PkLoopWarned']) { $handler.PSObject.Properties.Add([psnoteproperty]::new('PkLoopWarned', $true)) Write-Host "[ParallelKit] the message handler used 'continue' or 'break'. PowerShell sends those to the nearest loop, which here is one of the kit's, so the kit stopped the handler there instead. Use 'return' to leave it early." -ForegroundColor Yellow } return } catch { - # A handler that fails (a locked log file, say) must not take - # the run down with it. Fall back to the console for this one - # message and say why. + # A failing handler (a locked log file, say) mustn't stop the run: + # show this message on the console instead. Write-Host ("[ParallelKit] message handler failed (" + $_ + ") - showing the message here instead:") -ForegroundColor Yellow } } @@ -426,9 +408,8 @@ function Write-PkMessage { [ValidateSet('Info', 'Warn', 'Error', 'Warning')] [string]$Level = 'Info' ) - # A message handler only ever sees Info, Warn or Error, spelled that way: - # ValidateSet ignores case, and Warning means Warn. Before a worker queues - # it, so it holds there too. + # Handlers only ever see Info, Warn or Error, spelled that way: + # ValidateSet ignores case. Done before a worker queues it. switch ($Level) { 'Error' { $Level = 'Error' } 'Info' { $Level = 'Info' } @@ -452,17 +433,14 @@ function Receive-PkWorkerMessages { } function Invoke-PkCallback { - # Calls one of the caller's callbacks and returns what it output. - # PowerShell sends a `continue` or `break` to the nearest loop on the call - # stack, across the module boundary: for a callback, one of the kit's own - # loops, which then skips its bookkeeping or never ends. The do/while here - # is that nearest loop instead, so the keyword only ends the callback, as - # `return` would, keeping whatever it output so far. Warns once per - # -PkName in -PkWarned (every time without one). A labeled `break outer` - # still goes through to the caller's own loop. Invoke-PkMessageHandler has - # its own copy of this, since this one's warning is itself a message. - # pk-prefixed, parameters included: in a worker this frame sits between - # the kit and a callback built without .GetNewClosure(), which sees it. + # Calls one of the caller's callbacks and returns its output. A `continue` + # or `break` in a callback goes to the nearest loop on the call stack, even + # across the module boundary: one of the kit's, which then skips its + # bookkeeping or never ends. This do/while catches it instead, so it ends + # only the callback, like `return`. Warns once per -PkName in -PkWarned. + # A labeled `break outer` still reaches the caller's loop. + # Invoke-PkMessageHandler has its own copy of this. + # pk-prefixed: in a worker, a callback without .GetNewClosure() sees this frame. param( [Parameter(Mandatory)][scriptblock]$PkCallback, [object[]]$PkArgs = @(), @@ -475,8 +453,7 @@ function Invoke-PkCallback { if ($null -ne $PkWarned) { $PkWarned[$PkName] = $true } Write-PkMessage ($PkName + " used 'continue' or 'break' outside a loop of its own. PowerShell sends those to the nearest loop, which here is one of the kit's, so the kit stopped the callback there instead. Use 'return' to leave a callback early.") -Level Warn } - # The comma hands back exactly what the callback gave, as a direct call - # would: a collection it returned whole (`return ,$list`) stays one value. + # The comma keeps a collection returned whole (`return ,$list`) one value. if ($null -ne $pkOutput) { return ,$pkOutput } } @@ -499,10 +476,9 @@ function Initialize-PkChildProcessJob { } function Initialize-PkThreadPool { - # Every redirected child process holds two thread-pool threads for its - # async stdout/stderr reads. The pool grows slowly past its minimum, so - # a burst of processes can leave reads waiting long enough to look like - # a hung process. Raise the minimum up front. + # Each redirected child holds two pool threads for its output reads, and + # the pool grows slowly past its minimum, so a burst of processes could + # stall their reads. Raise the minimum up front. param([int]$MaxConcurrentProcesses = 0) $clampedProcs = [Math]::Min([Math]::Max($MaxConcurrentProcesses, 0), 16000) $minimumWorkerThreads = [Math]::Max(128, (2 * $clampedProcs) + 32) @@ -544,9 +520,8 @@ function Register-PkCancellationSource { # .PARAMETER Cts # The source to cancel on an abort. - # Canceling a source registered while an old abort is still in force - # matters: otherwise a later run would start normally while its workers, - # seeing the old flag, quietly did nothing. + # Canceled at once while an old abort is in force: otherwise a later run + # would start while its workers, seeing the old flag, did nothing. param([Parameter(Mandatory)][System.Threading.CancellationTokenSource]$Cts) [PkState]::AddSource($Cts) if ([PkState]::AbortLatched) { @@ -701,14 +676,11 @@ function Invoke-PkRun { [Parameter(Mandatory)] [scriptblock]$ScriptBlock, [bool]$DisableQuickEdit = $true, - # Stop the PC idle-sleeping for the length of the run (the display - # can still turn off; lid close / Start -> Sleep still work). [bool]$KeepAwake = $true ) - # Already inside a run (a helper that brackets its own work, called - # from a bracketed caller): just run it. Re-arming here would wipe the - # outer run's abort state, and disarming on the way out would leave the - # rest of the outer run unprotected. + # Already inside a run: just run it. Re-arming would wipe the outer run's + # abort state, and disarming afterwards would leave the rest of it + # unprotected. if ([PkConsoleCtrl]::RunActive) { & $ScriptBlock return @@ -717,7 +689,6 @@ function Invoke-PkRun { if (-not [PkChildProcessJob]::IsInitialized) { $null = Initialize-PkChildProcessJob } Reset-PkAbortState [PkConsoleCtrl]::RunActive = $true - # Only turn QuickEdit back on afterwards if it was on to begin with. $quickEditWasOn = ([PkConsoleQuickEdit]::GetEnabled() -eq 1) if ($DisableQuickEdit -and $quickEditWasOn) { $null = [PkConsoleQuickEdit]::SetEnabled($false) } if ($KeepAwake) { $null = [PkSleepBlock]::Block() } @@ -778,14 +749,12 @@ function ConvertTo-PkArgumentString { } function ConvertTo-PkArgumentStringCore { - # ConvertTo-PkArgumentString's body. -ForBatchFile is for a .bat or .cmd - # program, whose command line cmd.exe reads first: a quote inside an - # argument is written "" instead of \". cmd.exe counts every quote, and to - # it \" is an ordinary quote that ends the quoted part, so an argument like - # a" & del ... & " ran its second command. "" keeps cmd's count even, and a - # program the batch file passes %* on to still reads "" as one quote (the C - # runtime does). A line break would end cmd.exe's command line there and - # silently drop the rest, so it's refused. + # ConvertTo-PkArgumentString's body. -ForBatchFile: for a .bat or .cmd, + # whose command line cmd.exe reads first, a quote is written "" instead of + # \". cmd.exe counts every quote, \" included, so with \" an argument like + # a" & del ... & " runs a second command. "" keeps the count even, and the + # C runtime still reads it as one quote in a program the batch file passes + # %* to. A line break would end cmd.exe's command line, so it's refused. param( [AllowNull()] [AllowEmptyCollection()] @@ -806,10 +775,10 @@ function ConvertTo-PkArgumentStringCore { if ($ForBatchFile -and $arg.IndexOfAny([char[]]@("`r", "`n")) -ge 0) { throw ("Arguments element " + $position + " contains a line break, which can't be passed to a .bat or .cmd program: cmd.exe would end the command line there and drop the rest. Pass it another way (a file, an environment variable).") } - # The C runtime only needs quotes around whitespace and quotes. The rest - # are cmd.exe's: it runs a .bat/.cmd program's command line, acting on - # & | < > ^ ( ) outside quotes and splitting batch arguments at , ; - # Quoting them is harmless to other programs. + # The C runtime only needs whitespace and quotes quoted. The rest are for + # cmd.exe, which runs a .bat/.cmd: it acts on & | < > ^ ( ) outside + # quotes and splits batch arguments at , ; Quoting them is harmless + # to other programs. if ($arg.Length -gt 0 -and $arg.IndexOfAny([char[]]@(' ', "`t", "`n", "`v", '"', '&', '|', '<', '>', '^', '(', ')', ',', ';')) -lt 0) { $parts.Add($arg) continue @@ -844,13 +813,11 @@ function ConvertTo-PkArgumentStringCore { } function Resolve-PkArguments { - # -Arguments / Arguments as given -> the command line. A string is used as - # written; a list is quoted one argument per element, so a path with a space - # stays whole (a plain [string] cast would join it with spaces, or $OFS). A - # lone file or folder object is one argument, its full path. -FilePath is - # the program: a list for a .bat or .cmd is quoted for cmd.exe (see - # ConvertTo-PkArgumentStringCore). A bare name only ever finds an .exe, - # so a batch file always has its extension here. + # -Arguments as given -> the command line: a string as written, a list + # quoted one argument per element (a [string] cast would join it with + # spaces, or $OFS), a lone file or folder object as its full path. + # -FilePath picks batch-file quoting for a .bat or .cmd. A bare name only + # finds an .exe, so a batch file always has its extension here. param($Arguments, [string]$FilePath = '') if ($null -eq $Arguments) { return '' } if ($Arguments -is [string]) { return $Arguments } @@ -963,11 +930,9 @@ function Start-PkProcess { } function Start-PkProcessCore { - # Start-PkProcess's body. -CallerState is the session of whoever called the - # kit's public function, for paths only visible from there: the engines - # start programs from deep inside the module, where $PSCmdlet no longer - # leads back to the caller. Called by Start-PkProcess, - # Invoke-PkProcessWithTimeout, the dispatcher's default starter and the batch. + # Start-PkProcess's body. -CallerState: the session of whoever called the + # public function, for paths only visible from there. Deep inside the + # module, $PSCmdlet no longer leads back to the caller. param( [Parameter(Mandatory)] [string]$FilePath, @@ -978,9 +943,7 @@ function Start-PkProcessCore { ) $encoding = Resolve-PkEncoding -Encoding $OutputEncoding $commandLine = Resolve-PkArguments -Arguments $Arguments -FilePath $FilePath - # Self-starting: a caller who never ran Initialize-ParallelKit still - # gets children that die with the script, and output reads that - # aren't starved of threads. Both are cheap no-ops once done. + # For a caller who never ran Initialize-ParallelKit. No-ops once done. if (-not [PkChildProcessJob]::IsInitialized) { $null = [PkChildProcessJob]::Initialize() } if (-not [PkState]::ThreadPoolRaised) { $null = Initialize-PkThreadPool } @@ -996,18 +959,16 @@ function Start-PkProcessCore { $psi.RedirectStandardError = $true $psi.CreateNoWindow = $true - # Relative paths mean your current location, as everywhere else in - # PowerShell, not the process's working directory (which cd and - # Set-Location never change). From a location that isn't on disk (HKLM:, - # say) that's the last file-system one. If that folder is gone, fall back - # to the process's working directory, as before. + # Relative paths mean your current location, not the process's working + # directory, which Set-Location never changes. From a location that isn't + # on disk (HKLM:) that's the last file-system one. If that folder is gone, + # the process's working directory. $here = $ExecutionContext.SessionState.Path.CurrentFileSystemLocation.ProviderPath if (-not [System.IO.Directory]::Exists($here)) { $here = $null } - # In a try/catch on purpose: a bare .NET call that throws does not - # reliably stop a PowerShell script, and a $null process left behind - # would be polled forever. The path handling is in here too: it throws - # on a path with characters Windows doesn't allow. + # In a try/catch: a .NET call that throws doesn't reliably stop the + # script, and a $null process would be polled forever. The path handling + # is in here too: it throws on characters Windows doesn't allow. try { if ($WorkingDirectory) { if ($null -ne $here) { $WorkingDirectory = Resolve-PkFileSystemPath -Path $WorkingDirectory -Here $here -CallerState $CallerState } @@ -1069,14 +1030,12 @@ function Stop-PkProcessTree { if ($taskkill.WaitForExit(10000)) { $killed = ($taskkill.ExitCode -eq 0) } } finally { $taskkill.Dispose() } } catch { } # taskkill missing or blocked: the fallback below still kills each process itself - # taskkill reports failure if any process in the trees couldn't be killed, - # even when it killed the program itself: one already gone (a child in - # another listed tree, or a `cmd /c` that ended on its own once taskkill - # killed its child, which it does first), or a child it wasn't allowed - # to kill. Count it as killing them if any listed process is now gone. - # A program that exited on its own in the moment since the check above - # counts as killed too. Of the kit's callers only Stop-PkTimedOutJob reads - # the result, and it only kills a program already past its time limit. + # taskkill fails if any process in the trees couldn't be killed, even when + # it killed the program: one already gone (a child in another listed tree, + # or a `cmd /c` that ended once taskkill killed its child, which it does + # first) or one it wasn't allowed to kill. So a listed process now gone + # counts as killed, even one that just exited on its own. Only + # Stop-PkTimedOutJob reads the result, and it only kills overdue programs. if (-not $killed) { foreach ($p in $alive) { if ($p.HasExited) { $killed = $true; break } } } @@ -1090,9 +1049,9 @@ function Stop-PkProcessTree { } function Stop-PkJobProcesses { - # The dispatcher's force-stop: every running job's process tree in one - # Stop-PkProcessTree call, rather than one taskkill per job. A custom - # starter's .Proc may not be a Process; those are skipped. + # The dispatcher's force-stop: every running job's tree in one + # Stop-PkProcessTree call. A custom starter's .Proc may not be a Process; + # those are skipped. param([System.Collections.Generic.List[hashtable]]$Jobs) $procs = foreach ($job in $Jobs) { if ($job['Proc'] -is [System.Diagnostics.Process]) { $job['Proc'] } } try { $null = Stop-PkProcessTree -Process @($procs) } catch { } # best effort; each job is still waited on and disposed after @@ -1113,10 +1072,9 @@ function Get-PkJobTimeLimit { } function Stop-PkTimedOutJob { - # Kills a dispatcher job's program tree once it passes the job's TimeLimit and - # sets TimedOut; the poll then sees it exited and it is finalized like any - # other. Only a .Proc that is a real Process is timed: anything else is left - # to the job's own -JobPollCompleted. + # Kills a dispatcher job's program tree once it passes its TimeLimit and + # sets TimedOut; the job is then finalized like any other. Only a .Proc + # that is a real Process is timed. param([hashtable]$Job) $proc = $Job['Proc'] $clock = $Job['Stopwatch'] @@ -1165,12 +1123,11 @@ function Wait-PkProcess { function Wait-PkRunningJobs { # The dispatch loop's pause: until a running program exits, the token is - # canceled, or TimeoutMs passes. Only programs that haven't exited are waited - # on: a -JobPollCompleted can keep saying "not done" after the exit, and the - # signaled handle would make every wait return at once and spin a core. - # A job with no real process, or past the 63 handles WaitAny can hold on an STA - # thread (one of them the token), is only noticed when the time is up. With - # nothing to wait on, a plain sleep. Same handle wrapping as Wait-PkProcess. + # canceled, or TimeoutMs passes. Exited programs aren't waited on: a + # -JobPollCompleted can keep saying "not done" after the exit, and the + # signaled handle would spin a core. A job with no real process, or past + # the 63 handles WaitAny holds on an STA thread (one is the token), is only + # noticed when the time is up. Same handle wrapping as Wait-PkProcess. param( [System.Collections.Generic.List[hashtable]]$Jobs, [System.Threading.CancellationToken]$CancelToken, @@ -1223,9 +1180,8 @@ function Complete-PkProcess { # .OUTPUTS # An object with ExitCode, Stdout, Stderr, StreamTimedOut and StreamError. - # Only -Job by position: with -Dispose a switch, the old `-Dispose $true` - # would otherwise hand its $true to -StreamTimeoutMs (1 ms) and cut the - # output off without an error. This way it's an error. + # Only -Job by position: otherwise `-Dispose $true` would bind $true to + # -StreamTimeoutMs (1 ms) and cut the output off without an error. [CmdletBinding(PositionalBinding = $false)] param( [Parameter(Mandatory, Position = 0)] @@ -1234,9 +1190,9 @@ function Complete-PkProcess { [switch]$Dispose ) $result = [ordered]@{ ExitCode = $null; Stdout = $null; Stderr = $null; StreamTimedOut = $false; StreamError = $null } - # A job started by a custom starter may have no read tasks, and a read - # can fail. Neither is worth an exception here - this runs inside - # JobFinalize, where a throw stops the whole dispatcher. + # A custom starter's job may have no read tasks, and a read can fail. + # Neither throws: this runs inside JobFinalize, where a throw stops the + # whole dispatcher. $tasks = [System.Collections.Generic.List[System.Threading.Tasks.Task]]::new() if ($null -ne $Job['StdoutTask']) { $tasks.Add($Job['StdoutTask']) } if ($null -ne $Job['StderrTask']) { $tasks.Add($Job['StderrTask']) } @@ -1290,14 +1246,11 @@ function Invoke-PkProcessWithTimeout { [int]$StreamTimeoutMs = 15000, [string]$WorkingDirectory = "", $OutputEncoding = $null, - # For a program that has to run BECAUSE of an abort (cleanup, a - # restore step): keep going even though the run is being aborted. [switch]$IgnoreAbort ) $job = Start-PkProcessCore -FilePath $FilePath -Arguments $Arguments -WorkingDirectory $WorkingDirectory -OutputEncoding $OutputEncoding -CallerState $PSCmdlet.SessionState try { - # Wait in short slices so Ctrl+C (or the run's cancel token) is - # noticed within a moment, not after the whole time limit. + # Short slices, so an abort or cancel is noticed within a moment. $waited = [System.Diagnostics.Stopwatch]::StartNew() $exited = $false $stopWhy = $null @@ -1309,9 +1262,9 @@ function Invoke-PkProcessWithTimeout { } if (-not $exited) { try { $null = Stop-PkProcessTree -Process $job['Proc'] } catch { } # best effort - the throw below still reports why - # Let the two output reads finish (the kill closes their pipes) - # before the process is disposed under them. Bounded, and any - # read error is irrelevant - the stop reason is what gets reported. + # Let the output reads finish (the kill closes their pipes) before + # the process is disposed under them. Bounded; a read error doesn't + # matter here. try { $null = [System.Threading.Tasks.Task]::WaitAll(@($job['StdoutTask'], $job['StderrTask']), 500) } catch { } throw ("'" + $FilePath + "' " + $stopWhy + " - killed") } @@ -1447,9 +1400,8 @@ function Invoke-PkProcessDispatch { # Invoke-PkProcessBatch has the same check, so its error names the batch. if ($pkTimeoutSeconds -lt 0) { throw "Invoke-PkProcessDispatch: -TimeoutSeconds must be 0 (no limit) or more." } - # Two pool threads per running process for its output reads - make sure - # they exist up front. For a shared budget, the slots free right now - # are the best available measure of its size. + # Size the thread pool up front. For a shared budget, its free slots now + # are the best measure of its size. $pkPoolSizeHint = 0 if ($pkMaxConcurrent -is [int]) { $pkPoolSizeHint = $pkMaxConcurrent } elseif ($pkMaxConcurrent -is [System.Threading.SemaphoreSlim]) { $pkPoolSizeHint = $pkMaxConcurrent.CurrentCount } @@ -1483,10 +1435,9 @@ function Invoke-PkProcessDispatch { $pkCancelToken = $pkOwnCts.Token } } else { - # The caller's own token: nothing would cancel it on Ctrl+C. Run on - # a linked source that an abort DOES cancel (and that still follows - # the caller's token). In a worker it also follows the run's token, - # so the run being canceled for any reason stops this dispatcher. + # Nothing cancels the caller's token on Ctrl+C, so run on a linked + # source an abort does cancel. In a worker it also follows the run's + # token. $pkLinkTokens = [System.Collections.Generic.List[System.Threading.CancellationToken]]::new() $pkLinkTokens.Add($pkCancelToken) if ($null -ne $global:PkCancelToken) { $pkLinkTokens.Add($global:PkCancelToken) } @@ -1510,14 +1461,11 @@ function Invoke-PkProcessDispatch { } function Add-PkDispatchGroupJobs { - # Builds one group's jobs, tags them with the group and queues them. - # Returns how many were queued. Its own function, not inline in the - # dispatch loop, so the group's job list dies with this call. Kept in the - # loop's frame it stayed alive for the whole run, and with it every - # finished job's output, even output the caller had already dropped. - # Nulling the variables in the loop didn't help once the kit had run many - # times in a session (most likely PowerShell's compiled code keeping its - # own references); a separate call did. + # Builds one group's jobs, tags them with the group and queues them; + # returns how many. Its own function so the group's job list dies with + # this call: in the dispatch loop's frame it kept every finished job's + # output alive for the whole run, and nulling the variables there didn't + # release it. param( [Parameter(Mandatory)][scriptblock]$JobBuilder, $GroupInfo, @@ -1535,9 +1483,8 @@ function Add-PkDispatchGroupJobs { $pkCallbackWarned = $CallbackWarned Remove-Variable -Name JobBuilder, GroupInfo, GroupKey, Pending, CallbackWarned -Scope Local $pkRawJobs = Invoke-PkCallback -PkCallback $pkJobBuilder -PkArgs @(,$pkGroupInfo) -PkName 'ProcessDispatch: -JobBuilder' -PkWarned $pkCallbackWarned - # Assigned inside each branch, NOT `$pkJobs = if (...) {...} else {...}`: - # captured through an if/else, a single hashtable gets - # enumerated into its key/value pairs. + # Assigned in each branch, not `$x = if ... else ...`: captured that way, + # a single hashtable is enumerated into its key/value pairs. if ($null -eq $pkRawJobs) { $pkRawJobList = @() } else { $pkRawJobList = @($pkRawJobs) } # Keep only real job hashtables: a builder that lets a stray # value escape (a HashSet.Add's True, say) must not crash the run. @@ -1616,10 +1563,9 @@ function Invoke-PkProcessDispatchCore { $pkStarting = $null # Callbacks that used continue/break, so each is only warned about once. $pkCallbackWarned = @{} - # Teardown is in a finally, guarded by this flag, not in a catch: when - # PowerShell stops the script (Ctrl+C outside Invoke-PkRun, the runspace - # engine force-stopping a worker) catch blocks are skipped and only - # finally blocks run. + # Teardown is in a finally, guarded by this flag: when PowerShell stops + # the script (Ctrl+C outside Invoke-PkRun, the runspace engine + # force-stopping a worker), catch blocks are skipped. $pkLoopDone = $false try { while ((-not $pkCancelToken.IsCancellationRequested) -and ` @@ -1635,10 +1581,8 @@ function Invoke-PkProcessDispatchCore { while ((-not $pkProviderExhausted) -and $pkActiveGroupCount -lt $pkMaxQueueAhead -and (-not $pkCancelToken.IsCancellationRequested)) { $pkGroupInfo = Invoke-PkCallback -PkCallback $pkGroupProvider -PkName 'ProcessDispatch: -GroupProvider' -PkWarned $pkCallbackWarned if ($null -eq $pkGroupInfo) { $pkProviderExhausted = $true; break } - # A fresh object per group, NOT the group value itself: two - # groups that compare equal (the same folder name, say) must not - # share one job count. Jobs carry this key as .GroupKey and the - # group value itself as .Group. + # A fresh key per group, not the group value: two groups that + # compare equal (the same folder name) mustn't share a job count. $pkGroupKey = [object]::new() $pkJobCount = Add-PkDispatchGroupJobs -JobBuilder $pkJobBuilder -GroupInfo $pkGroupInfo -GroupKey $pkGroupKey -Pending $pkPending -CallbackWarned $pkCallbackWarned $pkState = @{ GroupInfo = $pkGroupInfo; Outstanding = $pkJobCount; Completed = $false } @@ -1661,17 +1605,12 @@ function Invoke-PkProcessDispatchCore { $(if ($pkIsSharedBudget) { $pkMaxConcurrent.Wait(0) } else { $pkRunning.Count -lt $pkMaxConcurrent })) { $pkJob = $pkPending.Dequeue() try { - # Don't trust the starter's output shape: a starter that - # forgets to return the job (or leaks other output) would - # leave something un-pollable in $pkRunning and spin forever. - # The running job is always the caller's own job object, so - # the fields its builder set are still there at finalize - # time. If the starter output a DIFFERENT hashtable (a bare - # `Start-PkProcess ...` outputs its start result), fold that - # one's fields into the job rather than swapping the job out. - # A job object that has been round once already (JobFinalize - # put the SAME object back on the queue) still carries its - # old, disposed process - clear that before it starts again. + # The running job is always the caller's own object: a starter + # that forgets to return it, or leaks other output, would + # otherwise leave something un-pollable running. A different + # hashtable it outputs (a bare Start-PkProcess result) has its + # fields folded in. A job JobFinalize re-queued still carries + # its old, disposed process: cleared first. if ($pkJob['SlotReleased']) { $pkJob['Proc'] = $null; $pkJob['StdoutTask'] = $null; $pkJob['StderrTask'] = $null; $pkJob['Stopwatch'] = $null } @@ -1696,12 +1635,11 @@ function Invoke-PkProcessDispatchCore { $pkRunning.Add($pkJob) $pkStarting = $null } catch { - # Release BEFORE reporting: if reporting throws, the slot - # would otherwise leak (this job never reached $pkRunning, so - # the outer cleanup can't find it). $pkStarting is cleared - # first, so the teardown can't release the same slot again. - # A starter that started its program and then threw: nothing - # tracks that program now, so kill it before its slot goes back. + # Release before reporting: this job isn't in $pkRunning, so if + # reporting threw, nothing would release its slot. A program the + # starter started before throwing is killed first: nothing else + # tracks it. $pkStarting is cleared before the release, so the + # teardown can't release the slot again. if ($pkJob['Proc'] -is [System.Diagnostics.Process]) { try { $null = Stop-PkProcessTree -Process $pkJob['Proc'] } catch { } # best effort; the failed start is still reported below try { $pkJob['Proc'].Dispose() } catch { } @@ -1739,12 +1677,9 @@ function Invoke-PkProcessDispatchCore { try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } # a failed Dispose can't affect the result already taken if ($pkIsSharedBudget) { $null = $pkMaxConcurrent.Release() } $pkJob['SlotReleased'] = $true - # Whatever JobFinalize queued (a retry, follow-up steps) is counted - # into its group HERE, by looking at the queue - not by trusting a - # returned flag. A flag with nothing queued would hang the group - # forever; two follow-ups would let it finish early. - # Only when the queue actually grew - walking it on every finished - # job would cost a full pass over a large batch each time. + # Whatever JobFinalize queued (a retry, follow-ups) joins its group + # here, read off the queue rather than a returned flag. Only when the + # queue grew: walking it for every job is slow on a large batch. if ($pkPending.Count -gt $pkPendingBefore) { $pkQueuedIndex = 0 foreach ($pkQueued in $pkPending) { @@ -1774,12 +1709,10 @@ function Invoke-PkProcessDispatchCore { $pkStillRunning.Add($pkJob) } } - # A job that finished freed a slot: go straight round and refill it. - # Not on a shared budget, where a sibling dispatcher only looks for a - # free slot every 50 ms: refilling at once took every slot until this - # dispatcher's queue ran out, and the sibling got none. - # The wait's 50 ms cap is how soon an abort flag or a sibling's released - # slot is noticed, neither of which wakes it. + # A finished job freed a slot: refill it right away. Not on a shared + # budget, where a sibling dispatcher only looks every 50 ms and + # refilling at once starved it. The 50 ms cap is how soon an abort or a + # sibling's released slot is noticed: neither wakes the wait. $pkAnyFinished = ($pkStillRunning.Count -lt $pkRunning.Count) $pkRunning = $pkStillRunning @@ -1800,12 +1733,9 @@ function Invoke-PkProcessDispatchCore { try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } if ($pkIsSharedBudget) { $null = $pkMaxConcurrent.Release() } $pkJob['SlotReleased'] = $true - # Whatever JobFinalize queued (a retry, follow-up steps) is counted - # into its group HERE, by looking at the queue - not by trusting a - # returned flag. A flag with nothing queued would hang the group - # forever; two follow-ups would let it finish early. - # Only when the queue actually grew - walking it on every finished - # job would cost a full pass over a large batch each time. + # Whatever JobFinalize queued (a retry, follow-ups) joins its group + # here, read off the queue rather than a returned flag. Only when the + # queue grew: walking it for every job is slow on a large batch. if ($pkPending.Count -gt $pkPendingBefore) { $pkQueuedIndex = 0 foreach ($pkQueued in $pkPending) { @@ -1839,12 +1769,11 @@ function Invoke-PkProcessDispatchCore { if ($pkRunning.Count -gt 0) { [System.Threading.Thread]::Sleep(250) } } - # ---- BLOCK: mandatory final completion-check pass ---- - # A window-close can exit the loop above before a single poll, and - # a job can finish in the last 250ms before the deadline. One more - # pass so a finished job is never thrown away as "force-stopped". - # (keep in sync with the two blocks above, except the time limit: - # whatever is still running is force-stopped together just below) + # ---- BLOCK: final pass ---- + # A window close can end the loop above before any poll, and a job can + # finish just before the deadline: a finished job mustn't count as + # force-stopped. Keep in sync with the two blocks above, minus the time + # limit (whatever is left is force-stopped just below). if ($pkRunning.Count -gt 0) { $pkStillWaiting = [System.Collections.Generic.List[hashtable]]::new() foreach ($pkJob in $pkRunning) { @@ -1854,12 +1783,9 @@ function Invoke-PkProcessDispatchCore { try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } if ($pkIsSharedBudget) { $null = $pkMaxConcurrent.Release() } $pkJob['SlotReleased'] = $true - # Whatever JobFinalize queued (a retry, follow-up steps) is counted - # into its group HERE, by looking at the queue - not by trusting a - # returned flag. A flag with nothing queued would hang the group - # forever; two follow-ups would let it finish early. - # Only when the queue actually grew - walking it on every finished - # job would cost a full pass over a large batch each time. + # Whatever JobFinalize queued (a retry, follow-ups) joins its group + # here, read off the queue rather than a returned flag. Only when the + # queue grew: walking it for every job is slow on a large batch. if ($pkPending.Count -gt $pkPendingBefore) { $pkQueuedIndex = 0 foreach ($pkQueued in $pkPending) { @@ -1909,12 +1835,11 @@ function Invoke-PkProcessDispatchCore { $pkLoopDone = $true } finally { if (-not $pkLoopDone) { - # Best-effort teardown on the way out of an error or a stop; an - # error carries on up by itself. A job with SlotReleased set was - # already finalized or force-stopped, and its process disposed: - # left alone, since a disposed Process makes Stop-PkProcessTree - # throw part-way through. Same per-job steps as the abort wait's - # force-stop above; keep the two in step. + # Best-effort teardown after an error or a stop (an error carries + # on up by itself). Jobs with SlotReleased set are already done and + # disposed, and a disposed Process makes Stop-PkProcessTree throw + # part-way. Same per-job steps as the force-stop above; keep the + # two in step. $pkTearDown = [System.Collections.Generic.List[hashtable]]::new() foreach ($pkJob in $pkRunning) { if (-not $pkJob['SlotReleased']) { $pkTearDown.Add($pkJob) } } if ($null -ne $pkStarting -and -not $pkTearDown.Contains($pkStarting)) { $pkTearDown.Add($pkStarting) } @@ -1944,10 +1869,9 @@ function Invoke-PkProcessDispatchCore { $pkDispatchResult.Aborted = $pkCancelToken.IsCancellationRequested foreach ($pkState in $pkGroupStates.Values) { if (-not $pkState.Completed) { - # An aborted group still gets its callback - it's the only place - # the caller can credit the jobs that DID finish before the stop. - # Swallowed: one callback failing during teardown shouldn't cost - # every other group its result. + # An aborted group still gets its callback: it's where the caller + # credits the jobs that did finish. Swallowed, so one failing + # callback doesn't cost the other groups their results. try { $null = Invoke-PkCallback -PkCallback $pkGroupCompleteCallback -PkArgs @(,$pkState.GroupInfo) -PkName 'ProcessDispatch: -GroupCompleteCallback' -PkWarned $pkCallbackWarned } catch { } $pkState.Completed = $true $pkGroupResults.Add([pscustomobject][ordered]@{ GroupInfo = $pkState.GroupInfo; Completed = $false; Aborted = $true }) @@ -2055,9 +1979,8 @@ function Invoke-PkProcessBatch { # Checked once here, so a bad name fails the call instead of every command. $null = Resolve-PkEncoding -Encoding $pkOutputEncoding - # Invoke-PkProcessDispatch has the same check; this one names the batch. + # Invoke-PkProcessDispatch has the same two checks; these name the batch. if ($pkMaxConcurrent -lt 1) { throw ("Invoke-PkProcessBatch: -MaxConcurrent must be at least 1 (got " + $pkMaxConcurrent + ").") } - # Invoke-PkProcessDispatch has the same check; this one names the batch. if ($pkTimeoutSeconds -lt 0) { throw "Invoke-PkProcessBatch: -TimeoutSeconds must be 0 (no limit) or more." } $pkResults = [System.Collections.Generic.List[object]]::new() @@ -2186,10 +2109,8 @@ function Get-PkHandlerParamCount { } function Add-PkCrashedJob { - # A worker threw: count it, record it with its job on the run's outcome, - # announce it, and tell -JobCrashedHandler. Shared by the main loop and - # the abort drain. pk-prefixed parameters: when the kit was dot-sourced - # rather than a module, a handler built without .GetNewClosure() saw them. + # A worker threw: count it, record it with its job, announce it, and tell + # -JobCrashedHandler. Shared by the main loop and the abort drain. param( [Parameter(Mandatory)][System.Collections.Specialized.OrderedDictionary]$PkOutcome, [hashtable]$PkJob, @@ -2217,10 +2138,10 @@ function Add-PkCrashedJob { } function Add-PkJobErrors { - # A finished worker's non-terminating errors: print each, as always, and - # record it with its job on the run's outcome. Shared by the main loop and - # the abort drain. Called for a worker that crashed too: its error stream - # holds only the errors from before the one that stopped it. + # A finished worker's non-terminating errors: print each and record it + # with its job. Shared by the main loop and the abort drain. Called for a + # crashed worker too: its error stream holds only the errors before the + # one that stopped it. param( [Parameter(Mandatory)][System.Collections.Specialized.OrderedDictionary]$PkOutcome, [hashtable]$PkJob, @@ -2244,16 +2165,12 @@ function Receive-PkCompletedHandles { $stillActive = [System.Collections.Generic.List[hashtable]]::new() foreach ($h in $Handles) { if ($h.Handle.IsCompleted) { - # Not $results: when the kit was dot-sourced rather than a module, a - # -ResultHandler built without .GetNewClosure() saw this function's - # locals, and $results.Add($r) there grew the collection this loop - # walks, so it never ended. Same in Invoke-PkRunspaceJobs. $pkWorkerOutput = $null $pkCrash = $null try { try { $pkWorkerOutput = $h.PS.EndInvoke($h.Handle) } catch { $pkCrash = $_ } - # Same report as Invoke-PkRunspaceJobs' main loop: the errors - # it wrote, then the crash, in the order they happened. + # Same as Invoke-PkRunspaceJobs' main loop: its errors, then the + # crash, in the order they happened. Add-PkJobErrors -PkOutcome $Outcome -PkJob $h['Job'] -PkErrors $h.PS.Streams.Error if ($null -ne $pkCrash) { Add-PkCrashedJob -PkOutcome $Outcome -PkJob $h['Job'] -PkError $pkCrash -PkHandler $JobCrashedHandler -PkWhen " during abort drain" -PkCallbackWarned $CallbackWarned @@ -2401,11 +2318,9 @@ function Invoke-PkRunspaceJobs { [Alias('AbortTimeoutSeconds')] [int]$DrainTimeoutSeconds = 60, [string]$AbortWaitMessage, - # On unless turned off; see $checkSafety below. [switch]$CheckWorkerSafety ) - # A switch can't default to on without a lint warning, so "not passed" - # means on. A bare -CheckWorkerSafety (the v1 opt-in) still means on. + # Not passed means on: a switch can't default to on without a lint warning. $checkSafety = (-not $PSBoundParameters.ContainsKey('CheckWorkerSafety')) -or $CheckWorkerSafety.IsPresent if ($null -eq $JobParams -and $null -eq $NextJobProvider) { @@ -2416,9 +2331,8 @@ function Invoke-PkRunspaceJobs { } if ($MaxParallel -lt 1) { throw "Invoke-PkRunspaceJobs: -MaxParallel must be at least 1." } - # Accept a List, an array, or a single hashtable. A job list built with - # `foreach` collapses to ONE bare hashtable when there is only one job - - # that must work the same as two. + # A List, an array or a single hashtable: a one-job list built with + # foreach collapses to a bare hashtable. if ($null -ne $JobParams) { $normalizedJobs = [System.Collections.Generic.List[hashtable]]::new() if ($JobParams -is [hashtable]) { @@ -2447,8 +2361,8 @@ function Invoke-PkRunspaceJobs { if ($null -eq $ResultHandler) { $ResultHandler = { param($r) } } - # Which parameters the worker actually declares - passing one it doesn't - # declare fails the whole invoke with an unhelpful binding error. + # Passing a parameter the worker doesn't declare fails the whole invoke + # with an unhelpful binding error. $declaredParams = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase) if ($null -ne $WorkerScript.Ast.ParamBlock) { foreach ($p in $WorkerScript.Ast.ParamBlock.Parameters) { $null = $declaredParams.Add($p.Name.VariablePath.UserPath) } @@ -2519,19 +2433,16 @@ function Invoke-PkRunspaceJobs { $pool = [RunspaceFactory]::CreateRunspacePool(1, $MaxParallel, $iss, $Host) $pool.Open() - # Each job starts in the caller's current location, the folder - # Start-PkProcess defaults to as well. The real folder, not a drive - # name: a New-PSDrive drive doesn't exist in the worker. + # Each job starts in the caller's current location: the real folder, + # since a New-PSDrive drive doesn't exist in the worker. $workerLocation = $ExecutionContext.SessionState.Path.CurrentFileSystemLocation.ProviderPath - # Every job runs this: move to that folder, then run the worker in a - # scope of its own with the job's values. Set per job because - # runspaces are reused: a job that changed folder would otherwise move - # the next one too. One script rather than a Set-Location statement - # ahead of the worker: with a second statement, a pool runspace's - # pipeline hadn't completed after 10 s in testing, on 5.1 and 7. The - # prefix shares the worker's first line, so error line numbers still - # match its code. If the folder is gone by the time a job starts, the - # job runs wherever its runspace already is, without an error per job. + # Every job runs this: move to that folder, then run the worker in its + # own scope with the job's values. Per job, because runspaces are + # reused. One script, not a Set-Location statement before the worker: + # with a second statement a pool runspace's pipeline never completed + # (5.1 and 7). The prefix shares the worker's first line, so error line + # numbers match its code. If the folder is gone, the job runs wherever + # its runspace is. $jobScript = 'param([string]$PkJobLocation, [hashtable]$PkJobValues) if ($PkJobLocation) { Set-Location -LiteralPath $PkJobLocation -ErrorAction SilentlyContinue }; & {' + $WorkerScript.ToString() + "`n} @PkJobValues" @@ -2592,17 +2503,12 @@ function Invoke-PkRunspaceJobs { $stillPending = [System.Collections.Generic.List[hashtable]]::new() foreach ($h in $handles) { if ($h.Handle.IsCompleted) { - # Not $results: when the kit was dot-sourced rather than a - # module, a -ResultHandler or -JobCrashedHandler built without - # .GetNewClosure() saw this function's locals, and - # $results.Add($r) there grew the collection this loop - # walks, so it never ended. Same in Receive-PkCompletedHandles. $pkWorkerOutput = $null $pkCrash = $null try { try { $pkWorkerOutput = $h.PS.EndInvoke($h.Handle) } catch { $pkCrash = $_ } - # Same report as Receive-PkCompletedHandles: the errors - # it wrote, then the crash, in the order they happened. + # Same as Receive-PkCompletedHandles: its errors, then + # the crash, in the order they happened. Add-PkJobErrors -PkOutcome $outcome -PkJob $h['Job'] -PkErrors $h.PS.Streams.Error if ($null -ne $pkCrash) { # The worker itself threw - there is no result, so @@ -2654,16 +2560,13 @@ function Invoke-PkRunspaceJobs { } catch { } } if ($null -ne $pool) { - # A worker stuck inside a .NET call can't be stopped, and - # Dispose() would sit and wait for it. Close in the background - # and move on; the abandoned worker ends when it returns or - # the process exits. + # A worker stuck in a .NET call can't be stopped, and Dispose() + # would wait for it, so close in the background. if ($stillRunning) { - # An abandoned worker does NOT stop when its blocking call - # returns - it can run on to its last line. Cancel the token - # so a worker that checks Test-PkCancelRequested before an - # irreversible step still gets told, even on a non-abort - # exit (one of your callbacks threw). + # An abandoned worker can run on to its last line once its call + # returns. Cancel the token so one that checks + # Test-PkCancelRequested before an irreversible step is told, + # even on a non-abort exit (one of your callbacks threw). try { $Cts.Cancel() } catch { } try { $null = $pool.BeginClose($null, $null) } catch { } } @@ -2685,17 +2588,15 @@ function Invoke-PkRunspaceJobs { # ============================================================================== function Get-PkCallerFunction { - # Looks function names up in the CALLER's session state, from the caller's - # current scope: a script's own functions (.\script.ps1), functions - # defined inside the calling function, a calling module's private ones. - # Get-Item Function: here would search this module (plus global), and - # SessionState.InvokeCommand.GetCommand only finds global functions. - # An unbound scriptblock (made from text) run with InvokeScript(SessionState) - # runs in that session state, dot-sourced into the caller's current scope. - # So the work happens inside & { } and the names are embedded as quoted - # literals: nothing is assigned in the caller's scope. - # With -FollowAlias, an alias is followed to the function it points at. - # Returns a hashtable: requested name -> FunctionInfo (missing = not found). + # Looks function names up in the caller's session state, from its current + # scope: a script's own functions, functions defined inside the calling + # function, a calling module's private ones. Get-Item Function: here would + # search this module, and InvokeCommand.GetCommand only finds global ones. + # A scriptblock made from text and run with InvokeScript(SessionState) is + # dot-sourced into the caller's scope, so the work happens inside & { } + # with the names as quoted literals: nothing is assigned there. + # -FollowAlias follows an alias to its function. Returns a hashtable: + # name -> FunctionInfo (missing = not found). param( [Parameter(Mandatory)][System.Management.Automation.SessionState]$SessionState, [string[]]$Name, @@ -2740,15 +2641,14 @@ function Get-PkCommandKind { # or $null): 'Function' (one of your session's functions - must be # shared), 'UnloadableModuleFunction' (from a module a worker can't # autoload), or 'Other' (cmdlet, program, autoloadable module function, - # or unknown). Works from the Alias: and Function: drives rather than - # Get-Command: Get-Command on a name that doesn't resolve searches every - # module on $env:PSModulePath, and the check runs on every - # Invoke-PkRunspaceJobs call. + # or unknown). Uses the Alias: and Function: drives, not Get-Command, + # which searches every module on $env:PSModulePath for a name that + # doesn't resolve, on every Invoke-PkRunspaceJobs call. param([System.Management.Automation.FunctionInfo]$Fn) if ($null -eq $Fn) { return 'Other' } if ([string]::IsNullOrEmpty($Fn.ModuleName)) { return 'Function' } - # The kit's own: a worker gets only the worker-safe ones, so treat the - # rest like a function of yours (needs listing), as before the kit was a module. + # The kit's own: a worker gets only the worker-safe ones, so the rest are + # treated like your own functions. if ($Fn.ModuleName -eq 'ParallelKit') { return 'Function' } # A module function (Get-FileHash, say) autoloads in a worker like a # cmdlet does - but only if the module can be found by name. One imported @@ -2802,10 +2702,8 @@ function Test-PkHoldsScriptBlock { } } catch { # A collection other threads are changing (a results sink shared by - # running workers) can't be enumerated safely; PowerShell may wrap - # the "collection was modified" error, so catch it whatever its type. - # Skip the value rather than fail the run: such sinks hold results, - # not code. + # workers) can't be enumerated safely, and PowerShell may wrap the + # error, so catch anything. Skip it: such sinks hold results, not code. return $false } return $false @@ -2946,19 +2844,14 @@ function Test-PkWorkerSafetyCore { Text = ($t.Where + " (line " + $cv.Extent.StartLineNumber + ") casts a scriptblock to the delegate [" + $cv.Type.TypeName.Name + "] - this can hang or crash a worker") }) } - # The implicit form: a scriptblock literal passed as an argument to - # a method or constructor - $dict.GetOrAdd($k, { ... }), - # [System.Lazy[object]]::new({ ... }). No cast is written, but .NET - # receives a delegate all the same. .ForEach()/.Where() are - # PowerShell's own and take real scriptblocks, so they're fine. - # The same goes for a variable assigned a scriptblock literal (or - # { }.GetNewClosure()) anywhere in this code, then passed as - # $lazy = [System.Lazy[object]]::new($factory). By name, not by flow: - # a variable reassigned to something else in between is still flagged. - # Methods that only store a value in a collection ($list.Add($sb), - # $map.Add($k, $sb), $queue.Enqueue($sb)) aren't flagged: storing a - # scriptblock makes no delegate. Not caught as a result: a collection - # typed to hold delegates (List[Func[object]]), where .Add converts. + # The implicit form: a scriptblock literal, or a variable assigned one + # ({ }.GetNewClosure() included) anywhere in this code, passed to a + # method or constructor: $dict.GetOrAdd($k, { }), + # [System.Lazy[object]]::new($factory). .NET gets a delegate all the + # same. Matched by variable name, not flow. .ForEach()/.Where() take + # real scriptblocks. Storage methods ($list.Add($sb), + # $queue.Enqueue($sb)) make no delegate and aren't flagged, so a + # collection typed to hold delegates (List[Func[object]]) isn't caught. $storageMethods = @('Add', 'AddRange', 'Insert', 'Enqueue', 'Push', 'TryAdd', 'Contains', 'Remove', 'IndexOf', 'set_Item') $scriptBlockVars = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase) foreach ($assign in $ast.FindAll({ param($node) $node -is [System.Management.Automation.Language.AssignmentStatementAst] }, $true)) { @@ -2997,11 +2890,9 @@ function Test-PkWorkerSafetyCore { # Public functions (the ones README.md documents) # ============================================================================== -# Imported globally (Import-Module -Global below): a .GetNewClosure() block -# can only see global functions, so this is what lets the batch's own -# callbacks, and yours, call them however your script was started. -# The same list is $publicFunctions in ParallelKit.Harness.ps1 (which checks -# each has Get-Help help and is visible); keep the two in step. +# Imported globally: a .GetNewClosure() block only sees global functions, +# so this lets the batch's callbacks, and yours, call them. The same list is +# $publicFunctions in ParallelKit.Harness.ps1; keep the two in step. Export-ModuleMember -Function @( 'Set-PkMessageHandler', 'Write-PkMessage', 'Initialize-ParallelKit', 'Register-PkCancellationSource', 'Unregister-PkCancellationSource', 'Reset-PkAbortState', 'Request-PkAbort', 'Test-PkAbortSignal', 'Test-PkCancelRequested', From 3fb5b246a188d0b2531129ce0e837470e37a9f11 Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:37:53 -0500 Subject: [PATCH 3/7] Prune comments in the harness and stress tests Drops references to behavior and bugs that no longer exist ("used to", "before the fix") and shortens the rest. No test changes. Co-Authored-By: Claude Opus 5.5 --- ParallelKit.Harness.ps1 | 55 +++++++++++++++-------------------- Testing/cmdlet-hang/inner.ps1 | 2 +- 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index aaa036e..29238b4 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -593,7 +593,6 @@ Write-Host "=== L: message handler ===" -ForegroundColor Cyan Reset-Harness Write-PkMessage "main-thread message" -Level Warn Test-Assert ($script:messages.Count -eq 1 -and $script:messages[0].Level -eq 'Warn') "on the main thread, Write-PkMessage goes straight to the handler" -# Called from inside a closure, where a $script:-based handler would have vanished. $closure = { Write-PkMessage "from a closure" }.GetNewClosure() & $closure Test-Assert ($script:messages.Count -eq 2) "the handler is still found when called from inside a .GetNewClosure()'d block" @@ -977,10 +976,9 @@ try { [System.Threading.Thread]::Sleep(300) Test-Assert ($threw -and $ran.Count -eq 0) ("a bad key in -JobParams is reported before any worker starts (workers run: " + $ran.Count + ")") -# Callbacks built WITHOUT .GetNewClosure() see the kit's locals by dynamic -# scope. A caller's $results.Add($r) in one used to land in the kit's own -# worker-output collection, which the kit was looping over: an endless loop. -# The guard throws instead of hanging the harness if that comes back. +# A callback built without .GetNewClosure() must add to the caller's +# $results, not to a collection of the kit's: if it were the one the kit +# loops over, the loop would never end. The guard throws instead of hanging. Reset-Harness $results = [System.Collections.Generic.List[object]]::new() $loopGuard = @{ Calls = 0 } @@ -1021,8 +1019,7 @@ try { $drained = @($script:messages | Where-Object { $_.Message -like '*Waiting for current jobs*' }).Count -eq 1 Test-Assert ($null -eq $threw -and $loopGuard.Calls -eq 2 -and $results.Count -eq 2 -and $drained) ("a -ResultHandler without a closure doesn't loop during an abort drain either (calls=" + $loopGuard.Calls + ", count=" + $results.Count + ", drained=" + $drained + ", threw=" + $threw + ")") -# A -JobCrashedHandler without a closure used to find the kit's $null -# $results instead of the caller's, and the error was swallowed. +# A -JobCrashedHandler without a closure sees the caller's $results. Reset-Harness $results = [System.Collections.Generic.List[object]]::new() $null = Invoke-PkRunspaceJobs -MaxParallel 1 -JobParams (New-JobList -Jobs @(@{ Id = 1 })) -WorkerScript { param([int]$Id) throw "x" } -JobCrashedHandler { param($e) $results.Add($e) } @@ -1259,10 +1256,8 @@ $stale = Invoke-PkProcessWithTimeout -FilePath $selfExe -Arguments ('-NoProfile Test-Assert ($stale.Stdout -like '*REFUSED:*older version of the kit*start a new one*') "loading the kit over an older copy's types is refused with a clear message" # Invoke-PkRun keeps the PC from idle-sleeping only while it runs. Checked -# on this thread itself: SetThreadExecutionState returns the thread's -# previous state, so re-setting the same value reads it back. (The -# system-wide flag can't tell us anything - any other program keeping the -# PC awake already holds it.) +# on this thread: SetThreadExecutionState returns the thread's previous +# state. The system-wide flag says nothing, since other programs may hold it. if (-not ([System.Management.Automation.PSTypeName]'PkThreadStateProbe').Type) { Add-Type -TypeDefinition 'using System; using System.Runtime.InteropServices; public static class PkThreadStateProbe { [DllImport("kernel32.dll")] public static extern uint SetThreadExecutionState(uint f); }' } @@ -1410,8 +1405,7 @@ $killedSeveral = Stop-PkProcessTree -Process @($several | ForEach-Object { $_.Pr Test-Assert ($killedSeveral -and (Get-HarnessPingCount 45) -eq 0 -and (Get-HarnessPingCount 46) -eq 0 -and (Get-HarnessPingCount 47) -eq 0 -and @($several | Where-Object { -not $_.Proc.HasExited }).Count -eq 0) "Stop-PkProcessTree kills several programs and their children in one call" foreach ($j in $several) { $j.Proc.Dispose() } # A parent and its own child in the same call: both die, and the call says -# it killed them. (The case this guards - taskkill failing for one PID that -# exited a moment before - is a race this can't set up on demand.) +# it killed them. Section AJ covers taskkill failing for part of a tree. $parentJob = Start-PkProcess -FilePath $cmdExe -Arguments (& $nestedPing 48) $childProc = $null $waitChild = [System.Diagnostics.Stopwatch]::StartNew() @@ -1948,7 +1942,7 @@ $cleanWorker = { EAP = [string]$ErrorActionPreference Data = (Get-Content -LiteralPath '.\data.txt' -ErrorAction SilentlyContinue) } - # Everything that used to carry over to the next job on the same runspace. + # Everything that could carry over to the next job on the same runspace. Set-Location -LiteralPath $env:SystemRoot $pkHarnessLeak = "from job " + $Id $ErrorActionPreference = 'Stop' @@ -1988,7 +1982,7 @@ Write-Host "=== Y: output that's been dropped is freed during the run ===" -Fore # handled shouldn't have the kit keep it alive until the run ends. Weak # references show what a full GC can free while the last job is finishing. # A few of the most recent jobs can still be referenced from the dispatch -# loop (5-7 at -MaxConcurrent 4, however long the run); before the fix all were. +# loop (5-7 at -MaxConcurrent 4, however long the run). Reset-Harness $chatty = @(1..30 | ForEach-Object { @{ FilePath = $cmdExe; Arguments = ('/c for /l %i in (1,1,1500) do @echo ' + ('x' * 100)) } }) $batchWeak = [System.Collections.Generic.List[System.WeakReference]]::new() @@ -2691,8 +2685,7 @@ if ($compiled) { # cmd.exe's special characters. A .bat/.cmd program runs through cmd.exe, # which acts on & | < > ^ outside quotes and splits batch arguments at , ; - # so an element holding one is quoted even without a space. 'a&ver' used to - # reach a batch file as 'a' and run ver. + # so an element holding one is quoted even without a space. $special = @('a&b', 'a|b', 'ab', 'a^b', '(x)', 'a,b', 'a;b') Test-Assert ((ConvertTo-PkArgumentString $special) -eq '"a&b" "a|b" "ab" "a^b" "(x)" "a,b" "a;b"') "an element with a cmd.exe special character is quoted" Test-Assert ((Get-BatchArgvText $special) -eq '[a&b][a|b][ab][a^b][(x)][a,b][a;b]') "and an ordinary program still gets it exactly" @@ -2707,13 +2700,12 @@ if ($compiled) { } try { [System.IO.File]::Delete($batFile) } catch { } # temp file only; best effort - # A quote inside a list element for a .bat/.cmd program. cmd.exe reads the - # command line first and counts every quote, \" included, so with \" an - # element like a" & & " ran that command. The kit writes "" for - # those programs instead. The batch file itself does nothing, so only - # cmd.exe's own reading of the command line could create the marker. The - # marker is a bare name in the working folder, so a space in %TEMP% can't - # make the injected command write somewhere else and the check pass anyway. + # A quote in a list element for a .bat/.cmd: cmd.exe counts every quote, + # \" included, so with \" an element like a" & & " runs that + # command; the kit writes "" instead. The batch file does nothing, so only + # cmd.exe's reading of the command line could create the marker. It's a + # bare name in the working folder, so a space in %TEMP% can't send it + # elsewhere and let the check pass anyway. $injDir = Join-Path $env:TEMP ("pk-inj-" + [guid]::NewGuid().ToString("N")) $null = New-Item -ItemType Directory -Path $injDir $injMarker = Join-Path $injDir 'pk-marker.txt' @@ -2756,10 +2748,10 @@ if ($compiled) { # A string is still exactly as written. Test-Assert ((Get-BatchArgvText '"C:\My Files\a.png" out.jxl') -eq $spacedText) "a string is still passed exactly as written" - # The documented change: list elements are values, not pieces of a command line. + # List elements are values, not pieces of a command line. Test-Assert ((Get-BatchArgvText @('"two words"', 'x')) -eq '["two words"][x]') "quotes already in a list element reach the program as quotes" - # The join used to follow $OFS. + # The join doesn't follow $OFS. $OFS = ',' try { $text = Get-BatchArgvText $spaced } finally { Remove-Variable -Name OFS } Test-Assert ($text -eq $spacedText) ("`$OFS doesn't change a list (" + $text + ")") @@ -2772,7 +2764,7 @@ if ($compiled) { try { $null = Invoke-PkProcessWithTimeout -FilePath $argvExe -Arguments @{ a = 1 } } catch { $thrown = [string]$_ } Test-Assert ($thrown -like '*Arguments must be*') "and a clear error from the direct functions" - # A list inside the list used to become one argument, joined with $OFS. + # A list inside the list is an error, not one argument. Reset-Harness $nestedBatch = Invoke-PkProcessBatch -Commands @(@{ FilePath = $argvExe; Arguments = @('a', @('b', 'c')) }) -MaxConcurrent 1 Test-Assert ($nestedBatch.Results[0].StartError -like '*element 2 is itself a list*' -and $null -eq $nestedBatch.Results[0].ExitCode) ("a list inside an Arguments list is that command's StartError (" + $nestedBatch.Results[0].StartError + ")") @@ -2781,7 +2773,7 @@ if ($compiled) { Test-Assert ($thrown -like '*element 2 is itself a list*') "and ConvertTo-PkArgumentString refuses one too, a hashtable included" # File and folder objects are their full path. On 5.1 a FileInfo from - # Get-ChildItem casts to its bare name, so the program got 'a b.txt'. + # Get-ChildItem casts to its bare name. $fsDir = Join-Path $env:TEMP ("pk fs " + [guid]::NewGuid().ToString("N")) $null = New-Item -ItemType Directory -Force -Path $fsDir [System.IO.File]::WriteAllText((Join-Path $fsDir 'a b.txt'), 'x') @@ -3247,7 +3239,7 @@ Test-Assert ($null -ne $tkChild -and $tkKilled -and $tkStart.Proc.HasExited) ("S $tkStart.Proc.Dispose() if ($null -ne $tkChild) { $tkChild.Dispose() } -# The symptom: a job killed for its time limit showed TimedOut = $false. +# And a job killed for its time limit has TimedOut = $true. $tkFeed = @{ Given = $false } $tkLog = [System.Collections.Generic.List[object]]::new() $null = Invoke-PkProcessDispatch -MaxConcurrent 1 -TimeoutSeconds 1 ` @@ -3268,7 +3260,7 @@ Write-Host "" Write-Host "=== AK: 3.0 parameter changes ===" -ForegroundColor Cyan # Complete-PkProcess -Dispose is a switch, and only -Job binds by position, so -# the old -Dispose $true is an error instead of a 1 ms -StreamTimeoutMs. +# -Dispose $true is an error instead of a 1 ms -StreamTimeoutMs. Reset-Harness $apiStarted = Start-PkProcess -FilePath $cmdExe -Arguments '/c echo pk-api' $null = $apiStarted.Proc.WaitForExit(10000) @@ -3298,8 +3290,7 @@ $null = Invoke-PkRunspaceJobs -MaxParallel 1 -JobParams (New-JobList -Jobs @(@{ $apiLevels = (@($script:messages | Where-Object { $_.Message -eq 'pk-level-worker' } | ForEach-Object { $_.Level }) -join ',') Test-Assert ($apiLevels -ceq 'Warn') ("and the same from a worker (" + $apiLevels + ")") -# The dispatcher's -PollCallback, the runspace engine's name for the same -# every-pass role. Its old name, -AbortPollCallback, still works. +# The dispatcher's -PollCallback, and its alias -AbortPollCallback. Reset-Harness foreach ($apiName in 'PollCallback', 'AbortPollCallback') { $apiFeed = @{ Given = $false; Polls = 0 } diff --git a/Testing/cmdlet-hang/inner.ps1 b/Testing/cmdlet-hang/inner.ps1 index be34852..9dbb126 100644 --- a/Testing/cmdlet-hang/inner.ps1 +++ b/Testing/cmdlet-hang/inner.ps1 @@ -78,7 +78,7 @@ for ($n = 0; $n -lt $count; $n++) { # JobParamCmdlet - cmdlets that take no scriptblock # JobParamWhere - Where-Object/ForEach-Object with an inner scriptblock # JobParamText - control: the JobParamWhere text, [scriptblock]::Create'd -# in the worker (the proposed workaround) +# in the worker (the README's workaround) $jobFns = @{ JobParamPlain = { param($i) $n = 0; for ($j = 0; $j -lt 50; $j++) { $n += $j }; $n + $i } JobParamCmdlet = { param($i) $null = Get-Date; $null = Test-Path -LiteralPath $env:SystemRoot; $null = 1 | Out-Null; $i } From f005132f349531eb4042da6f3743365f1124ad3d Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:44:25 -0500 Subject: [PATCH 4/7] README: bare names only find .exe files; callbacks in a worker can see pk variables A bare FilePath only finds an .exe: Windows doesn't use PATHEXT there, so a .cmd wrapper such as npm has to be written as npm.cmd. And inside a worker runspace, a plain callback can also see the kit's own variables, all pk-prefixed, so the README no longer says it never sees the kit's internals. Co-Authored-By: Claude Opus 5.5 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1252486..4d0734b 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,7 @@ Long parameter lists in these examples use splatting (`@batchParams`) instead of Each command is a hashtable with `FilePath`, `Arguments` and optionally `WorkingDirectory`, plus any keys of your own. `Arguments` is either one string, passed to the program exactly as written, or a list, where each element is one argument: `Arguments = @($in, $out)` works with spaces in the paths. A list is quoted the way `ConvertTo-PkArgumentString @(...)` quotes it, so the program gets exactly that list back, spaces, quotes and trailing backslashes included, and `$null` elements are left out. A file or folder object from `Get-ChildItem` or `Get-Item` is passed as its full path. An element that's itself a list is an error, not one argument. Quotes inside an element are passed on as quote characters, so don't pre-quote list elements. It follows the rules most Windows programs use; `cmd.exe /c` parses its own way, so give it one string. A `.bat` or `.cmd` program runs through `cmd.exe` too. A list element holding `&`, `|`, `<`, `>`, `^`, `(`, `)`, `,` or `;` is quoted, so such a program gets it as one argument. What quoting can't cover there: `cmd.exe` expands `%NAME%` in an argument, quoted or not, when a variable by that name exists; and it splits batch arguments at `=`, which isn't quoted because some programs read `NAME=value` from the command line themselves. A `"` inside an element is written `""` for these programs, so `cmd.exe` can't be made to run anything else, and a batch file that passes `%*` on to another program still delivers it as one `"`. An element holding a line break can't be passed to them at all, so it's an error. All this needs the list form: a string, including one from `ConvertTo-PkArgumentString`, is passed as written. Programs run hidden, with their output captured. -Programs run in your current location (`Get-Location`) unless a command says otherwise, and a relative `WorkingDirectory`, or a `FilePath` with a folder in it like `.\tools\cjxl.exe`, is relative to that location too. Drives PowerShell knows, like `~` or one made with `New-PSDrive`, work in both. A bare name like `cjxl.exe` is found the way Windows finds programs, mostly on `PATH`. A `.bat` or `.cmd` program can't run in a network-share (`\\server\share`) folder: `cmd.exe` falls back to `C:\Windows` there, so give such commands a local `WorkingDirectory`. +Programs run in your current location (`Get-Location`) unless a command says otherwise, and a relative `WorkingDirectory`, or a `FilePath` with a folder in it like `.\tools\cjxl.exe`, is relative to that location too. Drives PowerShell knows, like `~` or one made with `New-PSDrive`, work in both. A bare name like `cjxl.exe` is found the way Windows finds programs, mostly on `PATH`. Only an `.exe` is found without its extension (Windows doesn't use `PATHEXT` here), so a `.cmd` wrapper such as `npm` has to be written out: `npm.cmd`. A `.bat` or `.cmd` program can't run in a network-share (`\\server\share`) folder: `cmd.exe` falls back to `C:\Windows` there, so give such commands a local `WorkingDirectory`. `Invoke-PkProcessBatch` returns an object with `Results`, `Aborted` and `ForceKilledCount`. Each result is an object with `Command`, `ExitCode`, `Stdout`, `Stderr`, `StreamTimedOut`, `StreamError`, `StartError`, `Aborted` and `TimedOut`, every field always present, so `$batch.Results | Format-Table` reads well: @@ -255,7 +255,7 @@ All of these callbacks run on whichever thread called the dispatcher: the main t - An exception from one of your callbacks stops the dispatcher: running processes are killed and the exception is rethrown. - Returns an object with `GroupResults`, `Aborted`, `ForceKilledCount` and `JobStarterFailedCount`. `GroupResults` holds one object with `GroupInfo`, `Completed` and `Aborted` per group, where `GroupInfo` is the group value. -Callbacks: a plain `{ ... }` callback sees your variables and functions from where you called the kit, never the kit's own internals, so `.GetNewClosure()` isn't needed for that, inside a worker runspace or out. Keep any state a callback changes in a hashtable (`$state = @{ Count = 0 }`, then `$state.Count++`). A plain variable assigned inside a callback doesn't stick between calls. A `.GetNewClosure()` block can't see functions your script defines when the script is run from a prompt (`.\script.ps1`); the kit's own functions always work, but define any of yours that a closure calls as `function global:Get-Thing { ... }`. +Callbacks: a plain `{ ... }` callback sees your variables and functions from where you called the kit, not the kit's internals, so `.GetNewClosure()` isn't needed for that, inside a worker runspace or out. The one exception is inside a worker, where it can also see the kit's own variables, which all start with `pk`, so don't give yours names starting with `pk`. Keep any state a callback changes in a hashtable (`$state = @{ Count = 0 }`, then `$state.Count++`). A plain variable assigned inside a callback doesn't stick between calls. A `.GetNewClosure()` block can't see functions your script defines when the script is run from a prompt (`.\script.ps1`); the kit's own functions always work, but define any of yours that a closure calls as `function global:Get-Thing { ... }`. To leave any callback early, use `return`. PowerShell sends a `continue` or `break` to the nearest loop, which for a callback is one of the kit's own, so the kit catches them and treats them as `return`, with a warning (once per callback per call, or once per message handler). A labeled `break` to a loop of yours (`break outer`) does leave the kit's call: it stops there, kills the programs it started and returns nothing. From 7252f5aeae2ae93e62a92745fd8ad3e05d5a4ee3 Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:55:07 -0500 Subject: [PATCH 5/7] Bring the help text and README up to date with 3.0 Header says Version 3.0 and names PowerShell 7. Help and README now cover what 3.0 changed: bare names only find .exe files, a tree kill counts even if part of the tree survives, a third Ctrl+C still kills the engines' programs, callbacks should leave with return, the dispatcher's output rule has its exceptions, and batch-file quoting needs the list form. Examples pass Arguments as a list. The README's argument rules are split into a list, and it now says it describes 3.0. Co-Authored-By: Claude Opus 5.5 --- ParallelKit.ps1 | 24 +++++++++++++++--------- README.md | 27 ++++++++++++++++++--------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index fb61916..2adaaee 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -1,7 +1,8 @@ # ParallelKit - parallel runspace jobs, bounded external-process dispatch, and -# graceful Ctrl+C/window-close handling for Windows PowerShell 5.1. +# graceful Ctrl+C/window-close handling for Windows PowerShell 5.1 and +# PowerShell 7. # -# Version 2.3 - https://github.com/HalRiveria/PowerShell-ParallelKit +# Version 3.0 - https://github.com/HalRiveria/PowerShell-ParallelKit # Copyright (c) 2026 HalRiveria. MIT License: keep this notice with any copy; # the full text is in LICENSE in the repository. # @@ -355,6 +356,7 @@ function Set-PkMessageHandler { # The handler gets ($Message, $Level), where Level is Info, Warn or Error, and # always runs on the main thread, including for messages from worker code. If it # throws, the message is shown on the console instead and the run carries on. + # Leave it early with return: continue and break end it too, but warn once. # .PARAMETER Handler # { param($Message, $Level) ... }, or $null to go back to the default (console). # .EXAMPLE @@ -658,7 +660,8 @@ function Invoke-PkRun { # and whatever is left is then stopped. A long step of your own in here must # check Test-PkAbortSignal now and then and stop when it's true, or Ctrl+C will # seem to do nothing until that step ends. Pressing Ctrl+C again, or closing the - # window, skips the wait; a third press stops the script as Ctrl+C normally does. + # window, skips the wait; a third press stops the script as Ctrl+C normally does, + # and the kit's engines still kill the programs they started. # # For the length of the run it also keeps the PC from idle-sleeping and turns off # the console's QuickEdit mode. Whatever the block outputs is passed through. @@ -906,7 +909,8 @@ function Start-PkProcess { # The program to run. A path with a folder in it is read as PowerShell reads # paths: .\tool.exe and bin\tool.exe are relative to your current location, and # drives like Data:\ or ~ work. A bare name (tool.exe) is found the way Windows - # finds programs, mostly on PATH. + # finds programs, mostly on PATH. Only an .exe is found without its extension: + # write a .cmd wrapper out in full (npm.cmd). # .PARAMETER Arguments # Its command line: one string, passed exactly as written, or a list, each # element one argument, quoted for you (see ConvertTo-PkArgumentString). @@ -1007,8 +1011,9 @@ function Stop-PkProcessTree { # .PARAMETER Process # The program(s), such as the Proc that Start-PkProcess returns. # .OUTPUTS - # [bool] For one program, $true if this call killed it and $false if it - # had already exited. For several, $true if this call killed any of them. + # [bool] For one program, $true if this call killed it, even if part of its + # tree couldn't be killed, and $false if it had already exited. For several, + # $true if this call killed any of them. param( [Parameter(Mandatory)] [AllowEmptyCollection()] @@ -1289,8 +1294,9 @@ function Invoke-PkProcessDispatch { # .DESCRIPTION # Groups are pulled one at a time from -GroupProvider; -JobBuilder turns each # into job hashtables; jobs run up to -MaxConcurrent at once across all groups; - # -GroupCompleteCallback fires when a group's last job finishes. Anything your - # callbacks output is discarded. + # -GroupCompleteCallback fires when a group's last job finishes. Apart from the + # return values described below, anything your callbacks output is discarded. + # Leave a callback early with return: continue and break end it too, but warn. # # The dispatcher keeps its own state on each job hashtable. Don't use these keys # for anything else: GroupKey, SlotReleased, Proc, StdoutTask, StderrTask, @@ -1950,7 +1956,7 @@ function Invoke-PkProcessBatch { # ExitCode, Stdout, Stderr, StreamTimedOut, StreamError, StartError, Aborted and # TimedOut, in the order the programs finished. # .EXAMPLE - # $commands = foreach ($f in Get-ChildItem *.png) { @{ FilePath = 'cjxl.exe'; Arguments = (ConvertTo-PkArgumentString @($f.FullName, "$($f.FullName).jxl")) } } + # $commands = foreach ($f in Get-ChildItem *.png) { @{ FilePath = 'cjxl.exe'; Arguments = @($f.FullName, "$($f.FullName).jxl") } } # $batch = Invoke-PkRun { Invoke-PkProcessBatch -Commands $commands -MaxConcurrent 6 } # $batch.Results | Format-Table ExitCode, Aborted, TimedOut param( diff --git a/README.md b/README.md index 4d0734b..2b6a81a 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Most uses are this: a list of command-line programs, run a few at a time, where . "$PSScriptRoot\ParallelKit.ps1" $commands = foreach ($f in Get-ChildItem *.png) { - @{ FilePath = 'cjxl.exe'; Arguments = (ConvertTo-PkArgumentString @($f.FullName, "$($f.FullName).jxl")) } + @{ FilePath = 'cjxl.exe'; Arguments = @($f.FullName, "$($f.FullName).jxl") } } $batch = Invoke-PkRun { Invoke-PkProcessBatch -Commands $commands -MaxConcurrent 6 } @@ -80,7 +80,7 @@ What none of them do for you: ## Setup -Requires Windows, with Windows PowerShell 5.1 or PowerShell 7. Download `ParallelKit.ps1` from the [Releases page](https://github.com/HalRiveria/PowerShell-ParallelKit/releases) (this README describes version 2.3 and later), put it in your project, and dot-source it from your script: +Requires Windows, with Windows PowerShell 5.1 or PowerShell 7. Download `ParallelKit.ps1` from the [Releases page](https://github.com/HalRiveria/PowerShell-ParallelKit/releases) (this README describes version 3.0 and later), put it in your project, and dot-source it from your script: ```powershell . "$PSScriptRoot\ParallelKit.ps1" @@ -108,7 +108,7 @@ Each public function has built-in help: `Get-Help Invoke-PkProcessBatch -Full` s ```powershell # Any command-line tool works. cjxl is a JPEG XL image encoder. $commands = foreach ($f in Get-ChildItem *.png) { - @{ FilePath = 'cjxl.exe'; Arguments = (ConvertTo-PkArgumentString @($f.FullName, "$($f.FullName).jxl")); Source = $f } + @{ FilePath = 'cjxl.exe'; Arguments = @($f.FullName, "$($f.FullName).jxl"); Source = $f } } $batchParams = @{ Commands = $commands @@ -123,7 +123,16 @@ $batch = Invoke-PkRun { Invoke-PkProcessBatch @batchParams } Long parameter lists in these examples use splatting (`@batchParams`) instead of trailing backticks: it's easier to read, and a stray space after a backtick can't break it. -Each command is a hashtable with `FilePath`, `Arguments` and optionally `WorkingDirectory`, plus any keys of your own. `Arguments` is either one string, passed to the program exactly as written, or a list, where each element is one argument: `Arguments = @($in, $out)` works with spaces in the paths. A list is quoted the way `ConvertTo-PkArgumentString @(...)` quotes it, so the program gets exactly that list back, spaces, quotes and trailing backslashes included, and `$null` elements are left out. A file or folder object from `Get-ChildItem` or `Get-Item` is passed as its full path. An element that's itself a list is an error, not one argument. Quotes inside an element are passed on as quote characters, so don't pre-quote list elements. It follows the rules most Windows programs use; `cmd.exe /c` parses its own way, so give it one string. A `.bat` or `.cmd` program runs through `cmd.exe` too. A list element holding `&`, `|`, `<`, `>`, `^`, `(`, `)`, `,` or `;` is quoted, so such a program gets it as one argument. What quoting can't cover there: `cmd.exe` expands `%NAME%` in an argument, quoted or not, when a variable by that name exists; and it splits batch arguments at `=`, which isn't quoted because some programs read `NAME=value` from the command line themselves. A `"` inside an element is written `""` for these programs, so `cmd.exe` can't be made to run anything else, and a batch file that passes `%*` on to another program still delivers it as one `"`. An element holding a line break can't be passed to them at all, so it's an error. All this needs the list form: a string, including one from `ConvertTo-PkArgumentString`, is passed as written. Programs run hidden, with their output captured. +Each command is a hashtable with `FilePath`, `Arguments` and optionally `WorkingDirectory`, plus any keys of your own. Programs run hidden, with their output captured. + +`Arguments` is either one string, passed to the program exactly as written, or a list with one argument per element: `Arguments = @($in, $out)` works with spaces in the paths. With a list: + +- The program gets exactly that list back, spaces, quotes and trailing backslashes included. Don't pre-quote elements: a quote inside one is passed on as a quote character. +- `$null` elements are left out, a file or folder object from `Get-ChildItem` or `Get-Item` is its full path, and an element that's itself a list is an error. +- The quoting follows the rules most Windows programs use. `cmd.exe /c` parses its own way, so give it one string. +- A `.bat` or `.cmd` program runs through `cmd.exe`, and the list is quoted for it: an element holding `&`, `|`, `<`, `>`, `^`, `(`, `)`, `,`, `;` or `"` reaches it as one argument and can't make `cmd.exe` run anything else, and a batch file that passes `%*` on to another program delivers the same list. An element with a line break is an error. What quoting can't stop: `cmd.exe` expands `%NAME%` when a variable by that name exists, and splits batch arguments at `=` (left unquoted, since some programs read `NAME=value` themselves). + +Only the list form gets this quoting. A string, including one built with `ConvertTo-PkArgumentString`, is passed as written. Programs run in your current location (`Get-Location`) unless a command says otherwise, and a relative `WorkingDirectory`, or a `FilePath` with a folder in it like `.\tools\cjxl.exe`, is relative to that location too. Drives PowerShell knows, like `~` or one made with `New-PSDrive`, work in both. A bare name like `cjxl.exe` is found the way Windows finds programs, mostly on `PATH`. Only an `.exe` is found without its extension (Windows doesn't use `PATHEXT` here), so a `.cmd` wrapper such as `npm` has to be written out: `npm.cmd`. A `.bat` or `.cmd` program can't run in a network-share (`\\server\share`) folder: `cmd.exe` falls back to `C:\Windows` there, so give such commands a local `WorkingDirectory`. @@ -198,7 +207,7 @@ Each worker runs in its own runspace: a separate PowerShell session inside the s - Don't call `exit` in a worker. It ends that job quietly (not as a crash) and sets your whole script's exit code. - `-MaxParallel` has no default; you choose it. `-DrainTimeoutSeconds` (how long Ctrl+C waits for running workers) defaults to 60. The process engines' names, `-MaxConcurrent` and `-AbortTimeoutSeconds`, work here too. - If `-ResultHandler`, `-PollCallback` or `-NextJobProvider` throws, the run stops: workers still going are told to stop and the exception comes out of the call. -- Returns an object with `ForceStoppedCount`, `CrashedCount`, `Aborted`, `Crashed`, which lists the `Job` and `Error` of each worker that threw, and `Errors`, which lists the `Job` and `Error` of each non-terminating error a worker wrote (including any a worker wrote before it threw). A worker that was force-stopped is in neither list. Every entry is kept until the call returns, so a worker that can write thousands of errors (walking a tree full of folders it can't read, say) should handle them itself, e.g. with `-ErrorAction SilentlyContinue -ErrorVariable`. +- Returns an object with `ForceStoppedCount`, `CrashedCount`, `Aborted`, `Crashed` and `Errors`. `Crashed` lists the `Job` and `Error` of each worker that threw. `Errors` lists the `Job` and `Error` of each non-terminating error a worker wrote, including any written before it threw. A force-stopped worker is in neither list. Every entry is kept until the call returns, so a worker that can write thousands of errors (walking a tree of folders it can't read, say) should handle them itself, e.g. with `-ErrorAction SilentlyContinue -ErrorVariable`. ## The two rules for worker code @@ -264,11 +273,11 @@ To leave any callback early, use `return`. PowerShell sends a `continue` or `bre | Function | Use | |---|---| | `Invoke-PkProcessWithTimeout -FilePath -Arguments -TimeoutMs` | Run one program and wait, with a hard time limit (default 60s). Returns an object with `ExitCode`, `Stdout`, `Stderr`, `StreamTimedOut` and `StreamError`. If it overruns, or the run is aborted, the program is killed and this **throws**; add `-IgnoreAbort` for a cleanup step that must finish anyway. | -| `ConvertTo-PkArgumentString @(args)` | Join a list of arguments into one correctly quoted command line. `-Arguments` / `Arguments` also take the list directly and quote it the same way. | +| `ConvertTo-PkArgumentString @(args)` | Join a list of arguments into one correctly quoted command line. `-Arguments` / `Arguments` also take the list directly, and then also quote it for `cmd.exe` when the program is a `.bat` or `.cmd`. | | `Start-PkProcess -FilePath -Arguments` | Start one hidden, with output captured in the background. Returns `@{ Proc; StdoutTask; StderrTask; Stopwatch }`; throws if it can't start. | | `Wait-PkProcess -Process -TimeoutMs` | Wait for exit with a timeout that can't overrun. Returns `$true` if it exited. | | `Complete-PkProcess -Job` | Collect exit code and output from a finished one (`-Job` is what `Start-PkProcess` returned). Same result as `Invoke-PkProcessWithTimeout`. | -| `Stop-PkProcessTree -Process` | Kill one or more programs and every program they started, in one `taskkill` call. For one program, returns `$false` if it had already exited. | +| `Stop-PkProcessTree -Process` | Kill one or more programs and every program they started, in one `taskkill` call. For one program, returns `$true` if it killed it (even if part of its tree couldn't be) and `$false` if it had already exited. | `Invoke-PkProcessWithTimeout` and `Start-PkProcess` also take `-WorkingDirectory`. @@ -293,7 +302,7 @@ Inside `Invoke-PkRun { ... }`, Ctrl+C: 3. waits for in-flight work (`-AbortTimeoutSeconds`, default 60; every engine also takes it as `-DrainTimeoutSeconds`), 4. force-stops whatever is left, calling `-JobAbortCleanup` for each killed or never-started process job. -Press Ctrl+C again to skip step 3: whatever is still running is force-stopped right away, and your script carries on. A third press stops the script the way Ctrl+C normally does. Presses less than a second apart count as one, and Ctrl+Break works the same as Ctrl+C. +Press Ctrl+C again to skip step 3: whatever is still running is force-stopped right away, and your script carries on. A third press stops the script the way Ctrl+C normally does, and the kit's engines still kill the programs they started. Presses less than a second apart count as one, and Ctrl+Break works the same as Ctrl+C. Closing the window does the same minus step 3: Windows allows only a few seconds, so leftovers are stopped right away. @@ -332,7 +341,7 @@ Other pieces: `Request-PkAbort` triggers the same abort from code. `Test-PkAbort powershell -NoProfile -File .\ParallelKit.Harness.ps1 ``` -It uses real runspaces and real child processes (`cmd.exe`, `ping.exe`), with nothing mocked, so it takes about two minutes. It prints a `[PASS]`/`[FAIL]` line per check and exits 0 only if everything passed. A few checks send real Ctrl+C, Ctrl+Break and window-close events to child PowerShell processes in hidden consoles of their own, never to yours. Where a machine can't deliver those, they print `[SKIP]` with the reason instead; set `PK_HARNESS_NO_SKIP=1` to count skips as failures. A few `message handler failed` lines in the output are expected: some checks break the message handler on purpose to test the fallback. +It uses real runspaces and real child processes (`cmd.exe`, `ping.exe`), with nothing mocked, so it takes two to three minutes. It prints a `[PASS]`/`[FAIL]` line per check and exits 0 only if everything passed. A few checks send real Ctrl+C, Ctrl+Break and window-close events to child PowerShell processes in hidden consoles of their own, never to yours. Where a machine can't deliver those, they print `[SKIP]` with the reason instead; set `PK_HARNESS_NO_SKIP=1` to count skips as failures. A few `message handler failed` lines in the output are expected: some checks break the message handler on purpose to test the fallback. GitHub runs the same harness on Windows PowerShell 5.1 and on PowerShell 7 for every pull request and every change to `main`. From 5e3470b4fe1101b22a3227d9ad9d61d683d75659 Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Mon, 16 Nov 2026 19:01:59 -0500 Subject: [PATCH 6/7] Name each engine once in callback warnings; README: batch files see a quote doubled Each engine sets its callback-warning prefix once, and call sites pass only the parameter name. The continue/break tests now check each warning names the right engine and parameter. The README said a quote reaches a .bat/.cmd as one argument without noting that the batch file's own %1/%~1 sees it doubled. Co-Authored-By: Claude Opus 5.5 --- ParallelKit.Harness.ps1 | 12 +++-- ParallelKit.ps1 | 107 ++++++++++++++++++++++------------------ README.md | 2 +- 3 files changed, 68 insertions(+), 53 deletions(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index 29238b4..c2329a2 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -3088,7 +3088,8 @@ $kwOk = ($b.Results.Count -eq 4 -and $kwList.Count -ge 2); $kwDetail = "results '@ } $kwEpilogue = @' -[pscustomobject]@{ Ok = [bool]$kwOk; Detail = [string]$kwDetail; Warned = @($kwMessages | Where-Object { $_ -like "*'continue' or 'break'*" }).Count } +$kwWarnings = @($kwMessages | Where-Object { $_ -like "*'continue' or 'break'*" }) +[pscustomobject]@{ Ok = [bool]$kwOk; Detail = [string]$kwDetail; Warned = $kwWarnings.Count; Text = ($kwWarnings -join ' | ') } '@ $kwSavedHandler = [PkState]::MessageHandler foreach ($kwName in $kwCases.Keys) { @@ -3114,9 +3115,14 @@ foreach ($kwName in $kwCases.Keys) { # to the console, once however many messages the handler got. $kwWarned = ([regex]::Matches($kwConsole, "'continue' or 'break'")).Count -eq 1 } else { - $kwWarned = ($null -ne $kwOut -and $kwOut.Warned -eq 1) + # The warning names the parameter and the engine it was given to. + $kwEngine = 'ProcessDispatch' + if ($kwName -like '*(batch)') { $kwEngine = 'Invoke-PkProcessBatch' } + elseif ($kwName -in '-ResultHandler', '-PollCallback', '-NextJobProvider', '-JobCrashedHandler') { $kwEngine = 'Invoke-PkRunspaceJobs' } + $kwExpected = $kwEngine + ': ' + ($kwName -split ' ')[0] + " used 'continue' or 'break'" + $kwWarned = ($null -ne $kwOut -and $kwOut.Warned -eq 1 -and $kwOut.Text.StartsWith($kwExpected)) } - $kwDetailText = if ($null -ne $kwOut) { $kwOut.Detail + ", warnings " + $kwOut.Warned } elseif ($kwFinished) { "error: " + $kwError } else { "still running after 20 s" } + $kwDetailText = if ($null -ne $kwOut) { $kwOut.Detail + ", warnings " + $kwOut.Warned + ": " + $kwOut.Text } elseif ($kwFinished) { "error: " + $kwError } else { "still running after 20 s" } Test-Assert ($kwFinished -and $null -ne $kwOut -and $kwOut.Ok -and $kwWarned) ("'" + $kwKeyword + "' in " + $kwName + " ends only the callback, with one warning (" + $kwDetailText + ")") } } diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index 2adaaee..f608de1 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -439,7 +439,9 @@ function Invoke-PkCallback { # or `break` in a callback goes to the nearest loop on the call stack, even # across the module boundary: one of the kit's, which then skips its # bookkeeping or never ends. This do/while catches it instead, so it ends - # only the callback, like `return`. Warns once per -PkName in -PkWarned. + # only the callback, like `return`. Warns once per -PkName in a run. + # -PkContext is the run's @{ Prefix; Warned }: Prefix names the engine + # (prepended to -PkName in the warning), Warned the names warned about. # A labeled `break outer` still reaches the caller's loop. # Invoke-PkMessageHandler has its own copy of this. # pk-prefixed: in a worker, a callback without .GetNewClosure() sees this frame. @@ -447,13 +449,16 @@ function Invoke-PkCallback { [Parameter(Mandatory)][scriptblock]$PkCallback, [object[]]$PkArgs = @(), [Parameter(Mandatory)][string]$PkName, - [hashtable]$PkWarned + [hashtable]$PkContext ) $pkCompleted = $false $pkOutput = do { & $PkCallback @PkArgs; $pkCompleted = $true } while ($false) - if (-not $pkCompleted -and ($null -eq $PkWarned -or -not $PkWarned.ContainsKey($PkName))) { - if ($null -ne $PkWarned) { $PkWarned[$PkName] = $true } - Write-PkMessage ($PkName + " used 'continue' or 'break' outside a loop of its own. PowerShell sends those to the nearest loop, which here is one of the kit's, so the kit stopped the callback there instead. Use 'return' to leave a callback early.") -Level Warn + $pkWarned = $null + $pkFullName = $PkName + if ($null -ne $PkContext) { $pkWarned = $PkContext['Warned']; $pkFullName = [string]$PkContext['Prefix'] + $PkName } + if (-not $pkCompleted -and ($null -eq $pkWarned -or -not $pkWarned.ContainsKey($PkName))) { + if ($null -ne $pkWarned) { $pkWarned[$PkName] = $true } + Write-PkMessage ($pkFullName + " used 'continue' or 'break' outside a loop of its own. PowerShell sends those to the nearest loop, which here is one of the kit's, so the kit stopped the callback there instead. Use 'return' to leave a callback early.") -Level Warn } # The comma keeps a collection returned whole (`return ,$list`) one value. if ($null -ne $pkOutput) { return ,$pkOutput } @@ -1477,7 +1482,7 @@ function Add-PkDispatchGroupJobs { $GroupInfo, [Parameter(Mandatory)][object]$GroupKey, [Parameter(Mandatory)][System.Collections.Generic.Queue[hashtable]]$Pending, - [hashtable]$CallbackWarned + [hashtable]$CallbackContext ) # pk-prefixed, parameters included: inside a worker runspace the kit isn't # a module, so a callback without .GetNewClosure() sees this frame's @@ -1486,9 +1491,9 @@ function Add-PkDispatchGroupJobs { $pkGroupInfo = $GroupInfo $pkGroupKey = $GroupKey $pkPending = $Pending - $pkCallbackWarned = $CallbackWarned - Remove-Variable -Name JobBuilder, GroupInfo, GroupKey, Pending, CallbackWarned -Scope Local - $pkRawJobs = Invoke-PkCallback -PkCallback $pkJobBuilder -PkArgs @(,$pkGroupInfo) -PkName 'ProcessDispatch: -JobBuilder' -PkWarned $pkCallbackWarned + $pkCallbackContext = $CallbackContext + Remove-Variable -Name JobBuilder, GroupInfo, GroupKey, Pending, CallbackContext -Scope Local + $pkRawJobs = Invoke-PkCallback -PkCallback $pkJobBuilder -PkArgs @(,$pkGroupInfo) -PkName '-JobBuilder' -PkContext $pkCallbackContext # Assigned in each branch, not `$x = if ... else ...`: captured that way, # a single hashtable is enumerated into its key/value pairs. if ($null -eq $pkRawJobs) { $pkRawJobList = @() } else { $pkRawJobList = @($pkRawJobs) } @@ -1567,8 +1572,9 @@ function Invoke-PkProcessDispatchCore { # The job -JobStarter is working on: not in $pkRunning yet, but it may # already have a program and a shared slot. $pkStarting = $null - # Callbacks that used continue/break, so each is only warned about once. - $pkCallbackWarned = @{} + # Invoke-PkCallback's -PkContext: callbacks that used continue/break are + # warned about once each, under this prefix. + $pkCallbackContext = @{ Prefix = 'ProcessDispatch: '; Warned = @{} } # Teardown is in a finally, guarded by this flag: when PowerShell stops # the script (Ctrl+C outside Invoke-PkRun, the runspace engine # force-stopping a worker), catch blocks are skipped. @@ -1578,24 +1584,24 @@ function Invoke-PkProcessDispatchCore { (-not $pkProviderExhausted -or $pkActiveGroupCount -gt 0 -or $pkPending.Count -gt 0 -or $pkRunning.Count -gt 0)) { for ($pkH = $pkHeldGroups.Count - 1; $pkH -ge 0; $pkH--) { - if ([bool](Invoke-PkCallback -PkCallback $pkGroupReleaseCheck -PkArgs @(,$pkHeldGroups[$pkH]) -PkName 'ProcessDispatch: -GroupReleaseCheck' -PkWarned $pkCallbackWarned)) { + if ([bool](Invoke-PkCallback -PkCallback $pkGroupReleaseCheck -PkArgs @(,$pkHeldGroups[$pkH]) -PkName '-GroupReleaseCheck' -PkContext $pkCallbackContext)) { $pkHeldGroups.RemoveAt($pkH) $pkActiveGroupCount-- } } while ((-not $pkProviderExhausted) -and $pkActiveGroupCount -lt $pkMaxQueueAhead -and (-not $pkCancelToken.IsCancellationRequested)) { - $pkGroupInfo = Invoke-PkCallback -PkCallback $pkGroupProvider -PkName 'ProcessDispatch: -GroupProvider' -PkWarned $pkCallbackWarned + $pkGroupInfo = Invoke-PkCallback -PkCallback $pkGroupProvider -PkName '-GroupProvider' -PkContext $pkCallbackContext if ($null -eq $pkGroupInfo) { $pkProviderExhausted = $true; break } # A fresh key per group, not the group value: two groups that # compare equal (the same folder name) mustn't share a job count. $pkGroupKey = [object]::new() - $pkJobCount = Add-PkDispatchGroupJobs -JobBuilder $pkJobBuilder -GroupInfo $pkGroupInfo -GroupKey $pkGroupKey -Pending $pkPending -CallbackWarned $pkCallbackWarned + $pkJobCount = Add-PkDispatchGroupJobs -JobBuilder $pkJobBuilder -GroupInfo $pkGroupInfo -GroupKey $pkGroupKey -Pending $pkPending -CallbackContext $pkCallbackContext $pkState = @{ GroupInfo = $pkGroupInfo; Outstanding = $pkJobCount; Completed = $false } $pkGroupStates[$pkGroupKey] = $pkState $pkActiveGroupCount++ if ($pkState.Outstanding -le 0) { - $null = Invoke-PkCallback -PkCallback $pkGroupCompleteCallback -PkArgs @(,$pkGroupInfo) -PkName 'ProcessDispatch: -GroupCompleteCallback' -PkWarned $pkCallbackWarned + $null = Invoke-PkCallback -PkCallback $pkGroupCompleteCallback -PkArgs @(,$pkGroupInfo) -PkName '-GroupCompleteCallback' -PkContext $pkCallbackContext $pkState.Completed = $true if ($null -ne $pkGroupReleaseCheck) { $pkHeldGroups.Add($pkGroupInfo) } else { $pkActiveGroupCount-- } $pkGroupResults.Add([pscustomobject][ordered]@{ GroupInfo = $pkGroupInfo; Completed = $true; Aborted = $false }) @@ -1605,7 +1611,7 @@ function Invoke-PkProcessDispatchCore { # Main thread: this is what notices an abort and cancels the token. # Worker: just reads the flag; the main thread cancels the token. $null = Test-PkAbortSignal - if ($pkPollCallback) { $null = Invoke-PkCallback -PkCallback $pkPollCallback -PkArgs @(,@($pkRunning)) -PkName 'ProcessDispatch: -PollCallback' -PkWarned $pkCallbackWarned } + if ($pkPollCallback) { $null = Invoke-PkCallback -PkCallback $pkPollCallback -PkArgs @(,@($pkRunning)) -PkName '-PollCallback' -PkContext $pkCallbackContext } while ($pkPending.Count -gt 0 -and (-not $pkCancelToken.IsCancellationRequested) -and ` $(if ($pkIsSharedBudget) { $pkMaxConcurrent.Wait(0) } else { $pkRunning.Count -lt $pkMaxConcurrent })) { @@ -1626,7 +1632,7 @@ function Invoke-PkProcessDispatchCore { $pkStartFailureText = "ProcessDispatch: a job couldn't start: " $pkJob['TimeLimit'] = Get-PkJobTimeLimit -Job $pkJob -DefaultTimeoutSeconds $pkTimeoutSeconds $pkStartFailureText = "ProcessDispatch: JobStarter failed to start a job: " - $pkStartedRaw = Invoke-PkCallback -PkCallback $pkJobStarter -PkArgs @(,$pkJob) -PkName 'ProcessDispatch: -JobStarter' -PkWarned $pkCallbackWarned + $pkStartedRaw = Invoke-PkCallback -PkCallback $pkJobStarter -PkArgs @(,$pkJob) -PkName '-JobStarter' -PkContext $pkCallbackContext foreach ($pkItem in @($pkStartedRaw)) { if ($pkItem -isnot [hashtable] -or [object]::ReferenceEquals($pkItem, $pkJob)) { continue } foreach ($pkItemKey in @($pkItem.Keys)) { @@ -1660,7 +1666,7 @@ function Invoke-PkProcessDispatchCore { $pkState = $pkGroupStates[$pkKey] $pkState.Outstanding-- if ($pkState.Outstanding -le 0 -and -not $pkState.Completed) { - $null = Invoke-PkCallback -PkCallback $pkGroupCompleteCallback -PkArgs @(,$pkState.GroupInfo) -PkName 'ProcessDispatch: -GroupCompleteCallback' -PkWarned $pkCallbackWarned + $null = Invoke-PkCallback -PkCallback $pkGroupCompleteCallback -PkArgs @(,$pkState.GroupInfo) -PkName '-GroupCompleteCallback' -PkContext $pkCallbackContext $pkState.Completed = $true $pkActiveGroupCount-- $pkGroupResults.Add([pscustomobject][ordered]@{ GroupInfo = $pkState.GroupInfo; Completed = $true; Aborted = $false }) @@ -1677,9 +1683,9 @@ function Invoke-PkProcessDispatchCore { $pkStillRunning = [System.Collections.Generic.List[hashtable]]::new() foreach ($pkJob in $pkRunning) { if ($pkJob['TimeLimit'] -gt 0) { Stop-PkTimedOutJob -Job $pkJob } - if (Invoke-PkCallback -PkCallback $pkJobPollCompleted -PkArgs @(,$pkJob) -PkName 'ProcessDispatch: -JobPollCompleted' -PkWarned $pkCallbackWarned) { + if (Invoke-PkCallback -PkCallback $pkJobPollCompleted -PkArgs @(,$pkJob) -PkName '-JobPollCompleted' -PkContext $pkCallbackContext) { $pkPendingBefore = $pkPending.Count - $null = Invoke-PkCallback -PkCallback $pkJobFinalize -PkArgs @($pkJob, $pkPending) -PkName 'ProcessDispatch: -JobFinalize' -PkWarned $pkCallbackWarned + $null = Invoke-PkCallback -PkCallback $pkJobFinalize -PkArgs @($pkJob, $pkPending) -PkName '-JobFinalize' -PkContext $pkCallbackContext try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } # a failed Dispose can't affect the result already taken if ($pkIsSharedBudget) { $null = $pkMaxConcurrent.Release() } $pkJob['SlotReleased'] = $true @@ -1702,7 +1708,7 @@ function Invoke-PkProcessDispatchCore { $pkState = $pkGroupStates[$pkKey] $pkState.Outstanding-- if ($pkState.Outstanding -le 0 -and -not $pkState.Completed) { - $null = Invoke-PkCallback -PkCallback $pkGroupCompleteCallback -PkArgs @(,$pkState.GroupInfo) -PkName 'ProcessDispatch: -GroupCompleteCallback' -PkWarned $pkCallbackWarned + $null = Invoke-PkCallback -PkCallback $pkGroupCompleteCallback -PkArgs @(,$pkState.GroupInfo) -PkName '-GroupCompleteCallback' -PkContext $pkCallbackContext $pkState.Completed = $true $pkActiveGroupCount-- $pkGroupResults.Add([pscustomobject][ordered]@{ GroupInfo = $pkState.GroupInfo; Completed = $true; Aborted = $false }) @@ -1733,9 +1739,9 @@ function Invoke-PkProcessDispatchCore { $pkStillWaiting = [System.Collections.Generic.List[hashtable]]::new() foreach ($pkJob in $pkRunning) { if ($pkJob['TimeLimit'] -gt 0) { Stop-PkTimedOutJob -Job $pkJob } - if (Invoke-PkCallback -PkCallback $pkJobPollCompleted -PkArgs @(,$pkJob) -PkName 'ProcessDispatch: -JobPollCompleted' -PkWarned $pkCallbackWarned) { + if (Invoke-PkCallback -PkCallback $pkJobPollCompleted -PkArgs @(,$pkJob) -PkName '-JobPollCompleted' -PkContext $pkCallbackContext) { $pkPendingBefore = $pkPending.Count - $null = Invoke-PkCallback -PkCallback $pkJobFinalize -PkArgs @($pkJob, $pkPending) -PkName 'ProcessDispatch: -JobFinalize' -PkWarned $pkCallbackWarned + $null = Invoke-PkCallback -PkCallback $pkJobFinalize -PkArgs @($pkJob, $pkPending) -PkName '-JobFinalize' -PkContext $pkCallbackContext try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } if ($pkIsSharedBudget) { $null = $pkMaxConcurrent.Release() } $pkJob['SlotReleased'] = $true @@ -1758,7 +1764,7 @@ function Invoke-PkProcessDispatchCore { $pkState = $pkGroupStates[$pkKey] $pkState.Outstanding-- if ($pkState.Outstanding -le 0 -and -not $pkState.Completed) { - $null = Invoke-PkCallback -PkCallback $pkGroupCompleteCallback -PkArgs @(,$pkState.GroupInfo) -PkName 'ProcessDispatch: -GroupCompleteCallback' -PkWarned $pkCallbackWarned + $null = Invoke-PkCallback -PkCallback $pkGroupCompleteCallback -PkArgs @(,$pkState.GroupInfo) -PkName '-GroupCompleteCallback' -PkContext $pkCallbackContext $pkState.Completed = $true $pkActiveGroupCount-- $pkGroupResults.Add([pscustomobject][ordered]@{ GroupInfo = $pkState.GroupInfo; Completed = $true; Aborted = $false }) @@ -1783,9 +1789,9 @@ function Invoke-PkProcessDispatchCore { if ($pkRunning.Count -gt 0) { $pkStillWaiting = [System.Collections.Generic.List[hashtable]]::new() foreach ($pkJob in $pkRunning) { - if (Invoke-PkCallback -PkCallback $pkJobPollCompleted -PkArgs @(,$pkJob) -PkName 'ProcessDispatch: -JobPollCompleted' -PkWarned $pkCallbackWarned) { + if (Invoke-PkCallback -PkCallback $pkJobPollCompleted -PkArgs @(,$pkJob) -PkName '-JobPollCompleted' -PkContext $pkCallbackContext) { $pkPendingBefore = $pkPending.Count - $null = Invoke-PkCallback -PkCallback $pkJobFinalize -PkArgs @($pkJob, $pkPending) -PkName 'ProcessDispatch: -JobFinalize' -PkWarned $pkCallbackWarned + $null = Invoke-PkCallback -PkCallback $pkJobFinalize -PkArgs @($pkJob, $pkPending) -PkName '-JobFinalize' -PkContext $pkCallbackContext try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } if ($pkIsSharedBudget) { $null = $pkMaxConcurrent.Release() } $pkJob['SlotReleased'] = $true @@ -1808,7 +1814,7 @@ function Invoke-PkProcessDispatchCore { $pkState = $pkGroupStates[$pkKey] $pkState.Outstanding-- if ($pkState.Outstanding -le 0 -and -not $pkState.Completed) { - $null = Invoke-PkCallback -PkCallback $pkGroupCompleteCallback -PkArgs @(,$pkState.GroupInfo) -PkName 'ProcessDispatch: -GroupCompleteCallback' -PkWarned $pkCallbackWarned + $null = Invoke-PkCallback -PkCallback $pkGroupCompleteCallback -PkArgs @(,$pkState.GroupInfo) -PkName '-GroupCompleteCallback' -PkContext $pkCallbackContext $pkState.Completed = $true $pkActiveGroupCount-- $pkGroupResults.Add([pscustomobject][ordered]@{ GroupInfo = $pkState.GroupInfo; Completed = $true; Aborted = $false }) @@ -1831,7 +1837,7 @@ function Invoke-PkProcessDispatchCore { Stop-PkJobProcesses -Jobs $pkRunning foreach ($pkJob in $pkRunning) { try { if ($pkJob['Proc']) { $null = $pkJob['Proc'].WaitForExit(5000) } } catch { } - try { $null = Invoke-PkCallback -PkCallback $pkJobAbortCleanup -PkArgs @(,$pkJob) -PkName 'ProcessDispatch: -JobAbortCleanup' -PkWarned $pkCallbackWarned } catch { } + try { $null = Invoke-PkCallback -PkCallback $pkJobAbortCleanup -PkArgs @(,$pkJob) -PkName '-JobAbortCleanup' -PkContext $pkCallbackContext } catch { } try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } if ($pkIsSharedBudget) { try { $null = $pkMaxConcurrent.Release() } catch { } } $pkJob['SlotReleased'] = $true @@ -1852,7 +1858,7 @@ function Invoke-PkProcessDispatchCore { Stop-PkJobProcesses -Jobs $pkTearDown foreach ($pkJob in $pkTearDown) { try { if ($pkJob['Proc']) { $null = $pkJob['Proc'].WaitForExit(5000) } } catch { } - try { $null = Invoke-PkCallback -PkCallback $pkJobAbortCleanup -PkArgs @(,$pkJob) -PkName 'ProcessDispatch: -JobAbortCleanup' -PkWarned $pkCallbackWarned } catch { } + try { $null = Invoke-PkCallback -PkCallback $pkJobAbortCleanup -PkArgs @(,$pkJob) -PkName '-JobAbortCleanup' -PkContext $pkCallbackContext } catch { } if ($pkIsSharedBudget) { try { $null = $pkMaxConcurrent.Release() } catch { } } $pkJob['SlotReleased'] = $true try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } @@ -1865,7 +1871,7 @@ function Invoke-PkProcessDispatchCore { # never be reported. if ($pkPending.Count -gt 0) { foreach ($pkJob in $pkPending) { - try { $null = Invoke-PkCallback -PkCallback $pkJobAbortCleanup -PkArgs @(,$pkJob) -PkName 'ProcessDispatch: -JobAbortCleanup' -PkWarned $pkCallbackWarned } catch { } + try { $null = Invoke-PkCallback -PkCallback $pkJobAbortCleanup -PkArgs @(,$pkJob) -PkName '-JobAbortCleanup' -PkContext $pkCallbackContext } catch { } $pkKey = $pkJob['GroupKey'] if ($pkGroupStates.ContainsKey($pkKey)) { $pkGroupStates[$pkKey].Outstanding-- } } @@ -1878,7 +1884,7 @@ function Invoke-PkProcessDispatchCore { # An aborted group still gets its callback: it's where the caller # credits the jobs that did finish. Swallowed, so one failing # callback doesn't cost the other groups their results. - try { $null = Invoke-PkCallback -PkCallback $pkGroupCompleteCallback -PkArgs @(,$pkState.GroupInfo) -PkName 'ProcessDispatch: -GroupCompleteCallback' -PkWarned $pkCallbackWarned } catch { } + try { $null = Invoke-PkCallback -PkCallback $pkGroupCompleteCallback -PkArgs @(,$pkState.GroupInfo) -PkName '-GroupCompleteCallback' -PkContext $pkCallbackContext } catch { } $pkState.Completed = $true $pkGroupResults.Add([pscustomobject][ordered]@{ GroupInfo = $pkState.GroupInfo; Completed = $false; Aborted = $true }) } @@ -1998,7 +2004,9 @@ function Invoke-PkProcessBatch { $pkGetJobTimeLimit = ${function:Get-PkJobTimeLimit} $pkGetStartFailureCause = ${function:Get-PkStartFailureCause} $pkInvokeCallback = ${function:Invoke-PkCallback} - $pkCallbackWarned = @{} + # Invoke-PkCallback's -PkContext: callbacks that used continue/break are + # warned about once each, under this prefix. + $pkCallbackContext = @{ Prefix = 'Invoke-PkProcessBatch: '; Warned = @{} } $pkCallerState = $PSCmdlet.SessionState $pkStartFailures = [System.Collections.Specialized.OrderedDictionary]::new([System.StringComparer]::OrdinalIgnoreCase) @@ -2062,7 +2070,7 @@ function Invoke-PkProcessBatch { param($pkJob, $pkPendingQueue) $pkR = & $pkNewBatchResult -Command $pkJob['Command'] -Completed (Complete-PkProcess -Job $pkJob) -TimedOut ([bool]$pkJob['TimedOut']) $pkResults.Add($pkR) - if ($null -ne $pkOnComplete) { $null = & $pkInvokeCallback -PkCallback $pkOnComplete -PkArgs @($pkJob['Command'], $pkR) -PkName 'Invoke-PkProcessBatch: -OnComplete' -PkWarned $pkCallbackWarned } + if ($null -ne $pkOnComplete) { $null = & $pkInvokeCallback -PkCallback $pkOnComplete -PkArgs @($pkJob['Command'], $pkR) -PkName '-OnComplete' -PkContext $pkCallbackContext } return @{ Success = ($pkR.ExitCode -eq 0) } }.GetNewClosure() @@ -2123,7 +2131,7 @@ function Add-PkCrashedJob { [Parameter(Mandatory)][System.Management.Automation.ErrorRecord]$PkError, [scriptblock]$PkHandler, [string]$PkWhen = "", - [hashtable]$PkCallbackWarned + [hashtable]$PkCallbackContext ) $PkOutcome.CrashedCount++ $PkOutcome.Crashed.Add([pscustomobject][ordered]@{ Job = $PkJob; Error = $PkError }) @@ -2136,7 +2144,7 @@ function Add-PkCrashedJob { $pkParamCount = Get-PkHandlerParamCount -Callback $PkHandler $pkHandlerArgs = @($PkError, $PkJob) if ($pkParamCount -ne -1 -and $pkParamCount -lt 2) { $pkHandlerArgs = @(,$PkError) } - $null = Invoke-PkCallback -PkCallback $PkHandler -PkArgs $pkHandlerArgs -PkName 'Invoke-PkRunspaceJobs: -JobCrashedHandler' -PkWarned $PkCallbackWarned + $null = Invoke-PkCallback -PkCallback $PkHandler -PkArgs $pkHandlerArgs -PkName '-JobCrashedHandler' -PkContext $PkCallbackContext } catch { # Reported, not rethrown: a broken crash handler mustn't take the other workers down. Invoke-PkMessageHandler -Level Error -Message ("-JobCrashedHandler threw: " + $_) @@ -2166,7 +2174,7 @@ function Receive-PkCompletedHandles { [scriptblock]$ResultHandler, [System.Collections.Specialized.OrderedDictionary]$Outcome, [scriptblock]$JobCrashedHandler, - [hashtable]$CallbackWarned + [hashtable]$CallbackContext ) $stillActive = [System.Collections.Generic.List[hashtable]]::new() foreach ($h in $Handles) { @@ -2179,13 +2187,13 @@ function Receive-PkCompletedHandles { # crash, in the order they happened. Add-PkJobErrors -PkOutcome $Outcome -PkJob $h['Job'] -PkErrors $h.PS.Streams.Error if ($null -ne $pkCrash) { - Add-PkCrashedJob -PkOutcome $Outcome -PkJob $h['Job'] -PkError $pkCrash -PkHandler $JobCrashedHandler -PkWhen " during abort drain" -PkCallbackWarned $CallbackWarned + Add-PkCrashedJob -PkOutcome $Outcome -PkJob $h['Job'] -PkError $pkCrash -PkHandler $JobCrashedHandler -PkWhen " during abort drain" -PkCallbackContext $CallbackContext } } finally { $h.PS.Dispose() } if ($null -ne $ResultHandler -and $null -ne $pkWorkerOutput) { - foreach ($r in $pkWorkerOutput) { $null = Invoke-PkCallback -PkCallback $ResultHandler -PkArgs @(,$r) -PkName 'Invoke-PkRunspaceJobs: -ResultHandler' -PkWarned $CallbackWarned } + foreach ($r in $pkWorkerOutput) { $null = Invoke-PkCallback -PkCallback $ResultHandler -PkArgs @(,$r) -PkName '-ResultHandler' -PkContext $CallbackContext } } } else { $stillActive.Add($h) @@ -2209,7 +2217,7 @@ function Invoke-PkGracefulDrain { [string]$AbortWaitMessage, [Parameter(Mandatory)][System.Collections.Specialized.OrderedDictionary]$Outcome, [scriptblock]$JobCrashedHandler, - [hashtable]$CallbackWarned + [hashtable]$CallbackContext ) if ($Handles.Count -eq 0) { @@ -2227,14 +2235,14 @@ function Invoke-PkGracefulDrain { if ($AbortWaitMessage -ne "") { Invoke-PkMessageHandler -Message $AbortWaitMessage -Level Warn } while ($localHandles.Count -gt 0 -and -not [PkConsoleCtrl]::IsCloseEvent -and -not [PkConsoleCtrl]::SkipDrain -and $abortDeadline.Elapsed.TotalSeconds -lt $TimeoutSeconds) { - $localHandles = Receive-PkCompletedHandles -Handles $localHandles -ResultHandler $ResultHandler -Outcome $Outcome -JobCrashedHandler $JobCrashedHandler -CallbackWarned $CallbackWarned + $localHandles = Receive-PkCompletedHandles -Handles $localHandles -ResultHandler $ResultHandler -Outcome $Outcome -JobCrashedHandler $JobCrashedHandler -CallbackContext $CallbackContext Receive-PkWorkerMessages -Queue $MessageQueue if ($localHandles.Count -gt 0) { [System.Threading.Thread]::Sleep(250) } } # One final pass before declaring anything force-stopped. if ($localHandles.Count -gt 0) { - $localHandles = Receive-PkCompletedHandles -Handles $localHandles -ResultHandler $ResultHandler -Outcome $Outcome -JobCrashedHandler $JobCrashedHandler -CallbackWarned $CallbackWarned + $localHandles = Receive-PkCompletedHandles -Handles $localHandles -ResultHandler $ResultHandler -Outcome $Outcome -JobCrashedHandler $JobCrashedHandler -CallbackContext $CallbackContext Receive-PkWorkerMessages -Queue $MessageQueue } @@ -2397,8 +2405,9 @@ function Invoke-PkRunspaceJobs { $pool = $null $handles = [System.Collections.Generic.List[hashtable]]::new() $scanExhausted = $false - # Callbacks that used continue/break, so each is only warned about once. - $pkCallbackWarned = @{} + # Invoke-PkCallback's -PkContext: callbacks that used continue/break are + # warned about once each, under this prefix. + $pkCallbackContext = @{ Prefix = 'Invoke-PkRunspaceJobs: '; Warned = @{} } try { if ($null -eq $Cts) { @@ -2465,7 +2474,7 @@ function Invoke-PkRunspaceJobs { } else { # Same as the dispatcher's builder: take the job # hashtable, ignore anything else the provider let out. - $nextRaw = Invoke-PkCallback -PkCallback $NextJobProvider -PkName 'Invoke-PkRunspaceJobs: -NextJobProvider' -PkWarned $pkCallbackWarned + $nextRaw = Invoke-PkCallback -PkCallback $NextJobProvider -PkName '-NextJobProvider' -PkContext $pkCallbackContext $nextJob = $null $strayCount = 0 foreach ($rawItem in @($nextRaw)) { @@ -2504,7 +2513,7 @@ function Invoke-PkRunspaceJobs { $null = Test-PkAbortSignal Receive-PkWorkerMessages -Queue $messageQueue - if ($null -ne $PollCallback) { $null = Invoke-PkCallback -PkCallback $PollCallback -PkName 'Invoke-PkRunspaceJobs: -PollCallback' -PkWarned $pkCallbackWarned } + if ($null -ne $PollCallback) { $null = Invoke-PkCallback -PkCallback $PollCallback -PkName '-PollCallback' -PkContext $pkCallbackContext } $stillPending = [System.Collections.Generic.List[hashtable]]::new() foreach ($h in $handles) { @@ -2519,13 +2528,13 @@ function Invoke-PkRunspaceJobs { if ($null -ne $pkCrash) { # The worker itself threw - there is no result, so # -ResultHandler is never called for this job. - Add-PkCrashedJob -PkOutcome $outcome -PkJob $h['Job'] -PkError $pkCrash -PkHandler $JobCrashedHandler -PkCallbackWarned $pkCallbackWarned + Add-PkCrashedJob -PkOutcome $outcome -PkJob $h['Job'] -PkError $pkCrash -PkHandler $JobCrashedHandler -PkCallbackContext $pkCallbackContext } } finally { $h.PS.Dispose() } if ($null -ne $pkWorkerOutput) { - foreach ($r in $pkWorkerOutput) { $null = Invoke-PkCallback -PkCallback $ResultHandler -PkArgs @(,$r) -PkName 'Invoke-PkRunspaceJobs: -ResultHandler' -PkWarned $pkCallbackWarned } + foreach ($r in $pkWorkerOutput) { $null = Invoke-PkCallback -PkCallback $ResultHandler -PkArgs @(,$r) -PkName '-ResultHandler' -PkContext $pkCallbackContext } } } else { $stillPending.Add($h) @@ -2548,7 +2557,7 @@ function Invoke-PkRunspaceJobs { } if ($Cts.Token.IsCancellationRequested) { - $drainArgs = @{ Handles = $handles; ResultHandler = $ResultHandler; MessageQueue = $messageQueue; TimeoutSeconds = $DrainTimeoutSeconds; Outcome = $outcome; JobCrashedHandler = $JobCrashedHandler; CallbackWarned = $pkCallbackWarned } + $drainArgs = @{ Handles = $handles; ResultHandler = $ResultHandler; MessageQueue = $messageQueue; TimeoutSeconds = $DrainTimeoutSeconds; Outcome = $outcome; JobCrashedHandler = $JobCrashedHandler; CallbackContext = $pkCallbackContext } if ($PSBoundParameters.ContainsKey('AbortWaitMessage')) { $drainArgs.AbortWaitMessage = $AbortWaitMessage } $drainResult = Invoke-PkGracefulDrain @drainArgs $outcome.ForceStoppedCount = $drainResult.ForceStoppedCount @@ -2556,7 +2565,7 @@ function Invoke-PkRunspaceJobs { } Receive-PkWorkerMessages -Queue $messageQueue - if ($null -ne $PollCallback) { $null = Invoke-PkCallback -PkCallback $PollCallback -PkName 'Invoke-PkRunspaceJobs: -PollCallback' -PkWarned $pkCallbackWarned } + if ($null -ne $PollCallback) { $null = Invoke-PkCallback -PkCallback $PollCallback -PkName '-PollCallback' -PkContext $pkCallbackContext } } finally { # Best-effort teardown; whatever exception is already in flight is the one to surface. $stillRunning = $false diff --git a/README.md b/README.md index 2b6a81a..9cf3892 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ Each command is a hashtable with `FilePath`, `Arguments` and optionally `Working - The program gets exactly that list back, spaces, quotes and trailing backslashes included. Don't pre-quote elements: a quote inside one is passed on as a quote character. - `$null` elements are left out, a file or folder object from `Get-ChildItem` or `Get-Item` is its full path, and an element that's itself a list is an error. - The quoting follows the rules most Windows programs use. `cmd.exe /c` parses its own way, so give it one string. -- A `.bat` or `.cmd` program runs through `cmd.exe`, and the list is quoted for it: an element holding `&`, `|`, `<`, `>`, `^`, `(`, `)`, `,`, `;` or `"` reaches it as one argument and can't make `cmd.exe` run anything else, and a batch file that passes `%*` on to another program delivers the same list. An element with a line break is an error. What quoting can't stop: `cmd.exe` expands `%NAME%` when a variable by that name exists, and splits batch arguments at `=` (left unquoted, since some programs read `NAME=value` themselves). +- A `.bat` or `.cmd` program runs through `cmd.exe`, and the list is quoted for it: an element holding `&`, `|`, `<`, `>`, `^`, `(`, `)`, `,`, `;` or `"` reaches it as one argument and can't make `cmd.exe` run anything else. A `"` is written `""` for it, so the batch file itself sees the quote doubled in `%1` or `%~1`, while a program it passes `%*` on to gets the list back as given. An element with a line break is an error. What quoting can't stop: `cmd.exe` expands `%NAME%` when a variable by that name exists, and splits batch arguments at `=` (left unquoted, since some programs read `NAME=value` themselves). Only the list form gets this quoting. A string, including one built with `ConvertTo-PkArgumentString`, is passed as written. From 59a1e4b57e7865e51d9527a321e6f28763b0e2a5 Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Mon, 16 Nov 2026 20:01:45 -0500 Subject: [PATCH 7/7] Catch continue/break inline in per-job callbacks Calling the continue/break wrapper function for every per-job callback more than doubled the dispatcher's own time per job on Windows PowerShell 5.1. The callbacks that run per job, per pass or per result now do the do/while inline; the shared warning is Write-PkCallbackLoopWarning. Adds continue/break tests for -JobPollCompleted and -JobFinalize during an abort's graceful wait. Co-Authored-By: Claude Opus 5.5 --- ParallelKit.Harness.ps1 | 12 ++++++ ParallelKit.ps1 | 95 ++++++++++++++++++++++++++++++----------- 2 files changed, 82 insertions(+), 25 deletions(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index c2329a2..fda0fc5 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -3048,6 +3048,18 @@ $cts = [System.Threading.CancellationTokenSource]::new() $cts.CancelAfter(300) $d = Invoke-KwDispatch -Jobs 3 -FilePath 'ping.exe' -Arguments '-n 2 127.0.0.1' -Extra @{ CancelToken = $cts.Token; AbortTimeoutSeconds = 30; JobAbortCleanup = { param($job) $kwCount.N++; KEYWORD } } $kwOk = ($d.Result.Aborted -and $d.Done -eq 1 -and $kwCount.N -eq 2); $kwDetail = "done " + $d.Done + ", cleaned " + $kwCount.N +'@ + '-JobFinalize (abort wait)' = @' +$cts = [System.Threading.CancellationTokenSource]::new() +$cts.CancelAfter(300) +$d = Invoke-KwDispatch -Jobs 3 -FilePath 'ping.exe' -Arguments '-n 2 127.0.0.1' -Extra @{ CancelToken = $cts.Token; AbortTimeoutSeconds = 30; JobFinalize = { param($job, $q) $null = Complete-PkProcess -Job $job; $kwCount.N++; KEYWORD } } +$kwOk = ($d.Result.Aborted -and $kwCount.N -eq 1 -and $d.Result.ForceKilledCount -eq 0); $kwDetail = "finalized " + $kwCount.N + ", force-killed " + $d.Result.ForceKilledCount +'@ + '-JobPollCompleted (abort wait)' = @' +$cts = [System.Threading.CancellationTokenSource]::new() +$cts.CancelAfter(300) +$d = Invoke-KwDispatch -Jobs 3 -FilePath 'ping.exe' -Arguments '-n 2 127.0.0.1' -Extra @{ CancelToken = $cts.Token; AbortTimeoutSeconds = 30; JobPollCompleted = { param($job) if ($job['Proc'].HasExited) { return $true }; $kwCount.N++; KEYWORD } } +$kwOk = ($d.Result.Aborted -and $d.Done -eq 1 -and $d.Result.ForceKilledCount -eq 0); $kwDetail = "done " + $d.Done + ", force-killed " + $d.Result.ForceKilledCount '@ '-GroupReleaseCheck' = @' $d = Invoke-KwDispatch -Jobs 0 -Extra @{ JobBuilder = { param($g) }; GroupReleaseCheck = { param($g) $kwCount.N++; if ($kwCount.N -ge 3) { return $true }; KEYWORD } } diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index f608de1..c0eb733 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -439,11 +439,10 @@ function Invoke-PkCallback { # or `break` in a callback goes to the nearest loop on the call stack, even # across the module boundary: one of the kit's, which then skips its # bookkeeping or never ends. This do/while catches it instead, so it ends - # only the callback, like `return`. Warns once per -PkName in a run. - # -PkContext is the run's @{ Prefix; Warned }: Prefix names the engine - # (prepended to -PkName in the warning), Warned the names warned about. - # A labeled `break outer` still reaches the caller's loop. - # Invoke-PkMessageHandler has its own copy of this. + # only the callback, like `return`. A labeled `break outer` still reaches + # the caller's loop. Inlined where a callback runs per job or per result + # (on 5.1 this call more than doubled the dispatcher's own time per job), + # and in Invoke-PkMessageHandler. # pk-prefixed: in a worker, a callback without .GetNewClosure() sees this frame. param( [Parameter(Mandatory)][scriptblock]$PkCallback, @@ -453,17 +452,26 @@ function Invoke-PkCallback { ) $pkCompleted = $false $pkOutput = do { & $PkCallback @PkArgs; $pkCompleted = $true } while ($false) - $pkWarned = $null - $pkFullName = $PkName - if ($null -ne $PkContext) { $pkWarned = $PkContext['Warned']; $pkFullName = [string]$PkContext['Prefix'] + $PkName } - if (-not $pkCompleted -and ($null -eq $pkWarned -or -not $pkWarned.ContainsKey($PkName))) { - if ($null -ne $pkWarned) { $pkWarned[$PkName] = $true } - Write-PkMessage ($pkFullName + " used 'continue' or 'break' outside a loop of its own. PowerShell sends those to the nearest loop, which here is one of the kit's, so the kit stopped the callback there instead. Use 'return' to leave a callback early.") -Level Warn - } + if (-not $pkCompleted) { Write-PkCallbackLoopWarning -PkName $PkName -PkContext $PkContext } # The comma keeps a collection returned whole (`return ,$list`) one value. if ($null -ne $pkOutput) { return ,$pkOutput } } +function Write-PkCallbackLoopWarning { + # For a callback that used continue/break (see Invoke-PkCallback). Warns + # once per -PkName in a run. -PkContext is the run's @{ Prefix; Warned }: + # Prefix names the engine, Warned the names already warned about. + param([string]$PkName, [hashtable]$PkContext) + $pkFullName = $PkName + if ($null -ne $PkContext) { + $pkWarned = $PkContext['Warned'] + if ($pkWarned.ContainsKey($PkName)) { return } + $pkWarned[$PkName] = $true + $pkFullName = [string]$PkContext['Prefix'] + $PkName + } + Write-PkMessage ($pkFullName + " used 'continue' or 'break' outside a loop of its own. PowerShell sends those to the nearest loop, which here is one of the kit's, so the kit stopped the callback there instead. Use 'return' to leave a callback early.") -Level Warn +} + # ============================================================================== # Abort handling # ============================================================================== @@ -1611,7 +1619,12 @@ function Invoke-PkProcessDispatchCore { # Main thread: this is what notices an abort and cancels the token. # Worker: just reads the flag; the main thread cancels the token. $null = Test-PkAbortSignal - if ($pkPollCallback) { $null = Invoke-PkCallback -PkCallback $pkPollCallback -PkArgs @(,@($pkRunning)) -PkName '-PollCallback' -PkContext $pkCallbackContext } + # This and the per-job callbacks below inline Invoke-PkCallback. + if ($pkPollCallback) { + $pkCbDone = $false + $null = do { & $pkPollCallback @($pkRunning); $pkCbDone = $true } while ($false) + if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-PollCallback' -PkContext $pkCallbackContext } + } while ($pkPending.Count -gt 0 -and (-not $pkCancelToken.IsCancellationRequested) -and ` $(if ($pkIsSharedBudget) { $pkMaxConcurrent.Wait(0) } else { $pkRunning.Count -lt $pkMaxConcurrent })) { @@ -1632,7 +1645,9 @@ function Invoke-PkProcessDispatchCore { $pkStartFailureText = "ProcessDispatch: a job couldn't start: " $pkJob['TimeLimit'] = Get-PkJobTimeLimit -Job $pkJob -DefaultTimeoutSeconds $pkTimeoutSeconds $pkStartFailureText = "ProcessDispatch: JobStarter failed to start a job: " - $pkStartedRaw = Invoke-PkCallback -PkCallback $pkJobStarter -PkArgs @(,$pkJob) -PkName '-JobStarter' -PkContext $pkCallbackContext + $pkCbDone = $false + $pkStartedRaw = do { & $pkJobStarter $pkJob; $pkCbDone = $true } while ($false) + if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-JobStarter' -PkContext $pkCallbackContext } foreach ($pkItem in @($pkStartedRaw)) { if ($pkItem -isnot [hashtable] -or [object]::ReferenceEquals($pkItem, $pkJob)) { continue } foreach ($pkItemKey in @($pkItem.Keys)) { @@ -1683,9 +1698,14 @@ function Invoke-PkProcessDispatchCore { $pkStillRunning = [System.Collections.Generic.List[hashtable]]::new() foreach ($pkJob in $pkRunning) { if ($pkJob['TimeLimit'] -gt 0) { Stop-PkTimedOutJob -Job $pkJob } - if (Invoke-PkCallback -PkCallback $pkJobPollCompleted -PkArgs @(,$pkJob) -PkName '-JobPollCompleted' -PkContext $pkCallbackContext) { + $pkCbDone = $false + $pkPolled = do { & $pkJobPollCompleted $pkJob; $pkCbDone = $true } while ($false) + if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-JobPollCompleted' -PkContext $pkCallbackContext } + if ($pkPolled) { $pkPendingBefore = $pkPending.Count - $null = Invoke-PkCallback -PkCallback $pkJobFinalize -PkArgs @($pkJob, $pkPending) -PkName '-JobFinalize' -PkContext $pkCallbackContext + $pkCbDone = $false + $null = do { & $pkJobFinalize $pkJob $pkPending; $pkCbDone = $true } while ($false) + if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-JobFinalize' -PkContext $pkCallbackContext } try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } # a failed Dispose can't affect the result already taken if ($pkIsSharedBudget) { $null = $pkMaxConcurrent.Release() } $pkJob['SlotReleased'] = $true @@ -1739,9 +1759,14 @@ function Invoke-PkProcessDispatchCore { $pkStillWaiting = [System.Collections.Generic.List[hashtable]]::new() foreach ($pkJob in $pkRunning) { if ($pkJob['TimeLimit'] -gt 0) { Stop-PkTimedOutJob -Job $pkJob } - if (Invoke-PkCallback -PkCallback $pkJobPollCompleted -PkArgs @(,$pkJob) -PkName '-JobPollCompleted' -PkContext $pkCallbackContext) { + $pkCbDone = $false + $pkPolled = do { & $pkJobPollCompleted $pkJob; $pkCbDone = $true } while ($false) + if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-JobPollCompleted' -PkContext $pkCallbackContext } + if ($pkPolled) { $pkPendingBefore = $pkPending.Count - $null = Invoke-PkCallback -PkCallback $pkJobFinalize -PkArgs @($pkJob, $pkPending) -PkName '-JobFinalize' -PkContext $pkCallbackContext + $pkCbDone = $false + $null = do { & $pkJobFinalize $pkJob $pkPending; $pkCbDone = $true } while ($false) + if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-JobFinalize' -PkContext $pkCallbackContext } try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } if ($pkIsSharedBudget) { $null = $pkMaxConcurrent.Release() } $pkJob['SlotReleased'] = $true @@ -1789,9 +1814,14 @@ function Invoke-PkProcessDispatchCore { if ($pkRunning.Count -gt 0) { $pkStillWaiting = [System.Collections.Generic.List[hashtable]]::new() foreach ($pkJob in $pkRunning) { - if (Invoke-PkCallback -PkCallback $pkJobPollCompleted -PkArgs @(,$pkJob) -PkName '-JobPollCompleted' -PkContext $pkCallbackContext) { + $pkCbDone = $false + $pkPolled = do { & $pkJobPollCompleted $pkJob; $pkCbDone = $true } while ($false) + if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-JobPollCompleted' -PkContext $pkCallbackContext } + if ($pkPolled) { $pkPendingBefore = $pkPending.Count - $null = Invoke-PkCallback -PkCallback $pkJobFinalize -PkArgs @($pkJob, $pkPending) -PkName '-JobFinalize' -PkContext $pkCallbackContext + $pkCbDone = $false + $null = do { & $pkJobFinalize $pkJob $pkPending; $pkCbDone = $true } while ($false) + if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-JobFinalize' -PkContext $pkCallbackContext } try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } if ($pkIsSharedBudget) { $null = $pkMaxConcurrent.Release() } $pkJob['SlotReleased'] = $true @@ -2003,7 +2033,7 @@ function Invoke-PkProcessBatch { $pkStartProcessCore = ${function:Start-PkProcessCore} $pkGetJobTimeLimit = ${function:Get-PkJobTimeLimit} $pkGetStartFailureCause = ${function:Get-PkStartFailureCause} - $pkInvokeCallback = ${function:Invoke-PkCallback} + $pkWriteCallbackLoopWarning = ${function:Write-PkCallbackLoopWarning} # Invoke-PkCallback's -PkContext: callbacks that used continue/break are # warned about once each, under this prefix. $pkCallbackContext = @{ Prefix = 'Invoke-PkProcessBatch: '; Warned = @{} } @@ -2070,7 +2100,12 @@ function Invoke-PkProcessBatch { param($pkJob, $pkPendingQueue) $pkR = & $pkNewBatchResult -Command $pkJob['Command'] -Completed (Complete-PkProcess -Job $pkJob) -TimedOut ([bool]$pkJob['TimedOut']) $pkResults.Add($pkR) - if ($null -ne $pkOnComplete) { $null = & $pkInvokeCallback -PkCallback $pkOnComplete -PkArgs @($pkJob['Command'], $pkR) -PkName '-OnComplete' -PkContext $pkCallbackContext } + if ($null -ne $pkOnComplete) { + # Invoke-PkCallback, inlined: this runs per command. + $pkCbDone = $false + $null = do { & $pkOnComplete $pkJob['Command'] $pkR; $pkCbDone = $true } while ($false) + if (-not $pkCbDone) { & $pkWriteCallbackLoopWarning -PkName '-OnComplete' -PkContext $pkCallbackContext } + } return @{ Success = ($pkR.ExitCode -eq 0) } }.GetNewClosure() @@ -2106,7 +2141,7 @@ function Get-PkWorkerSafeFunctionNames { return @('Write-PkMessage', 'Test-PkAbortSignal', 'Test-PkCancelRequested', 'Initialize-PkThreadPool', 'Stop-PkProcessTree', 'Stop-PkJobProcesses', 'Resolve-PkEncoding', 'Start-PkProcess', 'Wait-PkProcess', 'Wait-PkRunningJobs', 'Complete-PkProcess', 'Invoke-PkProcessWithTimeout', 'Invoke-PkProcessDispatch', 'Invoke-PkProcessDispatchCore', 'Invoke-PkProcessBatch', - 'Register-PkCancellationSource', 'Unregister-PkCancellationSource', 'New-PkBatchResult', 'ConvertTo-PkArgumentString', 'Get-PkForceStopReason', 'Resolve-PkFileSystemPath', 'Add-PkDispatchGroupJobs', 'Start-PkProcessCore', 'Get-PkJobTimeLimit', 'Stop-PkTimedOutJob', 'Get-PkStartFailureCause', 'Resolve-PkArguments', 'Invoke-PkCallback', 'ConvertTo-PkArgumentStringCore') + 'Register-PkCancellationSource', 'Unregister-PkCancellationSource', 'New-PkBatchResult', 'ConvertTo-PkArgumentString', 'Get-PkForceStopReason', 'Resolve-PkFileSystemPath', 'Add-PkDispatchGroupJobs', 'Start-PkProcessCore', 'Get-PkJobTimeLimit', 'Stop-PkTimedOutJob', 'Get-PkStartFailureCause', 'Resolve-PkArguments', 'Invoke-PkCallback', 'Write-PkCallbackLoopWarning', 'ConvertTo-PkArgumentStringCore') } function Get-PkHandlerParamCount { @@ -2193,7 +2228,12 @@ function Receive-PkCompletedHandles { $h.PS.Dispose() } if ($null -ne $ResultHandler -and $null -ne $pkWorkerOutput) { - foreach ($r in $pkWorkerOutput) { $null = Invoke-PkCallback -PkCallback $ResultHandler -PkArgs @(,$r) -PkName '-ResultHandler' -PkContext $CallbackContext } + foreach ($r in $pkWorkerOutput) { + # Invoke-PkCallback, inlined: this runs per result. + $pkCbDone = $false + $null = do { & $ResultHandler $r; $pkCbDone = $true } while ($false) + if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-ResultHandler' -PkContext $CallbackContext } + } } } else { $stillActive.Add($h) @@ -2534,7 +2574,12 @@ function Invoke-PkRunspaceJobs { $h.PS.Dispose() } if ($null -ne $pkWorkerOutput) { - foreach ($r in $pkWorkerOutput) { $null = Invoke-PkCallback -PkCallback $ResultHandler -PkArgs @(,$r) -PkName '-ResultHandler' -PkContext $pkCallbackContext } + foreach ($r in $pkWorkerOutput) { + # Invoke-PkCallback, inlined: this runs per result. + $pkCbDone = $false + $null = do { & $ResultHandler $r; $pkCbDone = $true } while ($false) + if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-ResultHandler' -PkContext $pkCallbackContext } + } } } else { $stillPending.Add($h)