Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions ParallelKit.Harness.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
47 changes: 39 additions & 8 deletions ParallelKit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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(
Expand All @@ -1500,6 +1501,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
Expand Down Expand Up @@ -1566,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
Expand All @@ -1581,6 +1585,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
Expand Down Expand Up @@ -1618,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
Expand All @@ -1642,6 +1653,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.
Expand Down Expand Up @@ -1796,8 +1809,16 @@ 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. 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 + ").")
}
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.
Expand Down Expand Up @@ -2435,9 +2456,16 @@ 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. 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 ($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 }
Expand Down Expand Up @@ -2803,6 +2831,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.
Expand Down
Loading