diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index 2f00ceb..d31f838 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 } @@ -3464,7 +3482,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 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 { 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`. |