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
14 changes: 14 additions & 0 deletions ParallelKit.Harness.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3075,6 +3075,20 @@ try {
$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)")

# A -JobPollCompleted that leaks a value (ArrayList.Add returns the index)
# before its answer: only the last value counts, so the job isn't finalized
# while its program still runs.
Reset-Harness
$leakSeen = [System.Collections.ArrayList]::new()
$leakFin = @{ Count = 0; Exited = $null }
$leakFeed = @{ Given = $false }
$null = Invoke-PkProcessDispatch -MaxConcurrent 1 `
-GroupProvider { if ($leakFeed.Given) { return $null }; $leakFeed.Given = $true; return 'g' }.GetNewClosure() `
-JobBuilder { param($g) return @(@{ FilePath = $pingExe; Arguments = '-n 2 127.0.0.1' }) }.GetNewClosure() `
-JobPollCompleted { param($job) $leakSeen.Add(1); $job['Proc'].HasExited }.GetNewClosure() `
-JobFinalize { param($job, $q) $leakFin.Count++; $leakFin.Exited = $job['Proc'].HasExited; $null = Complete-PkProcess -Job $job }.GetNewClosure()
Test-Assert ($leakFin.Count -eq 1 -and $leakFin.Exited -eq $true -and $leakSeen.Count -gt 1) ("a -JobPollCompleted that leaks a value finalizes the job only once it has exited (finalized " + $leakFin.Count + "x, HasExited then: " + $leakFin.Exited + ", polled " + $leakSeen.Count + "x)")
Reset-Harness
Write-Host ""

Expand Down
7 changes: 6 additions & 1 deletion ParallelKit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ function Invoke-PkProcessDispatch {
# Start-PkProcess on the job's FilePath and Arguments.
# .PARAMETER JobPollCompleted
# { param($job) } returning $true once the job is finished. Default:
# $job['Proc'].HasExited.
# $job['Proc'].HasExited. Only its last output value counts.
# .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 @@ -1718,6 +1718,9 @@ function Invoke-PkProcessDispatchCore {
$pkCbDone = $false
$pkPolled = do { & $pkJobPollCompleted $pkJob; $pkCbDone = $true } while ($false)
if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-JobPollCompleted' -PkContext $pkCallbackContext }
# Only the poll's last value counts: a value it leaks first (an
# ArrayList.Add index, say) would make a running job look done.
if ($pkPolled -is [object[]]) { $pkPolled = $pkPolled[-1] }
if ($pkPolled) {
$pkPendingBefore = $pkPending.Count
$pkCbDone = $false
Expand Down Expand Up @@ -1784,6 +1787,7 @@ function Invoke-PkProcessDispatchCore {
$pkCbDone = $false
$pkPolled = do { & $pkJobPollCompleted $pkJob; $pkCbDone = $true } while ($false)
if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-JobPollCompleted' -PkContext $pkCallbackContext }
if ($pkPolled -is [object[]]) { $pkPolled = $pkPolled[-1] } # only the last value counts: see the main poll pass
if ($pkPolled) {
$pkPendingBefore = $pkPending.Count
$pkCbDone = $false
Expand Down Expand Up @@ -1841,6 +1845,7 @@ function Invoke-PkProcessDispatchCore {
$pkCbDone = $false
$pkPolled = do { & $pkJobPollCompleted $pkJob; $pkCbDone = $true } while ($false)
if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-JobPollCompleted' -PkContext $pkCallbackContext }
if ($pkPolled -is [object[]]) { $pkPolled = $pkPolled[-1] } # only the last value counts: see the main poll pass
if ($pkPolled) {
$pkPendingBefore = $pkPending.Count
$pkCbDone = $false
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ All of these callbacks run on whichever thread called the dispatcher: the main t
- Whatever your callbacks output is thrown away, apart from the return values listed here. They can't corrupt the dispatcher's result by accident.
- `-MaxConcurrent` can be a `SemaphoreSlim` shared by several dispatchers that must stay under one combined limit. It works inside workers, so two runspace jobs can each run a dispatcher against one shared budget.
- `-GroupReleaseCheck { param($group) }` makes a group with zero jobs keep its queue-ahead slot until the check returns true. Use it to stop one dispatcher racing ahead of another that it's paired with. The dispatcher doesn't return until every held group has been released, unless the run is canceled or aborted. It runs on every loop pass, so keep it cheap.
- `-JobPollCompleted { param($job) }` replaces the default "has the process exited" check. Use it for a custom starter whose job isn't a plain process.
- `-JobPollCompleted { param($job) }` replaces the default "has the process exited" check. Use it for a custom starter whose job isn't a plain process. Only its last output value counts, so a stray value written before the answer doesn't mark the job done.
- `-PollCallback { param($runningJobs) }` runs on every loop pass, with the jobs currently running (`-AbortPollCallback`, its name before 3.0, also works). That's about every 50 ms, and can be many times more often when programs finish quickly, so keep it cheap.
- `-CancelToken` stops the dispatcher when canceled. An abort stops it either way, without canceling your token.
- An exception from one of your callbacks stops the dispatcher: running processes are killed and the exception is rethrown.
Expand Down
Loading