From c4fffbb28d585a252361bc8653b0f4808e469aef Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Wed, 2 Dec 2026 18:08:32 -0500 Subject: [PATCH 1/2] Start no more work once an abort is requested between the engines' checks Request-PkAbort and Ctrl+C only raise a flag; the engines stop on their token, which Test-PkAbortSignal cancels, and each main loop called it only after its launch step. So an abort requested before a runspace batch, or by a -ResultHandler, -NextJobProvider or the dispatcher's -PollCallback, still started a round of workers or programs first, and one raised by -JobFinalize had the dispatcher ask -GroupProvider for more groups. Both main loops now check the flag first, the dispatcher checks it again after -PollCallback, and the runspace engine's launch loop reads it per job. Harness section O covers each of those five cases. --- ParallelKit.Harness.ps1 | 42 +++++++++++++++++++++++++++++++++++++++++ ParallelKit.ps1 | 17 ++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index 0537669..1398b19 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -967,6 +967,48 @@ $out = Invoke-PkRunspaceBatch -MaxConcurrent 2 -JobParams (New-JobList -Jobs @(1 -WorkerScript { param([int]$Id) if (Test-PkCancelRequested) { return }; return $Id } -ResultHandler { param($r) $results.Add($r) }.GetNewClosure() Test-Assert (-not $out.Aborted -and $results.Count -eq 4) "after Reset-PkAbortState the next run is normal again" +# An abort requested between the engines' checks (Request-PkAbort with no +# Test-PkAbortSignal after it, or from a callback) starts nothing more. +Reset-Harness +Request-PkAbort +$results = [System.Collections.Generic.List[object]]::new() +$out = Invoke-PkRunspaceBatch -MaxConcurrent 4 -JobParams (New-JobList -Jobs @(1..8 | ForEach-Object { @{ Id = $_ } })) ` + -WorkerScript { param([int]$Id) return $Id } -ResultHandler { param($r) $results.Add($r) }.GetNewClosure() +Test-Assert ($out.Aborted -and $results.Count -eq 0) ("runspace jobs: an abort requested before the run starts no worker (ran " + $results.Count + " of 8)") + +Reset-Harness +$results = [System.Collections.Generic.List[object]]::new() +$out = Invoke-PkRunspaceBatch -MaxConcurrent 2 -JobParams (New-JobList -Jobs @(1..20 | ForEach-Object { @{ Id = $_ } })) ` + -WorkerScript { param([int]$Id) [System.Threading.Thread]::Sleep(300); return $Id } ` + -ResultHandler { param($r) $results.Add($r); if ($results.Count -eq 1) { Request-PkAbort } }.GetNewClosure() +Test-Assert ($out.Aborted -and $results.Count -eq 2) ("runspace jobs: the slots a -ResultHandler frees after requesting an abort aren't refilled (ran " + $results.Count + " of 20)") + +Reset-Harness +$results = [System.Collections.Generic.List[object]]::new() +$feed = @{ Next = 0 } +$out = Invoke-PkRunspaceBatch -MaxConcurrent 4 -WorkerScript { param([int]$Id) [System.Threading.Thread]::Sleep(300); return $Id } ` + -NextJobProvider { if ($feed.Next -ge 8) { return $null }; $feed.Next++; if ($feed.Next -eq 2) { Request-PkAbort }; return @{ Id = $feed.Next } }.GetNewClosure() ` + -ResultHandler { param($r) $results.Add($r) }.GetNewClosure() +Test-Assert ($out.Aborted -and $feed.Next -eq 2 -and $results.Count -eq 2) ("runspace jobs: -NextJobProvider isn't asked again once it requests an abort (asked " + $feed.Next + " times, ran " + $results.Count + ")") + +Reset-Harness +$starts = @{ Count = 0 }; $feed = @{ Given = $false }; $ticks = @{ Count = 0 } +$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++; Start-PkProcess -FilePath $job['FilePath'] -Arguments $job['Arguments'] }.GetNewClosure() ` + -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 +$late = @{ Groups = 0; Aborting = $false; LateBuilds = 0 } +$d = Invoke-PkProcessDispatch -MaxConcurrent 1 -MaxQueueAhead 1 -AbortTimeoutSeconds 5 ` + -GroupProvider { if ($late.Groups -ge 5) { return $null }; $late.Groups++; return $late.Groups }.GetNewClosure() ` + -JobBuilder { param($g) if ($late.Aborting) { $late.LateBuilds++ }; return @(@{ FilePath = $cmdExe; Arguments = '/c exit 0' }) }.GetNewClosure() ` + -JobFinalize { param($job, $pending) $null = Complete-PkProcess -Job $job; if (-not $late.Aborting) { $late.Aborting = $true; Request-PkAbort } }.GetNewClosure() +Test-Assert ($d.Aborted -and $late.Groups -eq 1 -and $late.LateBuilds -eq 0) ("dispatch: a -JobFinalize that requests an abort gets no more groups from -GroupProvider (asked for " + $late.Groups + " group(s), " + $late.LateBuilds + " built after the abort)") + # Your own long step inside Invoke-PkRun has to poll. Reset-Harness $own = @{ Steps = 0 } diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index 9cace24..763e8ba 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -2735,6 +2735,13 @@ function Invoke-PkProcessDispatchCore { while ((-not $pkCancelToken.IsCancellationRequested) -and ` (-not $pkProviderExhausted -or $pkActiveGroupCount -gt 0 -or $pkPending.Count -gt 0 -or $pkRunning.Count -gt 0)) { + # An abort only raises a flag; Test-PkAbortSignal is what cancels the + # token. Checked first, so an abort raised since the last pass (by + # -JobFinalize, -GroupCompleteCallback, Ctrl+C during the wait) asks + # -GroupProvider for nothing more. The flag read is cheaper than a + # function call. + if ([PkConsoleCtrl]::AbortRequested) { $null = Test-PkAbortSignal } + for ($pkH = $pkHeldGroups.Count - 1; $pkH -ge 0; $pkH--) { $pkReleased = Invoke-PkCallback -PkCallback $pkGroupReleaseCheck -PkArgs @(,$pkHeldGroups[$pkH]) -PkName '-GroupReleaseCheck' -PkContext $pkCallbackContext # Only the check's last value counts, as with -JobPollCompleted: a @@ -2802,6 +2809,8 @@ function Invoke-PkProcessDispatchCore { $pkCbDone = $false $null = do { & $pkPollCallback @($pkRunning); $pkCbDone = $true } while ($false) if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-PollCallback' -PkContext $pkCallbackContext } + # And again, so an abort it raised starts nothing on this pass. + if ([PkConsoleCtrl]::AbortRequested) { $null = Test-PkAbortSignal } } while ($pkPending.Count -gt 0 -and (-not $pkCancelToken.IsCancellationRequested) -and ` @@ -3803,7 +3812,13 @@ function Invoke-PkRunspaceBatch { while ((-not $scanExhausted -or $handles.Count -gt 0) -and -not $Cts.Token.IsCancellationRequested) { - while (-not $scanExhausted -and $handles.Count -lt $MaxConcurrent -and -not $Cts.Token.IsCancellationRequested) { + # An abort only raises a flag; Test-PkAbortSignal is what cancels + # the token. Checked before launching, so an abort raised since the + # last check (before the run, or by a -ResultHandler) starts + # nothing more, and per job, for one -NextJobProvider raises. The + # flag read is cheaper than a function call. + if ([PkConsoleCtrl]::AbortRequested) { $null = Test-PkAbortSignal } + while (-not $scanExhausted -and $handles.Count -lt $MaxConcurrent -and -not $Cts.Token.IsCancellationRequested -and -not [PkConsoleCtrl]::AbortRequested) { if ($null -ne $JobParams) { if ($fixedListIndex -lt $JobParams.Count) { $nextJob = $JobParams[$fixedListIndex] From 32dd726fa03d9540f8be6cb2646ea02af281d2b2 Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Wed, 2 Dec 2026 18:24:23 -0500 Subject: [PATCH 2/2] Report every batch command as aborted when an abort is in force before it starts With the dispatcher now checking for an abort before it asks for any group, a batch started while an abort is in force never handed its commands to the dispatcher, so they came back with no results. The batch now reports each one as aborted itself. The harness's aborted-group callback test raises its abort once the group is built, since one already in force means no group is asked for, and section O checks that case for the dispatcher. --- ParallelKit.Harness.ps1 | 10 ++++++++-- ParallelKit.ps1 | 7 +++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index 1398b19..8bcbfbf 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -975,6 +975,11 @@ $results = [System.Collections.Generic.List[object]]::new() $out = Invoke-PkRunspaceBatch -MaxConcurrent 4 -JobParams (New-JobList -Jobs @(1..8 | ForEach-Object { @{ Id = $_ } })) ` -WorkerScript { param([int]$Id) return $Id } -ResultHandler { param($r) $results.Add($r) }.GetNewClosure() Test-Assert ($out.Aborted -and $results.Count -eq 0) ("runspace jobs: an abort requested before the run starts no worker (ran " + $results.Count + " of 8)") +Reset-Harness +Request-PkAbort +$asked = @{ Count = 0 } +$d = Invoke-PkProcessDispatch -MaxConcurrent 1 -GroupProvider { $asked.Count++; return $null }.GetNewClosure() -JobBuilder { param($g) } -JobFinalize { param($job, $pending) } +Test-Assert ($d.Aborted -and $asked.Count -eq 0) ("dispatch: an abort requested before the run asks -GroupProvider for nothing (asked " + $asked.Count + " times)") Reset-Harness $results = [System.Collections.Generic.List[object]]::new() @@ -1767,13 +1772,14 @@ Test-Assert $threw "a negative -TimeoutSeconds is refused" # An aborted group still gets -GroupCompleteCallback (the README warns about # this - a caller zipping a folder there must check for the abort first). +# The abort comes once the group is built: one already in force means no +# group is asked for at all. Reset-Harness -Request-PkAbort $called = @{ Count = 0; SawAbort = $false } $feed = @{ Given = $false } $d = Invoke-PkProcessDispatch -MaxConcurrent 1 ` -GroupProvider { if ($feed.Given) { return $null }; $feed.Given = $true; return 'g' }.GetNewClosure() ` - -JobBuilder { param($g) return @(@{ FilePath = $cmdExe; Arguments = '/c exit 0' }) }.GetNewClosure() ` + -JobBuilder { param($g) Request-PkAbort; return @(@{ FilePath = $cmdExe; Arguments = '/c exit 0' }) }.GetNewClosure() ` -JobFinalize { param($job, $pending) $null = Complete-PkProcess -Job $job } ` -GroupCompleteCallback { param($g) $called.Count++; $called.SawAbort = [bool](Test-PkCancelRequested) }.GetNewClosure() Test-Assert ($d.Aborted -and $called.Count -eq 1 -and $called.SawAbort) "an aborted group still gets its -GroupCompleteCallback, and Test-PkCancelRequested is true inside it" diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index 763e8ba..e03127c 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -3396,6 +3396,13 @@ function Invoke-PkProcessBatch { Write-PkMessage ("Invoke-PkProcessBatch: " + $pkFailure.More + " more " + $pkCommandWord + " couldn't start '" + $pkFailure.FilePath + "' (" + $pkFailure.Cause + ").") -Level Error } } + # An abort already in force stops the dispatcher before it asks for the + # batch's one group, so no command reached it: each was kept from starting. + if (-not $pkState.Given) { + foreach ($pkC in $pkCommands) { + if ($null -ne $pkC) { $pkResults.Add((& $pkNewBatchResult -Command $pkC -Aborted)) } + } + } return [pscustomobject][ordered]@{ Results = $pkResults; Aborted = $pkDispatch.Aborted; ForceKilledCount = $pkDispatch.ForceKilledCount } }