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
26 changes: 26 additions & 0 deletions ParallelKit.Harness.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3049,6 +3049,32 @@ 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
$dispLive.Proc.Dispose()

# A -JobFinalize that throws: the job it was finalizing had finished, so the
# teardown must not call -JobAbortCleanup on it (that deletes a killed job's
# partial output), only on the job it kills. Both slots still come back.
Reset-Harness
$fcJobs = @(@{ Name = 'run'; FilePath = $cmdExe; Arguments = ('/c ""' + $pingExe + '" -n 55 127.0.0.1"') }, @{ Name = 'done'; FilePath = $cmdExe; Arguments = '/c exit 0' })
$fcFeed = @{ Given = $false }
$fcCleaned = [System.Collections.Generic.List[string]]::new()
$fcBudget = [System.Threading.SemaphoreSlim]::new(2, 2)
$fcWait = [System.Diagnostics.Stopwatch]::StartNew()
$fcProblem = $null
try {
$null = Invoke-PkProcessDispatch -MaxConcurrent $fcBudget `
-GroupProvider { if ($fcFeed.Given) { return $null }; $fcFeed.Given = $true; return 'g' }.GetNewClosure() `
-JobBuilder { param($g) return $fcJobs }.GetNewClosure() `
-JobPollCompleted {
param($job)
if ($job['Name'] -ne 'done') { return $false }
return ($job['Proc'].HasExited -and ((Get-LimitPingCount 55) -ge 1 -or $fcWait.Elapsed.TotalSeconds -gt 15))
}.GetNewClosure() `
-JobFinalize { param($job, $q) $null = Complete-PkProcess -Job $job; throw "finalize failed" } `
-JobAbortCleanup { param($job) $fcCleaned.Add($job['Name']) }.GetNewClosure()
} catch { $fcProblem = [string]$_ }
$fcLeft = Get-LimitPingCount 55
Stop-StrayPings 55
Test-Assert ($fcProblem -like '*finalize failed*' -and ($fcCleaned -join ',') -eq 'run' -and $fcLeft -eq 0 -and $fcBudget.CurrentCount -eq 2) ("a -JobFinalize that throws gets no -JobAbortCleanup for its finished job, only for the killed one (cleaned: " + ($fcCleaned -join ',') + ", " + $fcLeft + " left, " + $fcBudget.CurrentCount + " of 2 slots free)")
Reset-Harness
Write-Host ""

Expand Down
14 changes: 13 additions & 1 deletion ParallelKit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,10 @@ function Invoke-PkProcessDispatchCore {
# The job -JobStarter is working on: not in $pkRunning yet, but it may
# already have a program and a shared slot.
$pkStarting = $null
# The job -JobFinalize is working on. It has finished, so if -JobFinalize
# throws, the teardown skips -JobAbortCleanup for it (that's for jobs it
# kills) and only gives back its slot.
$pkFinalizing = $null
# Invoke-PkCallback's -PkContext: callbacks that used continue/break are
# warned about once each, under this prefix.
$pkCallbackContext = @{ Prefix = 'ProcessDispatch: '; Warned = @{} }
Expand Down Expand Up @@ -1717,7 +1721,9 @@ function Invoke-PkProcessDispatchCore {
if ($pkPolled) {
$pkPendingBefore = $pkPending.Count
$pkCbDone = $false
$pkFinalizing = $pkJob
$null = do { & $pkJobFinalize $pkJob $pkPending; $pkCbDone = $true } while ($false)
$pkFinalizing = $null
if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-JobFinalize' -PkContext $pkCallbackContext }
# Marked done first: a stop landing after the Release or the
# Dispose would otherwise have the teardown release the slot a
Expand Down Expand Up @@ -1781,7 +1787,9 @@ function Invoke-PkProcessDispatchCore {
if ($pkPolled) {
$pkPendingBefore = $pkPending.Count
$pkCbDone = $false
$pkFinalizing = $pkJob
$null = do { & $pkJobFinalize $pkJob $pkPending; $pkCbDone = $true } while ($false)
$pkFinalizing = $null
if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-JobFinalize' -PkContext $pkCallbackContext }
$pkJob['SlotReleased'] = $true # first: see the main poll pass
if ($pkIsSharedBudget) { $null = $pkMaxConcurrent.Release() }
Expand Down Expand Up @@ -1836,7 +1844,9 @@ function Invoke-PkProcessDispatchCore {
if ($pkPolled) {
$pkPendingBefore = $pkPending.Count
$pkCbDone = $false
$pkFinalizing = $pkJob
$null = do { & $pkJobFinalize $pkJob $pkPending; $pkCbDone = $true } while ($false)
$pkFinalizing = $null
if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-JobFinalize' -PkContext $pkCallbackContext }
$pkJob['SlotReleased'] = $true # first: see the main poll pass
if ($pkIsSharedBudget) { $null = $pkMaxConcurrent.Release() }
Expand Down Expand Up @@ -1904,7 +1914,9 @@ function Invoke-PkProcessDispatchCore {
Stop-PkJobProcesses -Jobs $pkTearDown
foreach ($pkJob in $pkTearDown) {
try { if ($pkJob['Proc']) { $null = $pkJob['Proc'].WaitForExit(5000) } } catch { }
try { $null = Invoke-PkCallback -PkCallback $pkJobAbortCleanup -PkArgs @(,$pkJob) -PkName '-JobAbortCleanup' -PkContext $pkCallbackContext } catch { }
if (-not [object]::ReferenceEquals($pkJob, $pkFinalizing)) {
try { $null = Invoke-PkCallback -PkCallback $pkJobAbortCleanup -PkArgs @(,$pkJob) -PkName '-JobAbortCleanup' -PkContext $pkCallbackContext } catch { }
}
if ($pkIsSharedBudget) { try { $null = $pkMaxConcurrent.Release() } catch { } }
$pkJob['SlotReleased'] = $true
try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { }
Expand Down
Loading