From d70e336b7689b02b6e172bdedfc1a617acd3d507 Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:58:54 -0500 Subject: [PATCH] Leave a disposed process out of a tree kill, and mark a finished job before disposing it Co-Authored-By: Claude Opus 5.5 --- ParallelKit.Harness.ps1 | 43 +++++++++++++++++++++++++++++++++++++++++ ParallelKit.ps1 | 35 ++++++++++++++++++++------------- 2 files changed, 65 insertions(+), 13 deletions(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index 459c193..233e53d 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -3006,6 +3006,49 @@ try { $stopLeft = Get-LimitPingCount 47 Stop-StrayPings 47 Test-Assert ($stopProblem -like '*finalize failed*' -and $stopLeft -eq 0) ("an error after a job was finalized in the same pass still kills the running ones (" + $stopLeft + " left, problem: " + $stopProblem + ")") + +# A -JobFinalize that disposes the job's process and then throws: the +# teardown's kill list then holds a disposed Process, whose HasExited and Id +# read as $null. It mustn't spoil the kill for the jobs still running. +Reset-Harness +$dispJobs = @(1..3 | ForEach-Object { @{ Name = ('p' + $_); FilePath = $cmdExe; Arguments = ('/c ""' + $pingExe + '" -n 51 127.0.0.1"') } }) +$dispJobs += @{ Name = 'a'; FilePath = $cmdExe; Arguments = '/c exit 0' } +$dispFeed = @{ Given = $false } +$dispWaitStart = [System.Diagnostics.Stopwatch]::StartNew() +$dispProblem = $null +try { + $null = Invoke-PkProcessDispatch -MaxConcurrent 4 ` + -GroupProvider { if ($dispFeed.Given) { return $null }; $dispFeed.Given = $true; return 'g' }.GetNewClosure() ` + -JobBuilder { param($g) return $dispJobs }.GetNewClosure() ` + -JobPollCompleted { + param($job) + # 'a' reports done once the three pings run (bounded, so a ping + # that never starts fails the test instead of hanging it). + if ($job['Name'] -ne 'a') { return $false } + return ($job['Proc'].HasExited -and ((Get-LimitPingCount 51) -ge 3 -or $dispWaitStart.Elapsed.TotalSeconds -gt 15)) + }.GetNewClosure() ` + -JobFinalize { param($job, $q) $null = Complete-PkProcess -Job $job -Dispose; throw "finalize failed after dispose" } +} catch { $dispProblem = [string]$_ } +$dispLeft = Get-LimitPingCount 51 +Stop-StrayPings 51 +Test-Assert ($dispProblem -like '*finalize failed after dispose*' -and $dispLeft -eq 0) ("a -JobFinalize that disposes its process and throws still has the running jobs killed (" + $dispLeft + " of 3 left, problem: " + $dispProblem + ")") + +# The same directly: a disposed process in the list doesn't stop the live +# tree beside it being killed. +$dispGone = Start-PkProcess -FilePath $cmdExe -Arguments '/c exit 0' +$null = Wait-PkProcess -Process $dispGone.Proc -TimeoutMs 5000 +$dispGone.Proc.Dispose() +$dispLive = Start-PkProcess -FilePath $cmdExe -Arguments ('/c ""' + $pingExe + '" -n 53 127.0.0.1"') +$dispWait = [System.Diagnostics.Stopwatch]::StartNew() +while ((Get-LimitPingCount 53) -lt 1 -and $dispWait.Elapsed.TotalSeconds -lt 5) { Start-Sleep -Milliseconds 100 } +$dispThrew = $null +$dispKilled = $false +try { $dispKilled = Stop-PkProcessTree -Process @($dispGone.Proc, $dispLive.Proc) } catch { $dispThrew = [string]$_ } +$dispLeft = Get-LimitPingCount 53 +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() Reset-Harness Write-Host "" diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index 01d6848..eb3165a 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -1033,7 +1033,9 @@ function Stop-PkProcessTree { [System.Diagnostics.Process[]]$Process ) $alive = [System.Collections.Generic.List[System.Diagnostics.Process]]::new() - foreach ($p in $Process) { if ($null -ne $p -and -not $p.HasExited) { $alive.Add($p) } } + # A disposed Process reads $null for HasExited and Id (no throw), and an + # empty /PID fails taskkill for the whole list, so it's left out. + foreach ($p in $Process) { if ($null -ne $p -and $p.HasExited -eq $false -and $null -ne $p.Id) { $alive.Add($p) } } if ($alive.Count -eq 0) { return $false } $killed = $false $pidArgs = foreach ($p in $alive) { '/PID ' + $p.Id } @@ -1069,7 +1071,11 @@ function Stop-PkProcessTree { try { $p.Kill(); $killed = $true } catch { } # it exited between the check and the kill } } - if ($killed) { foreach ($p in $alive) { $null = $p.WaitForExit(5000) } } + if ($killed) { + foreach ($p in $alive) { + try { $null = $p.WaitForExit(5000) } catch { } # disposed by its owner meanwhile; one failed wait mustn't skip the others + } + } return $killed } @@ -1713,9 +1719,12 @@ function Invoke-PkProcessDispatchCore { $pkCbDone = $false $null = do { & $pkJobFinalize $pkJob $pkPending; $pkCbDone = $true } while ($false) if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-JobFinalize' -PkContext $pkCallbackContext } - try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } # a failed Dispose can't affect the result already taken - if ($pkIsSharedBudget) { $null = $pkMaxConcurrent.Release() } + # Marked done first: a stop landing after the Release or the + # Dispose would otherwise have the teardown release the slot a + # second time and treat the disposed job as still running. $pkJob['SlotReleased'] = $true + if ($pkIsSharedBudget) { $null = $pkMaxConcurrent.Release() } + try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } # a failed Dispose can't affect the result already taken # Whatever JobFinalize queued (a retry, follow-ups) joins its group # here, read off the queue rather than a returned flag. Only when the # queue grew: walking it for every job is slow on a large batch. @@ -1774,9 +1783,9 @@ function Invoke-PkProcessDispatchCore { $pkCbDone = $false $null = do { & $pkJobFinalize $pkJob $pkPending; $pkCbDone = $true } while ($false) if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-JobFinalize' -PkContext $pkCallbackContext } - try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } + $pkJob['SlotReleased'] = $true # first: see the main poll pass if ($pkIsSharedBudget) { $null = $pkMaxConcurrent.Release() } - $pkJob['SlotReleased'] = $true + try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } # a failed Dispose can't affect the result already taken # Whatever JobFinalize queued (a retry, follow-ups) joins its group # here, read off the queue rather than a returned flag. Only when the # queue grew: walking it for every job is slow on a large batch. @@ -1829,9 +1838,9 @@ function Invoke-PkProcessDispatchCore { $pkCbDone = $false $null = do { & $pkJobFinalize $pkJob $pkPending; $pkCbDone = $true } while ($false) if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-JobFinalize' -PkContext $pkCallbackContext } - try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } + $pkJob['SlotReleased'] = $true # first: see the main poll pass if ($pkIsSharedBudget) { $null = $pkMaxConcurrent.Release() } - $pkJob['SlotReleased'] = $true + try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } # a failed Dispose can't affect the result already taken # Whatever JobFinalize queued (a retry, follow-ups) joins its group # here, read off the queue rather than a returned flag. Only when the # queue grew: walking it for every job is slow on a large batch. @@ -1875,9 +1884,9 @@ function Invoke-PkProcessDispatchCore { foreach ($pkJob in $pkRunning) { try { if ($pkJob['Proc']) { $null = $pkJob['Proc'].WaitForExit(5000) } } catch { } try { $null = Invoke-PkCallback -PkCallback $pkJobAbortCleanup -PkArgs @(,$pkJob) -PkName '-JobAbortCleanup' -PkContext $pkCallbackContext } catch { } - try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } if ($pkIsSharedBudget) { try { $null = $pkMaxConcurrent.Release() } catch { } } $pkJob['SlotReleased'] = $true + try { if ($pkJob['Proc']) { $pkJob['Proc'].Dispose() } } catch { } } } } @@ -1885,10 +1894,10 @@ function Invoke-PkProcessDispatchCore { } finally { 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 and - # disposed, and a disposed Process makes Stop-PkProcessTree throw - # part-way. Same per-job steps as the force-stop above; keep the - # two in step. + # 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. $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) }