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
72 changes: 72 additions & 0 deletions ParallelKit.Harness.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4611,6 +4611,78 @@ $r = Complete-PkProcess @{ Proc = $s['Proc']; StdoutTask = $baFailed.Task; Stder
Test-Assert (-not $r.StreamTimedOut -and $r.StreamError -eq 'Test: the read failed.' -and $null -eq $r.Stdout) ("a failed read next to one that ended gives the read's own reason (StreamError '" + $r.StreamError + "')")
Write-Host ""

Write-Host "=== BB: a job waiting for its output doesn't hold up the others ===" -ForegroundColor Cyan
# Each held command's cmd exits at once and leaves a ping holding its output
# for a few seconds, so its output ends well inside the stream timeout. The
# wait for it used to run inside -JobFinalize, on the dispatch loop's own
# thread: no other job was polled, started, timed or stopped meanwhile. The
# pings carry -w 4321 so this section can find and kill its own.
Reset-Harness
function Stop-HarnessTaggedPings {
foreach ($p in @(Get-CimInstance Win32_Process -Filter "Name='PING.EXE' AND CommandLine LIKE '%-w 4321 %'")) { Stop-Process -Id $p.ProcessId -Force -ErrorAction SilentlyContinue }
}
function New-HarnessHeldCommand([int]$Seconds, $Key) {
return @{ FilePath = $cmdExe; Arguments = ('/d /c echo held& start /b ping -n ' + ($Seconds + 1) + ' -w 4321 127.0.0.1 >nul'); Key = $Key }
}
try {
# Two slots: one job waits about 6 s for its output while four 1 s
# programs go through the other slot, finishing by about 4 s.
$bbAt = [System.Collections.Generic.List[double]]::new()
$sw = [System.Diagnostics.Stopwatch]::StartNew()
$bbCommands = @(New-HarnessHeldCommand -Seconds 6 -Key 'held') + @(1..4 | ForEach-Object { @{ FilePath = $cmdExe; Arguments = '/d /c ping -n 2 127.0.0.1 >nul'; Key = 'quick' } })
$b = Invoke-PkProcessBatch -MaxConcurrent 2 -Commands $bbCommands -OnComplete { param($c, $r) if ($c['Key'] -eq 'quick') { $bbAt.Add($sw.Elapsed.TotalSeconds) } }.GetNewClosure()
$sw.Stop()
$bbLast = $null; if ($bbAt.Count -gt 0) { $bbLast = [math]::Round(($bbAt | Measure-Object -Maximum).Maximum, 1) }
$bbHeld = @($b.Results | Where-Object { $_.Command['Key'] -eq 'held' -and $_.Stdout -match 'held' -and -not $_.StreamTimedOut })
Test-Assert ($bbAt.Count -eq 4 -and $bbLast -lt 5.5 -and $bbHeld.Count -eq 1) ("batch: other programs keep running through the free slot while one waits for its output (last of 4 done at " + $bbLast + " s; the held one whole: " + ($bbHeld.Count -eq 1) + ")")

# One job waits for its output while the other passes its time limit:
# the limit is still enforced on time.
$b = Invoke-PkProcessBatch -MaxConcurrent 2 -Commands @((New-HarnessHeldCommand -Seconds 6 -Key 'held'), @{ FilePath = $pingExe; Arguments = '-n 30 127.0.0.1'; TimeoutSeconds = 2; Key = 'limited' })
$bbLimited = @($b.Results | Where-Object { $_.Command['Key'] -eq 'limited' })
$bbKilledAt = $null; if ($bbLimited.Count -eq 1 -and $null -ne $bbLimited[0].Duration) { $bbKilledAt = [math]::Round($bbLimited[0].Duration.TotalSeconds, 1) }
Test-Assert ($bbLimited.Count -eq 1 -and $bbLimited[0].TimedOut -and $null -ne $bbKilledAt -and $bbKilledAt -lt 3.5) ("batch: a program's 2 s time limit is enforced while another waits for its output (killed at " + $bbKilledAt + " s)")

# An abort while a job waits for its output: acted on at once, and after
# the abort's wait the job keeps what arrived rather than counting as
# force-stopped (its program had finished).
Reset-PkAbortState
$sw = [System.Diagnostics.Stopwatch]::StartNew()
$b = Invoke-PkProcessBatch -MaxConcurrent 2 -AbortTimeoutSeconds 1 -OnComplete { param($c, $r) if ($c['Key'] -eq 'quick') { Request-PkAbort } } -Commands @((New-HarnessHeldCommand -Seconds 8 -Key 'held'), @{ FilePath = $cmdExe; Arguments = '/d /c ping -n 2 127.0.0.1 >nul'; Key = 'quick' })
$sw.Stop()
Reset-PkAbortState
$bbHeld = @($b.Results | Where-Object { $_.Command['Key'] -eq 'held' })
$bbHeldText = 'no result'
if ($bbHeld.Count -eq 1) { $bbHeldText = "Aborted " + $bbHeld[0].Aborted + ", StreamTimedOut " + $bbHeld[0].StreamTimedOut + ", Stdout '" + ([string]$bbHeld[0].Stdout).Trim() + "'" }
Test-Assert ($b.Aborted -and $sw.Elapsed.TotalSeconds -lt 5 -and $bbHeld.Count -eq 1 -and -not $bbHeld[0].Aborted -and $bbHeld[0].StreamTimedOut -and $bbHeld[0].Stdout -match 'held' -and $b.ForceKilledCount -eq 0) ("batch: an abort while a job waits for its output ends the run in time, and that job keeps what arrived (" + [math]::Round($sw.Elapsed.TotalSeconds, 1) + " s, " + $bbHeldText + ", force-stopped " + $b.ForceKilledCount + ")")

# Output held past the 15 s stream timeout: the poll gives up at 15 s, and
# Complete-PkProcess doesn't wait another 15 on top.
$sw = [System.Diagnostics.Stopwatch]::StartNew()
$b = Invoke-PkProcessBatch -Commands @(New-HarnessHeldCommand -Seconds 25 -Key 'long')
$sw.Stop()
$bbLong = @($b.Results)
Test-Assert ($bbLong.Count -eq 1 -and $bbLong[0].StreamTimedOut -and $bbLong[0].Stdout -match 'held' -and $sw.Elapsed.TotalSeconds -ge 14 -and $sw.Elapsed.TotalSeconds -lt 20) ("batch: output held past the stream timeout is given up on once, after 15 s (" + [math]::Round($sw.Elapsed.TotalSeconds, 1) + " s)")
Stop-HarnessTaggedPings

# The dispatcher's default poll, with Complete-PkProcess in -JobFinalize:
# the same time-limit case.
$bbFeed = @{ Given = $false }
$bbJobs = @((New-HarnessHeldCommand -Seconds 6 -Key 'held'), @{ FilePath = $pingExe; Arguments = '-n 30 127.0.0.1'; TimeoutSeconds = 2; Key = 'limited' })
$bbDone = @{}
$null = Invoke-PkProcessDispatch -MaxConcurrent 2 `
-GroupProvider { if ($bbFeed.Given) { return $null }; $bbFeed.Given = $true; return @{ Name = 'held' } }.GetNewClosure() `
-JobBuilder { param($g) return $bbJobs }.GetNewClosure() `
-JobFinalize { param($j, $q) $bbDone[$j['Key']] = @{ Result = (Complete-PkProcess -Job $j); TimedOut = $j['TimedOut'] } }.GetNewClosure()
$bbKilledAt = $null; if ($bbDone.ContainsKey('limited') -and $null -ne $bbDone['limited'].Result.Duration) { $bbKilledAt = [math]::Round($bbDone['limited'].Result.Duration.TotalSeconds, 1) }
$bbWhole = ($bbDone.ContainsKey('held') -and $bbDone['held'].Result.Stdout -match 'held' -and -not $bbDone['held'].Result.StreamTimedOut)
Test-Assert ($bbDone.Count -eq 2 -and $bbDone['limited'].TimedOut -and $null -ne $bbKilledAt -and $bbKilledAt -lt 3.5 -and $bbWhole) ("dispatcher: with the default poll, a 2 s time limit is enforced while another job waits for its output (killed at " + $bbKilledAt + " s; the held one whole: " + $bbWhole + ")")
} finally {
Stop-HarnessTaggedPings
Reset-PkAbortState
}
Write-Host ""

Set-PkMessageHandler -Handler $null
Reset-PkAbortState

Expand Down
51 changes: 45 additions & 6 deletions ParallelKit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1791,7 +1791,9 @@ function Complete-PkProcess {
# 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,
# in milliseconds (default 15000); 0 or more, and 0 doesn't wait.
# in milliseconds (default 15000); 0 or more, and 0 doesn't wait. On a job
# the dispatcher's default -JobPollCompleted has already waited on, only
# what's left of it, so with the default nothing.
# .PARAMETER Dispose
# Dispose the process object afterwards.
# .OUTPUTS
Expand All @@ -1804,6 +1806,7 @@ function Complete-PkProcess {
param(
[Parameter(Mandatory, Position = 0)]
[hashtable]$Job,
# The dispatcher's default -JobPollCompleted waits the same 15 s: keep the two in step.
[int]$StreamTimeoutMs = 15000,
[switch]$Dispose
)
Expand All @@ -1820,9 +1823,17 @@ function Complete-PkProcess {
$tasks = [System.Collections.Generic.List[System.Threading.Tasks.Task]]::new()
if ($null -ne $Job['StdoutTask']) { $tasks.Add($Job['StdoutTask']) }
if ($null -ne $Job['StderrTask']) { $tasks.Add($Job['StderrTask']) }
# The dispatcher's default poll waits for the output alongside the other
# jobs, timing it with OutputWait: only what's left of the wait here. The
# final pass after an abort's wait sets OutputWaitOver: at most 250 ms
# then, enough for a program that exited just before to have its output
# read to the end, short enough for a window close.
$waitMs = $StreamTimeoutMs
if ($Job['OutputWait'] -is [System.Diagnostics.Stopwatch]) { $waitMs = [int][Math]::Max(0, $StreamTimeoutMs - $Job['OutputWait'].ElapsedMilliseconds) }
if ($Job['OutputWaitOver']) { $waitMs = [Math]::Min($waitMs, 250) }
if ($tasks.Count -gt 0) {
try {
if (-not [System.Threading.Tasks.Task]::WaitAll($tasks.ToArray(), $StreamTimeoutMs)) { $result.StreamTimedOut = $true }
if (-not [System.Threading.Tasks.Task]::WaitAll($tasks.ToArray(), $waitMs)) { $result.StreamTimedOut = $true }
} catch {
$result.StreamError = [string]$_.Exception.Message
}
Expand Down Expand Up @@ -1944,7 +1955,8 @@ function Invoke-PkProcessDispatch {
#
# The dispatcher keeps its own state on each job hashtable. Don't use these keys
# for anything else: GroupKey, SlotReleased, Proc, StdoutTask, StderrTask,
# Stopwatch, TimeLimit, TimedOut, StartFailureReported. It also sets Group to
# Stopwatch, TimeLimit, TimedOut, StartFailureReported, OutputWait,
# OutputWaitOver. It also sets Group to
# the job's group, unless the job already has a Group of its own. The default
# starter reads FilePath, Arguments (a string as written, or a list quoted one
# argument per element), WorkingDirectory and OutputEncoding from the job. A
Expand All @@ -1960,8 +1972,11 @@ function Invoke-PkProcessDispatch {
# { param($job) } that starts the job and returns it with .Proc set. Default:
# Start-PkProcess on the job's FilePath and Arguments.
# .PARAMETER JobPollCompleted
# { param($job) } returning $true once the job is finished. Default:
# $job['Proc'].HasExited. Only its last output value counts.
# { param($job) } returning $true once the job is finished. Only its last
# output value counts. Default: once the program has exited and its output
# has been read, or 15 s after the exit if a program it started holds the
# output open (the job keeps its slot meanwhile), so Complete-PkProcess in
# -JobFinalize doesn't wait and the other jobs carry on.
# .PARAMETER JobFinalize
# { param($job, $pendingQueue) } called once per finished job; collect its
# result with Complete-PkProcess. Enqueue new job hashtables onto $pendingQueue
Expand Down Expand Up @@ -2083,7 +2098,25 @@ function Invoke-PkProcessDispatch {
}
# The default poll reads $pkJob['Proc'], so a started job must have one.
$pkRequireProc = ($null -eq $pkJobPollCompleted)
if ($null -eq $pkJobPollCompleted) { $pkJobPollCompleted = { param($pkJob) return $pkJob['Proc'].HasExited } }
if ($null -eq $pkJobPollCompleted) {
# Done once the program has exited and its output has been read, so
# Complete-PkProcess in -JobFinalize has nothing to wait for: waiting
# there would stall the whole loop. A job whose output a leftover
# program holds open keeps its slot until the reads end or 15 s pass
# (Complete-PkProcess's default -StreamTimeoutMs; keep the two in
# step), timed by OutputWait, which Complete-PkProcess reads. After an
# abort's wait the final pass sets OutputWaitOver: it takes what has
# arrived.
$pkJobPollCompleted = {
param($pkJob)
if (-not $pkJob['Proc'].HasExited) { return $false }
$pkOut = $pkJob['StdoutTask']; $pkErr = $pkJob['StderrTask']
if (($pkOut -isnot [System.Threading.Tasks.Task] -or $pkOut.IsCompleted) -and ($pkErr -isnot [System.Threading.Tasks.Task] -or $pkErr.IsCompleted)) { return $true }
if ($pkJob['OutputWaitOver']) { return $true }
if ($pkJob['OutputWait'] -isnot [System.Diagnostics.Stopwatch]) { $pkJob['OutputWait'] = [System.Diagnostics.Stopwatch]::StartNew() }
return ($pkJob['OutputWait'].ElapsedMilliseconds -ge 15000)
}
}
if ($null -eq $pkJobAbortCleanup) { $pkJobAbortCleanup = { param($pkJob) } }
if ($null -eq $pkGroupCompleteCallback) { $pkGroupCompleteCallback = { param($pkGroup) } }

Expand Down Expand Up @@ -2301,6 +2334,7 @@ function Invoke-PkProcessDispatchCore {
# its old, disposed process: cleared first.
if ($pkJob['SlotReleased']) {
$pkJob['Proc'] = $null; $pkJob['StdoutTask'] = $null; $pkJob['StderrTask'] = $null; $pkJob['Stopwatch'] = $null
$pkJob['OutputWait'] = $null; $pkJob['OutputWaitOver'] = $null
}
$pkJob['SlotReleased'] = $false
$pkStarting = $pkJob
Expand Down Expand Up @@ -2486,6 +2520,11 @@ function Invoke-PkProcessDispatchCore {
# force-stopped. Keep in sync with the two blocks above, minus the time
# limit (whatever is left is force-stopped just below).
if ($pkRunning.Count -gt 0) {
# The wait is over: a job whose program has finished, and that is
# only waiting for its output, takes what has arrived (the default
# poll and Complete-PkProcess read this) rather than being
# force-stopped.
foreach ($pkJob in $pkRunning) { $pkJob['OutputWaitOver'] = $true }
$pkStillWaiting = [System.Collections.Generic.List[hashtable]]::new()
foreach ($pkJob in $pkRunning) {
$pkCbDone = $false
Expand Down
Loading
Loading