diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index b8b075c..dc6194a 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -3453,6 +3453,29 @@ Test-Assert ((Get-Command Invoke-PkProcessDispatch).Parameters['PollCallback'].A Reset-Harness Write-Host "" +# ============================================================================== +Write-Host "=== AL: a job is marked done before its shared slot is released ===" -ForegroundColor Cyan +# Released first, a stop landing before the mark has the teardown release the +# slot again, leaving a shared budget one above its limit. The window is one +# statement wide, too narrow to hit from a test, so this reads the dispatcher's +# code: the statement right before each `if ($pkIsSharedBudget) { ...Release() }` +# marks the job done (SlotReleased set, or $pkStarting cleared on a failed start). +$slotCoreAst = & (Get-Module ParallelKit) { (Get-Command Invoke-PkProcessDispatchCore).ScriptBlock.Ast } +$slotReleases = @($slotCoreAst.FindAll({ param($a) $a -is [System.Management.Automation.Language.InvokeMemberExpressionAst] -and $a.Member.Extent.Text -eq 'Release' -and $a.Expression.Extent.Text -eq '$pkMaxConcurrent' }, $true)) +$slotMarkedFirst = 0 +foreach ($slotCall in $slotReleases) { + $slotIf = $slotCall.Parent + while ($null -ne $slotIf -and -not ($slotIf -is [System.Management.Automation.Language.IfStatementAst] -and $slotIf.Clauses[0].Item1.Extent.Text -eq '$pkIsSharedBudget')) { $slotIf = $slotIf.Parent } + if ($null -eq $slotIf -or $slotIf.Parent -isnot [System.Management.Automation.Language.StatementBlockAst]) { continue } + $slotSiblings = $slotIf.Parent.Statements + $slotIndex = $slotSiblings.IndexOf($slotIf) + if ($slotIndex -lt 1) { continue } + $slotBefore = $slotSiblings[$slotIndex - 1].Extent.Text + if ($slotBefore -eq '$pkJob[''SlotReleased''] = $true' -or $slotBefore -eq '$pkStarting = $null') { $slotMarkedFirst++ } +} +Test-Assert ($slotReleases.Count -gt 0 -and $slotMarkedFirst -eq $slotReleases.Count) ("every shared-slot release comes after its job is marked done (" + $slotMarkedFirst + " of " + $slotReleases.Count + ")") +Write-Host "" + Set-PkMessageHandler -Handler $null Reset-PkAbortState diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index 3516b97..0536400 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -1904,8 +1904,8 @@ function Invoke-PkProcessDispatchCore { foreach ($pkJob in $pkRunning) { try { if ($pkJob['Proc']) { $null = $pkJob['Proc'].WaitForExit(5000) } } catch { } # a custom starter's .Proc may not be a Process; the kill above already ran try { $null = Invoke-PkCallback -PkCallback $pkJobAbortCleanup -PkArgs @(,$pkJob) -PkName '-JobAbortCleanup' -PkContext $pkCallbackContext } catch { } # one job's failed cleanup mustn't skip the others' + $pkJob['SlotReleased'] = $true # first: see the main poll pass if ($pkIsSharedBudget) { try { $null = $pkMaxConcurrent.Release() } catch { } } # best effort: one failed release mustn't stop the other jobs' cleanup - $pkJob['SlotReleased'] = $true try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } # the job is over either way } } @@ -1915,9 +1915,9 @@ function Invoke-PkProcessDispatchCore { if (-not $pkLoopDone) { # Best-effort teardown after an error or a stop (an error carries # on up by itself). Jobs with SlotReleased set are already done: - # their slot is released and their process disposed or about to - # be. Same per-job steps as the force-stop above; keep the two in - # step. + # their slot is released and their process disposed, or both are + # about to be. Same per-job steps as the force-stop above; keep the + # two in step. $pkTearDown = [System.Collections.Generic.List[hashtable]]::new() foreach ($pkJob in $pkRunning) { if (-not $pkJob['SlotReleased']) { $pkTearDown.Add($pkJob) } } if ($null -ne $pkStarting -and -not $pkTearDown.Contains($pkStarting)) { $pkTearDown.Add($pkStarting) } @@ -1927,8 +1927,8 @@ function Invoke-PkProcessDispatchCore { if (-not [object]::ReferenceEquals($pkJob, $pkFinalizing)) { try { $null = Invoke-PkCallback -PkCallback $pkJobAbortCleanup -PkArgs @(,$pkJob) -PkName '-JobAbortCleanup' -PkContext $pkCallbackContext } catch { } # one job's failed cleanup mustn't skip the others' } + $pkJob['SlotReleased'] = $true # first, as in the force-stop above if ($pkIsSharedBudget) { try { $null = $pkMaxConcurrent.Release() } catch { } } # best effort: one failed release mustn't stop the other jobs' cleanup - $pkJob['SlotReleased'] = $true try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } # the job is over either way } }