From e326d62eab24a760f73c5913e6d8b5fae8335e59 Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:04:47 -0500 Subject: [PATCH 1/2] Refuse time limits that can't work: NaN/Infinity -TimeoutSeconds, negative waits -TimeoutSeconds on the batch and dispatcher only checked `-lt 0`, so NaN and +Infinity passed and acted as no limit, while a job's own TimeoutSeconds key refuses both. Negative waits weren't refused anywhere: -StreamTimeoutMs -1 and Wait-PkProcess -TimeoutMs -1 waited without limit, -2 failed later with .NET's own message, and a negative -AbortTimeoutSeconds / -DrainTimeoutSeconds acted as 0. All are now refused before anything starts, with a message naming the command and parameter. 0 keeps its meaning (no limit, or don't wait). The batch's and dispatcher's twin checks name each other. Co-Authored-By: Claude Opus 5.5 --- ParallelKit.Harness.ps1 | 50 +++++++++++++++++++++++++++++++++++++++++ ParallelKit.ps1 | 34 ++++++++++++++++++++++++---- 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index aa1d72d..000939c 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -4156,6 +4156,56 @@ foreach ($njCase in 'two jobs', 'job then null') { } Write-Host "" +Write-Host "=== AU: time limits that can't work are refused up front ===" -ForegroundColor Cyan +# NaN and +Infinity for -TimeoutSeconds acted as no limit; a negative wait +# either waited until the program (or the leftover child holding its output) +# ended, defeating the parameter, or failed later with .NET's own message. +# Each must be refused before anything starts, naming the command and the +# parameter. 0 stays meaningful: no limit, or don't wait. +Reset-Harness +$tlJob = @{ FilePath = $cmdExe; Arguments = '/d /c exit 0' } +$tlDispatch = @{ + MaxConcurrent = 1 + GroupProvider = { return $null } + JobBuilder = { param($g) return @() } + JobFinalize = { param($job, $pending) } +} +$tlCases = [ordered]@{ + 'Invoke-PkProcessBatch -TimeoutSeconds NaN' = @{ Expect = 'Invoke-PkProcessBatch: -TimeoutSeconds'; Run = { $null = Invoke-PkProcessBatch -Commands @($tlJob) -TimeoutSeconds ([double]::NaN) } } + 'Invoke-PkProcessBatch -TimeoutSeconds Infinity' = @{ Expect = 'Invoke-PkProcessBatch: -TimeoutSeconds'; Run = { $null = Invoke-PkProcessBatch -Commands @($tlJob) -TimeoutSeconds ([double]::PositiveInfinity) } } + 'Invoke-PkProcessDispatch -TimeoutSeconds NaN' = @{ Expect = 'Invoke-PkProcessDispatch: -TimeoutSeconds'; Run = { $null = Invoke-PkProcessDispatch @tlDispatch -TimeoutSeconds ([double]::NaN) } } + 'Invoke-PkProcessDispatch -TimeoutSeconds Infinity' = @{ Expect = 'Invoke-PkProcessDispatch: -TimeoutSeconds'; Run = { $null = Invoke-PkProcessDispatch @tlDispatch -TimeoutSeconds ([double]::PositiveInfinity) } } + 'Invoke-PkProcessBatch -AbortTimeoutSeconds -5' = @{ Expect = 'Invoke-PkProcessBatch: -AbortTimeoutSeconds'; Run = { $null = Invoke-PkProcessBatch -Commands @($tlJob) -AbortTimeoutSeconds -5 } } + 'Invoke-PkProcessDispatch -AbortTimeoutSeconds -5' = @{ Expect = 'Invoke-PkProcessDispatch: -AbortTimeoutSeconds'; Run = { $null = Invoke-PkProcessDispatch @tlDispatch -AbortTimeoutSeconds -5 } } + 'Invoke-PkRunspaceJobs -DrainTimeoutSeconds -5' = @{ Expect = 'Invoke-PkRunspaceJobs: -DrainTimeoutSeconds'; Run = { $null = Invoke-PkRunspaceJobs -WorkerScript { param($Id) $Id } -JobParams @(@{ Id = 1 }) -MaxParallel 1 -DrainTimeoutSeconds -5 } } + 'Invoke-PkProcessWithTimeout -StreamTimeoutMs -1' = @{ Expect = 'Invoke-PkProcessWithTimeout: -StreamTimeoutMs'; Run = { $null = Invoke-PkProcessWithTimeout -FilePath $cmdExe -Arguments '/d /c exit 0' -StreamTimeoutMs -1 } } + 'Complete-PkProcess -StreamTimeoutMs -1' = @{ Expect = 'Complete-PkProcess: -StreamTimeoutMs'; Started = $true; Run = { param($s) $null = Complete-PkProcess $s -StreamTimeoutMs -1 } } + 'Wait-PkProcess -TimeoutMs -1' = @{ Expect = 'Wait-PkProcess: -TimeoutMs'; Started = $true; Run = { param($s) $null = Wait-PkProcess -Process $s['Proc'] -TimeoutMs -1 } } +} +foreach ($tlName in $tlCases.Keys) { + $tlCase = $tlCases[$tlName] + $tlError = $null + $tlStarted = $null + if ($tlCase.ContainsKey('Started')) { $tlStarted = Start-PkProcess -FilePath $cmdExe -Arguments '/d /c exit 0' } + $sw = [System.Diagnostics.Stopwatch]::StartNew() + try { & $tlCase.Run $tlStarted } catch { $tlError = $_.ToString() } + $sw.Stop() + if ($null -ne $tlStarted) { $null = Complete-PkProcess $tlStarted -Dispose } + Test-Assert ($null -ne $tlError -and $tlError.StartsWith($tlCase.Expect + ' must be') -and $sw.Elapsed.TotalSeconds -lt 2) ($tlName + " is refused up front with a clear message (error: " + $tlError + ")") +} +# 0 is still taken where it means something: don't wait. +$s = Start-PkProcess -FilePath $cmdExe -Arguments '/d /c exit 0' +$tlZero = $null +try { + $null = Wait-PkProcess -Process $s['Proc'] -TimeoutMs 0 + $null = Wait-PkProcess -Process $s['Proc'] -TimeoutMs 5000 + $null = Complete-PkProcess $s -StreamTimeoutMs 0 -Dispose + $null = Invoke-PkProcessBatch -Commands @($tlJob) -AbortTimeoutSeconds 0 -TimeoutSeconds 0 + $null = Invoke-PkRunspaceJobs -WorkerScript { param($Id) $Id } -JobParams @(@{ Id = 1 }) -MaxParallel 1 -DrainTimeoutSeconds 0 +} catch { $tlZero = $_.ToString() } +Test-Assert ($null -eq $tlZero) ("0 is still accepted for -TimeoutMs, -StreamTimeoutMs, -AbortTimeoutSeconds, -DrainTimeoutSeconds and -TimeoutSeconds (error: " + $tlZero + ")") +Write-Host "" + Set-PkMessageHandler -Handler $null Reset-PkAbortState diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index 5050edf..08047aa 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -1500,6 +1500,8 @@ function Wait-PkProcess { [Parameter(Mandatory)] [int]$TimeoutMs ) + # -1 would wait until the program exits, forever if it never does. + if ($TimeoutMs -lt 0) { throw ("Wait-PkProcess: -TimeoutMs must be 0 or more (got " + $TimeoutMs + "). It's how long to wait, in milliseconds; 0 just checks.") } # Same handle wrapping as Wait-PkRunningJobs. $waitHandle = [System.Threading.ManualResetEvent]::new($false) $origHandle = $waitHandle.SafeWaitHandle @@ -1581,6 +1583,12 @@ function Complete-PkProcess { [int]$StreamTimeoutMs = 15000, [switch]$Dispose ) + # A negative wait isn't a limit: -1 waited until the output closed, however + # long a leftover child held it. Invoke-PkProcessWithTimeout checks it too, + # before starting its program. + if ($StreamTimeoutMs -lt 0) { + throw ("Complete-PkProcess: -StreamTimeoutMs must be 0 or more (got " + $StreamTimeoutMs + "). It's how long to wait for the output after the program exits, in milliseconds; 0 doesn't wait.") + } $result = [ordered]@{ ExitCode = $null; Stdout = $null; Stderr = $null; StreamTimedOut = $false; StreamError = $null } # 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 @@ -1642,6 +1650,8 @@ function Invoke-PkProcessWithTimeout { ) # Checked before starting anything: 0 or less would kill the program at once. if ($TimeoutMs -lt 1) { throw ("Invoke-PkProcessWithTimeout: -TimeoutMs must be at least 1 (got " + $TimeoutMs + "). It's the program's time limit in milliseconds.") } + # Complete-PkProcess makes the same check, but only after the program has run. + if ($StreamTimeoutMs -lt 0) { throw ("Invoke-PkProcessWithTimeout: -StreamTimeoutMs must be 0 or more (got " + $StreamTimeoutMs + "). It's how long to wait for the output after the program exits, in milliseconds; 0 doesn't wait.") } $job = Start-PkProcessCore -FilePath $FilePath -Arguments $Arguments -WorkingDirectory $WorkingDirectory -OutputEncoding $OutputEncoding -CallerState $PSCmdlet.SessionState try { # Short slices, so an abort or cancel is noticed within a moment. @@ -1796,8 +1806,15 @@ function Invoke-PkProcessDispatch { $pkMaxConcurrent = $pkMaxConcurrentInt } if ($pkMaxQueueAhead -lt 1) { throw "Invoke-PkProcessDispatch: -MaxQueueAhead must be at least 1." } - # 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." } + # Invoke-PkProcessBatch makes the same -MaxConcurrent, -TimeoutSeconds and + # -AbortTimeoutSeconds checks, so its errors name the batch: keep the two in + # step. NaN and +Infinity pass a `-lt 0` test and acted as no limit. + if ([double]::IsNaN($pkTimeoutSeconds) -or [double]::IsInfinity($pkTimeoutSeconds) -or $pkTimeoutSeconds -lt 0) { + throw ("Invoke-PkProcessDispatch: -TimeoutSeconds must be a number of seconds, 0 (no limit) or more (got " + $pkTimeoutSeconds + ").") + } + if ($pkAbortTimeoutSeconds -lt 0) { + throw ("Invoke-PkProcessDispatch: -AbortTimeoutSeconds must be 0 or more (got " + $pkAbortTimeoutSeconds + "). It's how long an abort waits for running jobs before force-stopping them; 0 doesn't wait.") + } # Size the thread pool up front. For a shared budget, its free slots now # are the best measure of its size. @@ -2435,9 +2452,15 @@ 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 two checks; these name the batch. + # Invoke-PkProcessDispatch makes the same three checks; these name the + # batch. Keep the two in step. if ($pkMaxConcurrent -lt 1) { throw ("Invoke-PkProcessBatch: -MaxConcurrent must be at least 1 (got " + $pkMaxConcurrent + ").") } - if ($pkTimeoutSeconds -lt 0) { throw "Invoke-PkProcessBatch: -TimeoutSeconds must be 0 (no limit) or more." } + if ([double]::IsNaN($pkTimeoutSeconds) -or [double]::IsInfinity($pkTimeoutSeconds) -or $pkTimeoutSeconds -lt 0) { + throw ("Invoke-PkProcessBatch: -TimeoutSeconds must be a number of seconds, 0 (no limit) or more (got " + $pkTimeoutSeconds + ").") + } + if ($pkAbortTimeoutSeconds -lt 0) { + throw ("Invoke-PkProcessBatch: -AbortTimeoutSeconds must be 0 or more (got " + $pkAbortTimeoutSeconds + "). It's how long an abort waits for running programs before force-stopping them; 0 doesn't wait.") + } $pkResults = [System.Collections.Generic.List[object]]::new() $pkState = @{ Given = $false } @@ -2803,6 +2826,9 @@ function Invoke-PkRunspaceJobs { throw "Invoke-PkRunspaceJobs: supply only one of -JobParams or -NextJobProvider, not both." } if ($MaxParallel -lt 1) { throw "Invoke-PkRunspaceJobs: -MaxParallel must be at least 1." } + if ($DrainTimeoutSeconds -lt 0) { + throw ("Invoke-PkRunspaceJobs: -DrainTimeoutSeconds must be 0 or more (got " + $DrainTimeoutSeconds + "). It's how long an abort waits for running workers before force-stopping them; 0 doesn't wait.") + } # A List, an array or a single hashtable: a one-job list built with # foreach collapses to a bare hashtable. From 6558bed3ee009d25b3080bac6ed2a6fb2e2a8caf Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:11:32 -0500 Subject: [PATCH 2/2] Name the timeout checks' twins; say 0 or more in the wait parameters' help Co-Authored-By: Claude Opus 5.5 --- ParallelKit.ps1 | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index 08047aa..546a51c 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -1450,7 +1450,8 @@ function Stop-PkJobProcesses { function Get-PkJobTimeLimit { # A dispatcher job's time limit in seconds: its own TimeoutSeconds key if it - # has one, else the default. 0 = no limit. Throws on a bad key. + # has one, else the default. 0 = no limit. Throws on a bad key. The batch + # and dispatcher check their -TimeoutSeconds by the same rule. param([hashtable]$Job, [double]$DefaultTimeoutSeconds = 0) $value = $Job['TimeoutSeconds'] if ($null -eq $value) { return $DefaultTimeoutSeconds } @@ -1491,7 +1492,7 @@ function Wait-PkProcess { # .PARAMETER Process # The program, such as the Proc that Start-PkProcess returns. # .PARAMETER TimeoutMs - # How long to wait, in milliseconds. + # How long to wait, in milliseconds; 0 or more, and 0 just checks. # .OUTPUTS # [bool] $true if it exited in time. param( @@ -1568,7 +1569,8 @@ function Complete-PkProcess { # .PARAMETER Job # What Start-PkProcess returned, or a dispatcher job with the same keys. # .PARAMETER StreamTimeoutMs - # How long to wait for the output after the program exits (default 15000). + # How long to wait for the output after the program exits, + # in milliseconds (default 15000); 0 or more, and 0 doesn't wait. # .PARAMETER Dispose # Dispose the process object afterwards. # .OUTPUTS @@ -1626,7 +1628,8 @@ function Invoke-PkProcessWithTimeout { # .PARAMETER TimeoutMs # The time limit in milliseconds (default 60000), at least 1. # .PARAMETER StreamTimeoutMs - # How long to wait for the output after the program exits (default 15000). + # How long to wait for the output after the program exits, + # in milliseconds (default 15000); 0 or more, and 0 doesn't wait. # .PARAMETER WorkingDirectory # Where to run it, as for Start-PkProcess. Default: your current location. # .PARAMETER OutputEncoding @@ -1808,7 +1811,8 @@ function Invoke-PkProcessDispatch { if ($pkMaxQueueAhead -lt 1) { throw "Invoke-PkProcessDispatch: -MaxQueueAhead must be at least 1." } # Invoke-PkProcessBatch makes the same -MaxConcurrent, -TimeoutSeconds and # -AbortTimeoutSeconds checks, so its errors name the batch: keep the two in - # step. NaN and +Infinity pass a `-lt 0` test and acted as no limit. + # step. NaN and +Infinity pass a `-lt 0` test and acted as no limit. A job's + # own TimeoutSeconds key follows the same rule (Get-PkJobTimeLimit). if ([double]::IsNaN($pkTimeoutSeconds) -or [double]::IsInfinity($pkTimeoutSeconds) -or $pkTimeoutSeconds -lt 0) { throw ("Invoke-PkProcessDispatch: -TimeoutSeconds must be a number of seconds, 0 (no limit) or more (got " + $pkTimeoutSeconds + ").") } @@ -2453,7 +2457,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 makes the same three checks; these name the - # batch. Keep the two in step. + # batch. Keep the two in step. Get-PkJobTimeLimit checks a job's own + # TimeoutSeconds key by the same rule. if ($pkMaxConcurrent -lt 1) { throw ("Invoke-PkProcessBatch: -MaxConcurrent must be at least 1 (got " + $pkMaxConcurrent + ").") } if ([double]::IsNaN($pkTimeoutSeconds) -or [double]::IsInfinity($pkTimeoutSeconds) -or $pkTimeoutSeconds -lt 0) { throw ("Invoke-PkProcessBatch: -TimeoutSeconds must be a number of seconds, 0 (no limit) or more (got " + $pkTimeoutSeconds + ").")