From 9e3e2c8f4fad11547107dd9b3ab4720edfbe729e Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Wed, 2 Dec 2026 20:08:14 -0500 Subject: [PATCH 1/3] README: the ConvertTo-PkArgumentString row names cmd.exe among the programs a list is quoted for --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ba7ac7f..156fa59 100644 --- a/README.md +++ b/README.md @@ -619,7 +619,7 @@ For one program at a time, outside the engines. All of these are safe in worker | Function | Use | |---|---| | `Invoke-PkProcessWithTimeout -FilePath -Arguments -TimeoutMs` | Run one program and wait, with a hard time limit (default 60s). Returns the [process result](#process-results) fields marked "all". If it overruns, or the run is aborted, the program is killed and this **throws**; called while an abort or cancel is already in force, it throws without starting the program. Add `-IgnoreAbort` for a cleanup step that must run anyway. | -| `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`. | +| `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 `cmd.exe`, a `.bat` or a `.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`. | From 40b1f0a09ab2a7b7ff60510a0960452bc7bb1080 Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Wed, 2 Dec 2026 20:22:48 -0500 Subject: [PATCH 2/3] Check for an abort before each start in the dispatcher The dispatcher checks for an abort once per pass, and its start loop read only the token, which that check cancels. So a Ctrl+C landing while the loop was starting programs, or a -JobStarter that requested an abort, still filled every free slot on that pass. The start loop now also reads the abort flag before each job, ahead of the shared budget's Wait(0) so no slot is taken for a job that won't start. Harness section O covers a -JobStarter that requests an abort, with a plain limit and with a shared budget. --- ParallelKit.Harness.ps1 | 18 ++++++++++++++++++ ParallelKit.ps1 | 5 ++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index 2f00ceb..d608450 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -1005,6 +1005,24 @@ $d = Invoke-PkProcessDispatch -MaxConcurrent 3 -AbortTimeoutSeconds 5 ` -JobFinalize { param($job, $pending) $null = Complete-PkProcess -Job $job } ` -PollCallback { param($running) $ticks.Count++; if ($ticks.Count -eq 1) { Request-PkAbort } }.GetNewClosure() Test-Assert ($d.Aborted -and $starts.Count -eq 0) ("dispatch: a -PollCallback that requests an abort starts nothing more on that pass (started " + $starts.Count + " of 6)") +Reset-Harness +$starts = @{ Count = 0 }; $feed = @{ Given = $false } +$d = Invoke-PkProcessDispatch -MaxConcurrent 3 -AbortTimeoutSeconds 5 ` + -GroupProvider { if ($feed.Given) { return $null }; $feed.Given = $true; return 'g' }.GetNewClosure() ` + -JobBuilder { param($g) return @(1..6 | ForEach-Object { @{ FilePath = $cmdExe; Arguments = '/c exit 0' } }) }.GetNewClosure() ` + -JobStarter { param($job) $starts.Count++; if ($starts.Count -eq 1) { Request-PkAbort }; Start-PkProcess -FilePath $job['FilePath'] -Arguments $job['Arguments'] }.GetNewClosure() ` + -JobFinalize { param($job, $pending) $null = Complete-PkProcess -Job $job } +Test-Assert ($d.Aborted -and $starts.Count -eq 1) ("dispatch: a -JobStarter that requests an abort starts nothing more after its own program (started " + $starts.Count + " of 6)") +Reset-Harness +$budget = [System.Threading.SemaphoreSlim]::new(3, 3) +$starts = @{ Count = 0 }; $feed = @{ Given = $false } +$d = Invoke-PkProcessDispatch -MaxConcurrent $budget -AbortTimeoutSeconds 5 ` + -GroupProvider { if ($feed.Given) { return $null }; $feed.Given = $true; return 'g' }.GetNewClosure() ` + -JobBuilder { param($g) return @(1..6 | ForEach-Object { @{ FilePath = $cmdExe; Arguments = '/c exit 0' } }) }.GetNewClosure() ` + -JobStarter { param($job) $starts.Count++; if ($starts.Count -eq 1) { Request-PkAbort }; Start-PkProcess -FilePath $job['FilePath'] -Arguments $job['Arguments'] }.GetNewClosure() ` + -JobFinalize { param($job, $pending) $null = Complete-PkProcess -Job $job } +Test-Assert ($d.Aborted -and $starts.Count -eq 1 -and $budget.CurrentCount -eq 3) ("and on a shared budget, without taking a slot it doesn't use (started " + $starts.Count + " of 6, " + $budget.CurrentCount + " of 3 slots free after)") +$budget.Dispose() Reset-Harness $late = @{ Groups = 0; Aborting = $false; LateBuilds = 0 } diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index 6895836..2c63253 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -2844,7 +2844,10 @@ function Invoke-PkProcessDispatchCore { if ([PkConsoleCtrl]::AbortRequested) { $null = Test-PkAbortSignal } } - while ($pkPending.Count -gt 0 -and (-not $pkCancelToken.IsCancellationRequested) -and ` + # The abort flag per job too, so a Ctrl+C or a -JobStarter's abort + # starts no more on this pass; before the shared budget's Wait(0), + # which would take a slot. + while ($pkPending.Count -gt 0 -and (-not $pkCancelToken.IsCancellationRequested) -and (-not [PkConsoleCtrl]::AbortRequested) -and ` $(if ($pkIsSharedBudget) { $pkMaxConcurrent.Wait(0) } else { $pkRunning.Count -lt $pkMaxConcurrent })) { $pkJob = $pkPending.Dequeue() try { From c82dfd3693efb9b14095511bf1e8287fc7679cac Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Wed, 2 Dec 2026 20:36:52 -0500 Subject: [PATCH 3/3] Harness: wait for a killed tree to go before counting what's left The test that kills a live tree listed beside a disposed process counted leftover pings right after Stop-PkProcessTree returned. Ending a Job Object returns before Windows has finished tearing its processes down, so on a CI runner the ping was still listed once. It now waits up to 5 seconds for it to go, as the other kill tests do. --- ParallelKit.Harness.ps1 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index 2f00ceb..6e12203 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -3464,7 +3464,10 @@ while ((Get-LimitPingCount 53) -lt 1 -and $dispWait.Elapsed.TotalSeconds -lt 5) $dispThrew = $null $dispKilled = $false try { $dispKilled = Stop-PkProcessTree -Process @($dispGone.Proc, $dispLive.Proc) } catch { $dispThrew = [string]$_ } -$dispLeft = Get-LimitPingCount 53 +# Ending a Job Object returns before Windows has torn its processes down: +# give the ping a moment to go. +$dispWait = [System.Diagnostics.Stopwatch]::StartNew() +do { $dispLeft = Get-LimitPingCount 53; if ($dispLeft -eq 0) { break }; Start-Sleep -Milliseconds 100 } while ($dispWait.Elapsed.TotalSeconds -lt 5) Stop-StrayPings 53 Test-Assert ($null -eq $dispThrew -and $dispKilled -and $dispLeft -eq 0 -and $dispLive.Proc.HasExited) ("Stop-PkProcessTree kills a live tree listed beside a disposed process (" + $dispLeft + " left, returned " + $dispKilled + ", threw: " + $dispThrew + ")") try { if (-not $dispLive.Proc.HasExited) { $dispLive.Proc.Kill() } } catch { } # cleanup after a failure; already-exited is fine