From c07fb023a3be7acf5a9a46c795f267906f8d213a Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:16:32 -0500 Subject: [PATCH 1/2] Mark a force-stopped job done before releasing its shared slot The force-stop loop released a shared SemaphoreSlim slot before setting SlotReleased. A stop landing between the two had the teardown release the slot again, leaving the shared budget one above its limit. The finalize passes already mark first; the force-stop loop and the teardown now do too. Co-Authored-By: Claude Opus 5.5 --- ParallelKit.Harness.ps1 | 20 ++++++++++++++++++++ ParallelKit.ps1 | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index b8b075c..b111b11 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -3453,6 +3453,26 @@ 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: in the loop body or catch block around each .Release(), the job is +# marked done first (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) { + $slotUnit = $slotCall.Parent + while ($null -ne $slotUnit -and $slotUnit -isnot [System.Management.Automation.Language.CatchClauseAst] -and $slotUnit -isnot [System.Management.Automation.Language.LoopStatementAst]) { $slotUnit = $slotUnit.Parent } + if ($null -eq $slotUnit) { continue } + $slotMarks = @($slotUnit.FindAll({ param($a) $a -is [System.Management.Automation.Language.AssignmentStatementAst] -and ($a.Extent.Text -eq '$pkJob[''SlotReleased''] = $true' -or $a.Extent.Text -eq '$pkStarting = $null') }, $true)) + if (@($slotMarks | Where-Object { $_.Extent.EndOffset -lt $slotCall.Extent.StartOffset }).Count -gt 0) { $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..ded71ee 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 } } @@ -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 } } From b9cf46773e5d3f9c0088ee4363f42d60e427ba11 Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:22:49 -0500 Subject: [PATCH 2/2] Tie the slot-order check to each release; correct the teardown comment The harness check now requires the statement right before each shared-budget release to be the job's mark, not any mark earlier in the loop. The teardown comment no longer says a marked job's slot is always released already. Co-Authored-By: Claude Opus 5.5 --- ParallelKit.Harness.ps1 | 17 ++++++++++------- ParallelKit.ps1 | 6 +++--- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index b111b11..dc6194a 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -3458,17 +3458,20 @@ Write-Host "=== AL: a job is marked done before its shared slot is released ===" # 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: in the loop body or catch block around each .Release(), the job is -# marked done first (SlotReleased set, or $pkStarting cleared on a failed start). +# 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) { - $slotUnit = $slotCall.Parent - while ($null -ne $slotUnit -and $slotUnit -isnot [System.Management.Automation.Language.CatchClauseAst] -and $slotUnit -isnot [System.Management.Automation.Language.LoopStatementAst]) { $slotUnit = $slotUnit.Parent } - if ($null -eq $slotUnit) { continue } - $slotMarks = @($slotUnit.FindAll({ param($a) $a -is [System.Management.Automation.Language.AssignmentStatementAst] -and ($a.Extent.Text -eq '$pkJob[''SlotReleased''] = $true' -or $a.Extent.Text -eq '$pkStarting = $null') }, $true)) - if (@($slotMarks | Where-Object { $_.Extent.EndOffset -lt $slotCall.Extent.StartOffset }).Count -gt 0) { $slotMarkedFirst++ } + $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 "" diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index ded71ee..0536400 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -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) }