From 38c1cc9f9cd222b025e25214281f30eaf93cca63 Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:26:31 -0500 Subject: [PATCH] Refuse an Invoke-PkProcessWithTimeout -TimeoutMs below 1 Co-Authored-By: Claude Opus 5.5 --- ParallelKit.Harness.ps1 | 7 +++++++ ParallelKit.ps1 | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index 370bfa3..8766a41 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -227,6 +227,13 @@ $sw = [System.Diagnostics.Stopwatch]::StartNew() try { $null = Invoke-PkProcessWithTimeout -FilePath $pingExe -Arguments '-n 30 127.0.0.1' -TimeoutMs 800 } catch { $threw = ($_.ToString() -like '*did not exit within*') } $sw.Stop() Test-Assert ($threw -and $sw.Elapsed.TotalSeconds -lt 6) "a process that overruns its time limit is killed and reported" +# A -TimeoutMs below 1 is refused before anything starts, not taken as a +# limit that's already used up. +foreach ($badMs in 0, -5) { + $badMsError = $null + try { $null = Invoke-PkProcessWithTimeout -FilePath $cmdExe -Arguments '/c exit 0' -TimeoutMs $badMs } catch { $badMsError = $_.ToString() } + Test-Assert ($badMsError -like '*-TimeoutMs must be at least 1*') ("-TimeoutMs " + $badMs + " is refused with a clear message (error: " + $badMsError + ")") +} $threw = $false try { $null = Start-PkProcess -FilePath 'C:\no\such\program.exe' } catch { $threw = ($_.ToString() -like "*could not start*") } Test-Assert $threw "a program that can't be started throws a clear error (not a silent null)" diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index cdfa9f1..9c3d34f 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -1255,7 +1255,7 @@ function Invoke-PkProcessWithTimeout { # Its command line: one string, passed exactly as written, or a list, each # element one argument, quoted for you (see ConvertTo-PkArgumentString). # .PARAMETER TimeoutMs - # The time limit in milliseconds (default 60000). + # 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). # .PARAMETER WorkingDirectory @@ -1279,6 +1279,8 @@ function Invoke-PkProcessWithTimeout { $OutputEncoding = $null, [switch]$IgnoreAbort ) + # 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.") } $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.