From fab447b008b64e61c54995a2ce0d9c5fa4acb15e Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:15:23 -0500 Subject: [PATCH 1/3] Leave the thread pool's minimum alone Co-Authored-By: Claude Opus 5.5 --- ParallelKit.Harness.ps1 | 28 +++++++++++++++++----------- ParallelKit.ps1 | 40 +++++++++++----------------------------- README.md | 4 ++-- 3 files changed, 30 insertions(+), 42 deletions(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index eaed3ef..13dfa32 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -9,6 +9,9 @@ $ErrorActionPreference = "Stop" # The whole suite runs under strict mode: a host project may well use it, # and the kit must not depend on lenient hashtable/variable reads. Set-StrictMode -Version Latest +# The thread pool's minimum before the kit loads: the kit leaves it alone. +$poolMinBefore = 0; $poolIoBefore = 0 +[System.Threading.ThreadPool]::GetMinThreads([ref]$poolMinBefore, [ref]$poolIoBefore) . (Join-Path $PSScriptRoot "ParallelKit.ps1") $script:passCount = 0 @@ -75,10 +78,10 @@ Write-Host "=== A: setup ===" -ForegroundColor Cyan $init = Initialize-ParallelKit -MaxConcurrentProcesses 8 Test-Assert ($init.AbortHandler -eq $true) "abort handler registers" Test-Assert ($init.ChildProcessJob -eq $true) "child-process job object initializes" -Test-Assert ($init.ThreadPool -eq $true) "thread pool minimum is raised" +Test-Assert ($init.ThreadPool -eq $true) "ThreadPool is still `$true, for scripts that check it" $w = 0; $io = 0 [System.Threading.ThreadPool]::GetMinThreads([ref]$w, [ref]$io) -Test-Assert ($w -ge 128) "thread pool minimum is at least 128 worker threads" +Test-Assert ($w -eq $poolMinBefore -and $io -eq $poolIoBefore) ("Initialize-ParallelKit leaves the thread pool's minimum alone (" + $w + "/" + $io + ", was " + $poolMinBefore + "/" + $poolIoBefore + ")") Write-Host "" # ============================================================================== @@ -854,24 +857,27 @@ $writer.Dispose() Test-Assert ($null -eq $scanError) ("scanning a collection other threads are changing doesn't throw (" + $scanError + ")") # With no Initialize-ParallelKit at all, starting a process sets up the -# thread pool and the kill-on-close job object. Needs a fresh process. +# kill-on-close job object, and leaves the thread pool alone. Needs a fresh +# process. $probeScript = Join-Path $env:TEMP ("pk-probe-" + [guid]::NewGuid().ToString("N") + ".ps1") $kitPath = Join-Path $PSScriptRoot "ParallelKit.ps1" [System.IO.File]::WriteAllText($probeScript, @" +`$w0 = 0; `$io0 = 0 +[System.Threading.ThreadPool]::GetMinThreads([ref]`$w0, [ref]`$io0) . '$kitPath' `$before = [PkChildProcessJob]::IsInitialized `$j = Start-PkProcess -FilePath '$cmdExe' -Arguments '/c exit 0' `$null = Wait-PkProcess -Process `$j.Proc -TimeoutMs 5000 `$w = 0; `$io = 0 [System.Threading.ThreadPool]::GetMinThreads([ref]`$w, [ref]`$io) -Write-Output ("before=" + `$before + " after=" + [PkChildProcessJob]::IsInitialized + " threads=" + `$w) +Write-Output ("before=" + `$before + " after=" + [PkChildProcessJob]::IsInitialized + " threads=" + `$w + " was=" + `$w0) "@) $probe = Invoke-PkProcessWithTimeout -FilePath $selfExe -Arguments ('-NoProfile -ExecutionPolicy Bypass -File "' + $probeScript + '"') -TimeoutMs 60000 [System.IO.File]::Delete($probeScript) Test-Assert ($probe.Stdout -match 'before=False after=True') ("starting a process sets up child cleanup by itself (" + ([string]$probe.Stdout).Trim() + ")") -$probeThreads = 0 -if ($probe.Stdout -match 'threads=(\d+)') { $probeThreads = [int]$Matches[1] } -Test-Assert ($probeThreads -ge 128) ("and raises the thread pool by itself (minimum " + $probeThreads + ")") +$probeThreads = -1; $probeWas = -2 +if ($probe.Stdout -match 'threads=(\d+) was=(\d+)') { $probeThreads = [int]$Matches[1]; $probeWas = [int]$Matches[2] } +Test-Assert ($probeThreads -eq $probeWas) ("and leaves the thread pool's minimum alone (" + $probeThreads + ", was " + $probeWas + ")") # A script run the way a prompt runs it (& script, or .\script.ps1) holds # the kit's functions in its own scope, where a .GetNewClosure() block can't @@ -914,12 +920,12 @@ $clm = Invoke-PkProcessWithTimeout -FilePath $selfExe -Arguments ('-NoProfile -E [System.IO.File]::Delete($clmScript) Test-Assert ($clm.Stdout -like '*ConstrainedLanguage mode, but the kit needs FullLanguage*') ("loading in Constrained Language Mode fails with a message naming the problem (" + ([string]$clm.Stdout).Trim() + ")") -# The dispatcher sizes the pool for its own concurrency. +# The dispatcher leaves the pool alone, whatever its concurrency. $feed = @{ Given = $false } $null = Invoke-PkProcessDispatch -MaxConcurrent 100 -GroupProvider { return $null } -JobBuilder { param($g) } -JobFinalize { param($j, $p) } $w = 0; $io = 0 [System.Threading.ThreadPool]::GetMinThreads([ref]$w, [ref]$io) -Test-Assert ($w -ge 232) ("the dispatcher raises the thread pool to fit -MaxConcurrent (minimum " + $w + " for 100 processes)") +Test-Assert ($w -eq $poolMinBefore) ("the dispatcher leaves the thread pool's minimum alone at -MaxConcurrent 100 (" + $w + ", was " + $poolMinBefore + ")") # A one-line starter keeps the caller's job fields. Reset-Harness @@ -1102,12 +1108,12 @@ $threw = $false try { $null = Invoke-PkRunspaceJobs -MaxParallel 1 -JobParams @(@{ Id = 1 }, $null) -WorkerScript { param($Id) } } catch { $threw = ($_.ToString() -like '*entry 1 is not a hashtable*') } Test-Assert $threw "-JobParams with a `$null entry fails the same way" -# A shared budget sizes the thread pool too. +# So does a shared budget. $bigBudget = [System.Threading.SemaphoreSlim]::new(150, 150) $null = Invoke-PkProcessDispatch -MaxConcurrent $bigBudget -GroupProvider { return $null } -JobBuilder { param($g) } -JobFinalize { param($j, $p) } $w = 0; $io = 0 [System.Threading.ThreadPool]::GetMinThreads([ref]$w, [ref]$io) -Test-Assert ($w -ge 332) ("a shared budget of 150 raises the thread pool to fit (minimum " + $w + ")") +Test-Assert ($w -eq $poolMinBefore) ("and with a shared budget of 150 (" + $w + ", was " + $poolMinBefore + ")") $bigBudget.Dispose() Write-Host "" diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index 7464803..1f925e2 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -966,32 +966,19 @@ function Initialize-PkChildProcessJob { return $false } -function Initialize-PkThreadPool { - # Raises the pool's minimum, since the pool grows slowly past it. This was - # for the output reads, two per program; those now run on threads of their - # own (PkStreamReadTask), so the kit's own work doesn't depend on it. - param([int]$MaxConcurrentProcesses = 0) - $clampedProcs = [Math]::Min([Math]::Max($MaxConcurrentProcesses, 0), 16000) - $minimumWorkerThreads = [Math]::Max(128, (2 * $clampedProcs) + 32) - $worker = 0; $io = 0 - [System.Threading.ThreadPool]::GetMinThreads([ref]$worker, [ref]$io) - [PkState]::ThreadPoolRaised = $true - if ($worker -ge $minimumWorkerThreads) { return $true } - return [System.Threading.ThreadPool]::SetMinThreads($minimumWorkerThreads, $io) -} - function Initialize-ParallelKit { # .SYNOPSIS - # Sets up Ctrl+C handling, child-process cleanup and the thread pool up front. Optional. + # Sets up Ctrl+C handling and child-process cleanup up front. Optional. # .DESCRIPTION # The engines set up what they need on first use anyway. Safe to call more than # once. A failed Ctrl+C handler or child-process cleanup also prints a warning. # .PARAMETER MaxConcurrentProcesses - # How many programs you expect to run at once, used to size the thread pool. - # 0 (the default) sizes it for up to about 48. + # Not used since 3.4, which leaves the thread pool's settings alone; kept so + # existing calls still work. # .OUTPUTS - # An object with AbortHandler, ChildProcessJob and ThreadPool, each $true if that - # part worked; Version, the kit's version; and StartMode, how the kit starts + # An object with AbortHandler and ChildProcessJob, each $true if that part + # worked; ThreadPool, always $true since 3.4 (kept for scripts that check + # it); Version, the kit's version; and StartMode, how the kit starts # programs here: CreatedInJob (inside the Job Object, so anything they start # is in it too), AddedToJob (started, then added to it just after), or NoJob # (no cleanup if PowerShell is killed). A program whose start can't be set up @@ -1000,7 +987,10 @@ function Initialize-ParallelKit { param([int]$MaxConcurrentProcesses = 0) $handlerOk = Register-PkAbortHandler $jobOk = Initialize-PkChildProcessJob - $poolOk = Initialize-PkThreadPool -MaxConcurrentProcesses $MaxConcurrentProcesses + # The kit used to raise the thread pool's minimum here, for output reads on + # pool threads. Those have threads of their own now (PkStreamReadTask), and + # the minimum is a setting the caller's own code shares. + $poolOk = $true # The header's Version line: the version PR changes both, and the harness # checks they match. $version = [version]'3.3' @@ -1454,7 +1444,6 @@ function Start-PkProcessCore { $commandLine = Resolve-PkArguments -Arguments $Arguments -FilePath $FilePath # For a caller who never ran Initialize-ParallelKit. No-ops once done. if (-not [PkChildProcessJob]::IsInitialized) { $null = [PkChildProcessJob]::Initialize() } - if (-not [PkState]::ThreadPoolRaised) { $null = Initialize-PkThreadPool } $psi = [System.Diagnostics.ProcessStartInfo]::new() $psi.FileName = $FilePath @@ -2103,13 +2092,6 @@ function Invoke-PkProcessDispatch { throw ("Invoke-PkProcessDispatch: -StreamTimeoutMs must be 0 or more (got " + $pkStreamTimeoutMs + "). It's how long to wait for a job's output after its program exits, in milliseconds; 0 doesn't wait.") } - # Size the thread pool up front. For a shared budget, its free slots now - # are the best measure of its size. - $pkPoolSizeHint = 0 - if ($pkMaxConcurrent -is [int]) { $pkPoolSizeHint = $pkMaxConcurrent } - elseif ($pkMaxConcurrent -is [System.Threading.SemaphoreSlim]) { $pkPoolSizeHint = $pkMaxConcurrent.CurrentCount } - $null = Initialize-PkThreadPool -MaxConcurrentProcesses $pkPoolSizeHint - if ($null -eq $pkJobStarter) { $pkCallerState = $PSCmdlet.SessionState $pkJobStarter = { @@ -2912,7 +2894,7 @@ function Invoke-PkProcessBatch { function Get-PkWorkerSafeFunctionNames { # Kit functions every worker runspace gets automatically. - return @('Write-PkMessage', 'Test-PkAbortSignal', 'Test-PkCancelRequested', 'Initialize-PkThreadPool', 'Stop-PkProcessTree', 'Stop-PkProcessTreeCore', 'Split-PkTaskkillArguments', 'Stop-PkJobProcesses', 'Get-PkProcessDuration', 'Resolve-PkEncoding', + return @('Write-PkMessage', 'Test-PkAbortSignal', 'Test-PkCancelRequested', 'Stop-PkProcessTree', 'Stop-PkProcessTreeCore', 'Split-PkTaskkillArguments', 'Stop-PkJobProcesses', 'Get-PkProcessDuration', 'Resolve-PkEncoding', 'Start-PkProcess', 'Wait-PkProcess', 'Wait-PkRunningJobs', 'Complete-PkProcess', 'Invoke-PkProcessWithTimeout', 'Invoke-PkProcessDispatch', 'Invoke-PkProcessDispatchCore', 'Invoke-PkProcessBatch', 'Register-PkCancellationSource', 'Unregister-PkCancellationSource', 'New-PkBatchResult', 'ConvertTo-PkArgumentString', 'Get-PkForceStopReason', 'Resolve-PkFileSystemPath', 'Add-PkDispatchGroupJobs', 'Start-PkProcessCore', 'Get-PkJobTimeLimit', 'Stop-PkTimedOutJob', 'Get-PkStartFailureCause', 'Resolve-PkArguments', 'Invoke-PkCallback', 'Write-PkCallbackLoopWarning', 'ConvertTo-PkArgumentStringCore') diff --git a/README.md b/README.md index 489e8e0..05b24b1 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ Requires Windows, with Windows PowerShell 5.1 or PowerShell 7. Download `Paralle ```powershell . "$PSScriptRoot\ParallelKit.ps1" -$null = Initialize-ParallelKit -MaxConcurrentProcesses 8 +$null = Initialize-ParallelKit ``` If you downloaded the file, Windows marks it as coming from the internet and your execution policy may refuse to load it. Run `Unblock-File .\ParallelKit.ps1` once to clear that. @@ -99,7 +99,7 @@ Dot-source it. It loads itself as an in-memory module named `ParallelKit`: nothi Its public functions are imported globally, so they stay available for the rest of that PowerShell session, and a `.GetNewClosure()` block can call them however your script was started. Its internal helpers stay inside the module. -`Initialize-ParallelKit` is optional. It registers the Ctrl+C handler, sets up child-process cleanup and sizes the thread pool up front; the engines set up what they need on first use either way. It returns an object with `AbortHandler`, `ChildProcessJob` and `ThreadPool`, each `$true` if that part worked (a failed abort handler or child-process cleanup also prints a warning), `Version`, the kit's version, and `StartMode`, how the kit starts programs here: `CreatedInJob` (inside the Job Object, the normal case), `AddedToJob` (started, then added to it just after) or `NoJob` (no cleanup if PowerShell is killed). It's the default for the session, not a promise for every program: one whose start can't be set up inside the Job Object falls back to `AddedToJob`, with a warning. +`Initialize-ParallelKit` is optional. It registers the Ctrl+C handler and sets up child-process cleanup up front; the engines set up what they need on first use either way. The kit leaves the thread pool's settings alone. It returns an object with `AbortHandler` and `ChildProcessJob`, each `$true` if that part worked (a failure also prints a warning), `ThreadPool`, always `$true` (kept for scripts that check it), `Version`, the kit's version, and `StartMode`, how the kit starts programs here: `CreatedInJob` (inside the Job Object, the normal case), `AddedToJob` (started, then added to it just after) or `NoJob` (no cleanup if PowerShell is killed). It's the default for the session, not a promise for every program: one whose start can't be set up inside the Job Object falls back to `AddedToJob`, with a warning. Each public function has built-in help: `Get-Help Invoke-PkProcessBatch -Full` shows its parameters, their defaults, what it returns and an example. From 48f868d9d980b7e453cc1a8dc16fb42105ffc97b Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:55:50 -0500 Subject: [PATCH 2/3] Kill each program's whole tree through a Job Object of its own Co-Authored-By: Claude Opus 5.5 --- ParallelKit.Harness.ps1 | 91 +++++++++++++++++++++++++++++- ParallelKit.ps1 | 119 ++++++++++++++++++++++++++++++++++------ README.md | 4 +- 3 files changed, 193 insertions(+), 21 deletions(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index d60f9d1..abec180 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -1449,7 +1449,9 @@ $parentJob.Proc.Dispose(); if ($null -ne $childProc) { $childProc.Dispose() } # A large kill: taskkill prints a line per process it kills (three per # `cmd /c ping` tree). Left unread, that fills its output pipe and taskkill # blocks until the 10 s wait gives up, leaving most children running. -$many = @(1..40 | ForEach-Object { Start-PkProcess -FilePath $cmdExe -Arguments (& $nestedPing 49) }) +# Started without tree jobs, so the kill goes to taskkill. +[PkChildProcessJob]::TestNoTreeJob = $true +try { $many = @(1..40 | ForEach-Object { Start-PkProcess -FilePath $cmdExe -Arguments (& $nestedPing 49) }) } finally { [PkChildProcessJob]::TestNoTreeJob = $false } $waitMany = [System.Diagnostics.Stopwatch]::StartNew() while ((Get-HarnessPingCount 49) -lt 40 -and $waitMany.Elapsed.TotalSeconds -lt 15) { Start-Sleep -Milliseconds 200 } $startedMany = Get-HarnessPingCount 49 @@ -3395,7 +3397,10 @@ function Protect-HarnessPingChild { } Reset-Harness -$tkStart = Start-PkProcess -FilePath $cmdExe -Arguments ('/c ""' + $pingExe + '" -n 7 127.0.0.1"') +# Without a tree job, so the kill goes to taskkill. Section BF has the same +# protected child killed through the tree job. +[PkChildProcessJob]::TestNoTreeJob = $true +try { $tkStart = Start-PkProcess -FilePath $cmdExe -Arguments ('/c ""' + $pingExe + '" -n 7 127.0.0.1"') } finally { [PkChildProcessJob]::TestNoTreeJob = $false } $tkChild = Protect-HarnessPingChild -Parent $tkStart.Proc $tkKilled = Stop-PkProcessTree -Process $tkStart.Proc $tkNote = if ($null -ne $tkChild -and -not $tkChild.HasExited) { "the protected child survived" } else { "taskkill killed the child anyway, so this run doesn't exercise the failure path" } @@ -4266,6 +4271,8 @@ Test-Assert $compiled "the stand-in taskkill compiles" if ($compiled) { $tkLog = [System.IO.Path]::ChangeExtension($tkExe, '.log') $env:PK_FAKE_TASKKILL_LOG = $tkLog + # These test the taskkill path: no tree jobs. + [PkChildProcessJob]::TestNoTreeJob = $true try { # Five programs, an argument limit that fits two PIDs: several # taskkill calls, each under the limit. The stand-in fails, so the @@ -4292,6 +4299,7 @@ if ($compiled) { foreach ($h in $tkHung) { Stop-Process -Id $h.ProcessId -Force -ErrorAction SilentlyContinue } foreach ($p in $tkProcs) { $p.Dispose() } } finally { + [PkChildProcessJob]::TestNoTreeJob = $false Remove-Item Env:PK_FAKE_TASKKILL_LOG, Env:PK_FAKE_TASKKILL_MODE -ErrorAction SilentlyContinue try { [System.IO.File]::Delete($tkLog) } catch { } # temp file only; best effort } @@ -4302,7 +4310,9 @@ if ($compiled) { } # The real taskkill, one PID per call: each call still kills the whole tree. -$tkStarted = @(1..3 | ForEach-Object { Start-PkProcess -FilePath $cmdExe -Arguments '/d /c ping -n 30 127.0.0.1 >nul' }) +# Without tree jobs, so the kill goes to taskkill. +[PkChildProcessJob]::TestNoTreeJob = $true +try { $tkStarted = @(1..3 | ForEach-Object { Start-PkProcess -FilePath $cmdExe -Arguments '/d /c ping -n 30 127.0.0.1 >nul' }) } finally { [PkChildProcessJob]::TestNoTreeJob = $false } $tkPings = @() foreach ($s in $tkStarted) { $p = $null @@ -4963,6 +4973,81 @@ foreach ($beWhich in 'Start-PkProcess', 'Invoke-PkProcessWithTimeout', 'Invoke-P Test-Assert (@($beErrors | Where-Object { $_ -ne 'ok' }).Count -eq 0 -and $beStarted.Count -eq 0) ("-MaxOutputChars -1 is refused by all four before anything starts (" + ($beErrors -join ' | ') + ")") Write-Host "" +Write-Host "=== BF: a kill reaches a child whose parent already exited ===" -ForegroundColor Cyan +# The program pings for a second, runs an inner cmd that starts a ping +# (-w 4325) and exits at once, leaving that ping orphaned, then pings +# (-w 4326) itself. taskkill /T finds a tree through parent PIDs, so it +# missed the orphan; each program's own Job Object doesn't. +Reset-Harness +function Get-HarnessTagPings([string]$Tag) { return @(Get-CimInstance Win32_Process -Filter "Name='PING.EXE' AND CommandLine LIKE '%-w $Tag %'") } +function Stop-HarnessTagPings { foreach ($tag in '4325', '4326', '4327') { foreach ($p in (Get-HarnessTagPings $tag)) { Stop-Process -Id $p.ProcessId -Force -ErrorAction SilentlyContinue } } } +function Wait-HarnessTagPings([string[]]$Tags) { + $sw = [System.Diagnostics.Stopwatch]::StartNew() + while ($sw.Elapsed.TotalSeconds -lt 10) { + if (@($Tags | Where-Object { @(Get-HarnessTagPings $_).Count -eq 0 }).Count -eq 0) { return $true } + Start-Sleep -Milliseconds 100 + } + return $false +} +$bfOrphanArgs = '/d /c ping -n 2 127.0.0.1 >nul & ' + $cmdExe + ' /d /c start /b ping -n 60 -w 4325 127.0.0.1 >nul & ping -n 60 -w 4326 127.0.0.1 >nul' +try { + foreach ($bfMode in 'created in the job', 'Process.Start fallback') { + [PkChildProcessJob]::TestUseProcessStart = ($bfMode -ne 'created in the job') + try { $bfJob = Start-PkProcess -FilePath $cmdExe -Arguments $bfOrphanArgs } finally { [PkChildProcessJob]::TestUseProcessStart = $false } + $bfUp = Wait-HarnessTagPings @('4325', '4326') + $bfHas = [PkChildProcessJob]::TestHasTree($bfJob['Proc']) + $bfKilled = Stop-PkProcessTree -Process $bfJob['Proc'] + Start-Sleep -Milliseconds 300 + $bfOrphans = @(Get-HarnessTagPings '4325').Count; $bfOwn = @(Get-HarnessTagPings '4326').Count + Test-Assert ($bfUp -and $bfHas -and $bfKilled -and $bfJob['Proc'].HasExited -and $bfOrphans -eq 0 -and $bfOwn -eq 0) ($bfMode + ": Stop-PkProcessTree kills a child whose parent already exited (both pings up: " + $bfUp + ", tree job: " + $bfHas + ", orphans left " + $bfOrphans + ", own left " + $bfOwn + ")") + $null = Complete-PkProcess -Job $bfJob -StreamTimeoutMs 0 -Dispose + Stop-HarnessTagPings + } + + # The time limit: the program and its orphan go, and the orphan no + # longer holds the job's output for the stream timeout after the kill. + $sw = [System.Diagnostics.Stopwatch]::StartNew() + $bfB = Invoke-PkProcessBatch -TimeoutSeconds 3 -Commands @(@{ FilePath = $cmdExe; Arguments = $bfOrphanArgs }) + $sw.Stop() + Start-Sleep -Milliseconds 300 + $bfOrphans = @(Get-HarnessTagPings '4325').Count + Test-Assert ($bfB.Results[0].TimedOut -and $bfOrphans -eq 0 -and $sw.Elapsed.TotalSeconds -lt 8) ("batch: a time-limit kill takes the orphan too, and the job ends soon after (" + [math]::Round($sw.Elapsed.TotalSeconds, 1) + " s, orphans left " + $bfOrphans + ")") + Stop-HarnessTagPings + + # An abort's force-stop. + Reset-PkAbortState + $bfB = Invoke-PkProcessBatch -MaxConcurrent 2 -AbortTimeoutSeconds 1 -OnComplete { param($c, $r) if ($c['Key'] -eq 'quick') { Request-PkAbort } } -Commands @(@{ FilePath = $cmdExe; Arguments = $bfOrphanArgs; Key = 'tree' }, @{ FilePath = $cmdExe; Arguments = '/d /c ping -n 4 127.0.0.1 >nul'; Key = 'quick' }) + Reset-PkAbortState + Start-Sleep -Milliseconds 300 + $bfOrphans = @(Get-HarnessTagPings '4325').Count + Test-Assert ($bfB.Aborted -and $bfB.ForceKilledCount -eq 1 -and $bfOrphans -eq 0) ("batch: an abort's force-stop takes the orphan too (force-stopped " + $bfB.ForceKilledCount + ", orphans left " + $bfOrphans + ")") + Stop-HarnessTagPings + + # A child that refuses PROCESS_TERMINATE still goes: the job is ended as a whole. + $bfStart = Start-PkProcess -FilePath $cmdExe -Arguments ('/c ""' + $pingExe + '" -n 7 127.0.0.1"') + $bfChild = Protect-HarnessPingChild -Parent $bfStart.Proc + $bfKilled = Stop-PkProcessTree -Process $bfStart.Proc + $bfGone = ($null -ne $bfChild -and $bfChild.WaitForExit(3000)) + Test-Assert ($null -ne $bfChild -and $bfKilled -and $bfGone) ("a child that refuses to be terminated is killed with its program's tree job (child found: " + ($null -ne $bfChild) + ", gone: " + $bfGone + ")") + $null = Complete-PkProcess -Job $bfStart -StreamTimeoutMs 0 -Dispose + if ($null -ne $bfChild) { $bfChild.Dispose() } + + # Unchanged: a program that exits on its own leaves what it started + # running, and Complete-PkProcess lets go of its tree job. + $bfJob = Start-PkProcess -FilePath $cmdExe -Arguments '/d /c start /b ping -n 30 -w 4327 127.0.0.1 >nul' + $null = Wait-PkProcess -Process $bfJob['Proc'] -TimeoutMs 10000 + $bfUp = Wait-HarnessTagPings @('4327') + $null = Complete-PkProcess -Job $bfJob -StreamTimeoutMs 0 + $bfReleased = -not [PkChildProcessJob]::TestHasTree($bfJob['Proc']) + $bfJob['Proc'].Dispose() + Test-Assert ($bfUp -and @(Get-HarnessTagPings '4327').Count -eq 1 -and $bfReleased) ("a leftover from a program that exits on its own keeps running, and the tree job is released (leftover up: " + $bfUp + ", released: " + $bfReleased + ")") +} finally { + [PkChildProcessJob]::TestUseProcessStart = $false + Stop-HarnessTagPings + Reset-PkAbortState +} +Write-Host "" + Set-PkMessageHandler -Handler $null Reset-PkAbortState diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index 2d386dc..ba66932 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -160,11 +160,10 @@ public static class PkState public static bool AbortLatched = false; public static bool CloseAbort = false; public static object MessageHandler = null; // a PowerShell scriptblock, or null for the default - public static volatile bool ThreadPoolRaised = false; // Bump this, and the check after the type definitions, whenever any C# // in this file changes: a session keeps the types it compiled first. - public const int TypesVersion = 10; + public const int TypesVersion = 11; // Locked: Register-/Unregister-PkCancellationSource are callable from // worker runspaces, and List is not safe for concurrent writers. @@ -236,6 +235,7 @@ using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; +using System.Runtime.CompilerServices; using Microsoft.Win32.SafeHandles; public sealed class PkStartedProcess @@ -415,6 +415,10 @@ public sealed class PkStreamReadTask : Task // A Windows Job Object with KILL_ON_JOB_CLOSE: every child process assigned // to it dies when this process exits for ANY reason, force-kill included. +// Each program the kit starts also gets a job of its own (its tree job), +// nested in that one, so the program and everything it started can be +// killed at once (KillTree), including a child whose parent has already +// exited, which taskkill /T can't find. public static class PkChildProcessJob { private const int JobObjectExtendedLimitInformation = 9; @@ -464,6 +468,60 @@ public static class PkChildProcessJob [DllImport("kernel32.dll", SetLastError = true)] private static extern bool CloseHandle(IntPtr hObject); + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool TerminateJobObject(IntPtr hJob, uint uExitCode); + + // A program's tree job, closed by ReleaseTree or, failing that, when its + // Process object is collected. Without KILL_ON_JOB_CLOSE: closing it + // leaves whatever is still running in the kit's job, as before. + private sealed class TreeJob : SafeHandleZeroOrMinusOneIsInvalid + { + public TreeJob(IntPtr h) : base(true) { SetHandle(h); } + protected override bool ReleaseHandle() { return CloseHandle(handle); } + } + private static readonly ConditionalWeakTable _treeJobs = new ConditionalWeakTable(); + + // Harness only: start programs without a tree job, to test the taskkill path. + public static bool TestNoTreeJob = false; + + private static void AddTree(Process process, IntPtr treeJob) + { + try { _treeJobs.Add(process, new TreeJob(treeJob)); } + catch (ArgumentException) { CloseHandle(treeJob); } // already has one (can't happen: each start is a new Process) + } + + // Kills every process in the program's tree job. False if it has none, + // or Windows refused: the caller then uses taskkill. + public static bool KillTree(Process process) + { + if (process == null) { return false; } + TreeJob j; + if (!_treeJobs.TryGetValue(process, out j)) { return false; } + bool added = false; + try + { + j.DangerousAddRef(ref added); + return TerminateJobObject(j.DangerousGetHandle(), 1); + } + catch (ObjectDisposedException) { return false; } // released meanwhile: taskkill instead + finally { if (added) { j.DangerousRelease(); } } + } + + // Closes the program's tree job: KillTree can't reach its tree after this. + public static void ReleaseTree(Process process) + { + if (process == null) { return; } + TreeJob j; + if (_treeJobs.TryGetValue(process, out j)) { _treeJobs.Remove(process); j.Dispose(); } + } + + // Harness only: whether the program has a tree job. + public static bool TestHasTree(Process process) + { + TreeJob j; + return process != null && _treeJobs.TryGetValue(process, out j); + } + // Never closed: Windows closes it when this process exits, and that is // what kills the children. private static IntPtr _job = IntPtr.Zero; @@ -501,15 +559,23 @@ public static class PkChildProcessJob } // Never throws: a failed assignment only loses the force-kill cleanup - // for that one process. + // for that one process. Once it's in the kit's job, it also gets a tree + // job (Windows nests it); without one, a kill uses taskkill. public static bool Assign(System.Diagnostics.Process process) { if (_job == IntPtr.Zero || process == null) { return false; } try { - if (AssignProcessToJobObject(_job, process.Handle)) { return true; } - LastError = Marshal.GetLastWin32Error(); - return false; + if (!AssignProcessToJobObject(_job, process.Handle)) { LastError = Marshal.GetLastWin32Error(); return false; } + if (!TestNoTreeJob) + { + IntPtr treeJob = CreateJobObject(IntPtr.Zero, null); + if (treeJob != IntPtr.Zero) + { + if (AssignProcessToJobObject(treeJob, process.Handle)) { AddTree(process, treeJob); } else { CloseHandle(treeJob); } + } + } + return true; } catch { return false; } } @@ -765,7 +831,7 @@ public static class PkChildProcessJob StringBuilder commandLine = BuildCommandLine(fileName, arguments); IntPtr outRead = IntPtr.Zero, outWrite = IntPtr.Zero, errRead = IntPtr.Zero, errWrite = IntPtr.Zero; - IntPtr attrList = IntPtr.Zero, handleArr = IntPtr.Zero, jobArr = IntPtr.Zero; + IntPtr attrList = IntPtr.Zero, handleArr = IntPtr.Zero, jobArr = IntPtr.Zero, treeJob = IntPtr.Zero; bool attrReady = false, created = false, setupFailed = false; IntPtr stdin = IntPtr.Zero; PROCESS_INFORMATION pi = new PROCESS_INFORMATION(); @@ -794,8 +860,14 @@ public static class PkChildProcessJob Marshal.WriteIntPtr(handleArr, 0, outWrite); Marshal.WriteIntPtr(handleArr, IntPtr.Size, errWrite); if (stdinInherit) { Marshal.WriteIntPtr(handleArr, 2 * IntPtr.Size, stdin); } - jobArr = Marshal.AllocHGlobal(IntPtr.Size); + // The kit's job, then the program's tree job, which Windows + // nests in it. No tree job if it can't be made: a kill + // then uses taskkill. + if (!TestNoTreeJob) { treeJob = CreateJobObject(IntPtr.Zero, null); } + int jobCount = treeJob != IntPtr.Zero ? 2 : 1; + jobArr = Marshal.AllocHGlobal(IntPtr.Size * jobCount); Marshal.WriteIntPtr(jobArr, _job); + if (jobCount == 2) { Marshal.WriteIntPtr(jobArr, IntPtr.Size, treeJob); } IntPtr size = IntPtr.Zero; InitializeProcThreadAttributeList(IntPtr.Zero, 2, 0, ref size); // fails by design: returns the size @@ -805,7 +877,7 @@ public static class PkChildProcessJob if (!UpdateProcThreadAttribute(attrList, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, handleArr, (IntPtr)(IntPtr.Size * handleCount), IntPtr.Zero, IntPtr.Zero) || !UpdateProcThreadAttribute(attrList, 0, PROC_THREAD_ATTRIBUTE_JOB_LIST, jobArr, - (IntPtr)IntPtr.Size, IntPtr.Zero, IntPtr.Zero)) + (IntPtr)(IntPtr.Size * jobCount), IntPtr.Zero, IntPtr.Zero)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } @@ -850,6 +922,7 @@ public static class PkChildProcessJob { if (outRead != IntPtr.Zero) { CloseHandle(outRead); } if (errRead != IntPtr.Zero) { CloseHandle(errRead); } + if (treeJob != IntPtr.Zero) { CloseHandle(treeJob); treeJob = IntPtr.Zero; } } } } @@ -882,9 +955,11 @@ public static class PkChildProcessJob if (stderrReader != null) { stderrReader.Dispose(); } if (errSafe != null) { errSafe.Dispose(); } else { CloseHandle(errRead); } CloseHandle(pi.hThread); CloseHandle(pi.hProcess); + if (treeJob != IntPtr.Zero) { CloseHandle(treeJob); } if (proc != null) { proc.Dispose(); } throw; } + if (treeJob != IntPtr.Zero) { AddTree(proc, treeJob); } CloseHandle(pi.hThread); CloseHandle(pi.hProcess); @@ -901,7 +976,7 @@ public static class PkChildProcessJob # A session that loaded an older copy keeps its compiled types. Say so, # rather than fail later with a missing-method error. Keep this number in # sync with [PkState]::TypesVersion. -if ((& { try { [PkState]::TypesVersion } catch { 0 } }) -ne 10) { +if ((& { try { [PkState]::TypesVersion } catch { 0 } }) -ne 11) { throw "ParallelKit: this PowerShell session already loaded an older version of the kit, and its compiled parts can't be swapped out. Close this window and start a new one." } @@ -1626,10 +1701,13 @@ function Stop-PkProcessTree { # .SYNOPSIS # Kills one or more programs and every program they started. # .DESCRIPTION - # Uses taskkill /T, in as few calls as Windows' command-line limit allows, - # so none is left running and holding a program's output open. If taskkill - # can't start, fails, or is still running after 10 seconds (it's killed - # then), each program is killed directly. + # A program the kit started is killed through its own Job Object, with + # everything it started, even a child whose parent has already exited, so + # none is left running and holding its output open. Any other program (or + # one Windows won't end that way) goes to taskkill /T, in as few calls as + # Windows' command-line limit allows; if taskkill can't start, fails, or is + # still running after 10 seconds (it's killed then), each program is killed + # directly. # .PARAMETER Process # The program(s), such as the Proc that Start-PkProcess returns. # .OUTPUTS @@ -1661,12 +1739,18 @@ function Stop-PkProcessTreeCore { 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 + # A program the kit started has a tree job: killing that ends the program + # and everything it started at once, a child whose parent already exited + # included. taskkill /T only finds children through their parent, so it's + # for the rest: a program without a tree job, or one Windows wouldn't end. + $viaTaskkill = [System.Collections.Generic.List[System.Diagnostics.Process]]::new() + foreach ($p in $alive) { if ([PkChildProcessJob]::KillTree($p)) { $killed = $true } else { $viaTaskkill.Add($p) } } # One command line for every PID passes Windows' 32,767-character limit at # roughly 2,500-3,000 programs, and then taskkill can't start at all: so as # many taskkill calls as the limit needs. if ($MaxArgumentLength -le 0) { $MaxArgumentLength = 32000 - $TaskkillPath.Length } - $ids = [int[]]@(foreach ($p in $alive) { $p.Id }) - foreach ($taskkillArgs in (Split-PkTaskkillArguments -Ids $ids -MaxLength $MaxArgumentLength)) { + $ids = [int[]]@(foreach ($p in $viaTaskkill) { $p.Id }) + foreach ($taskkillArgs in $(if ($ids.Count -gt 0) { Split-PkTaskkillArguments -Ids $ids -MaxLength $MaxArgumentLength })) { $psi = [System.Diagnostics.ProcessStartInfo]::new($TaskkillPath, $taskkillArgs) $psi.UseShellExecute = $false $psi.CreateNoWindow = $true @@ -1983,6 +2067,9 @@ function Complete-PkProcess { $result.ExitCode = $Job['Proc'].ExitCode # The program's end, not the output's: before Dispose, which loses it. $result.Duration = Get-PkProcessDuration -Process $Job['Proc'] + # The program has exited: its tree job's handle can go. Anything it left + # running stays in the kit's job, as before. + if ($Job['Proc'] -is [System.Diagnostics.Process]) { [PkChildProcessJob]::ReleaseTree($Job['Proc']) } if ($Dispose) { $Job['Proc'].Dispose() } return [pscustomobject]$result } diff --git a/README.md b/README.md index 9f95dc4..d0a5f4f 100644 --- a/README.md +++ b/README.md @@ -282,7 +282,7 @@ To leave any callback early, use `return`. PowerShell sends a `continue` or `bre | `Start-PkProcess -FilePath -Arguments` | Start one hidden, with output captured in the background. Returns `@{ Proc; StdoutTask; StderrTask; Stopwatch }`; throws if it can't start. | | `Wait-PkProcess -Process -TimeoutMs` | Wait for exit with a timeout that can't overrun. Returns `$true` if it exited. | | `Complete-PkProcess -Job` | Collect exit code and output from a finished one (`-Job` is what `Start-PkProcess` returned). Same result as `Invoke-PkProcessWithTimeout`. | -| `Stop-PkProcessTree -Process` | Kill one or more programs and every program they started, with `taskkill` (as few calls as Windows' command-line limit allows). For one program, returns `$true` if it killed it (even if part of its tree couldn't be) and `$false` if it had already exited. | +| `Stop-PkProcessTree -Process` | Kill one or more programs and every program they started. A program the kit started is killed through its own Job Object, which also reaches a child whose parent has already exited; anything else goes to `taskkill /T` (as few calls as Windows' command-line limit allows). For one program, returns `$true` if it killed it (even if part of its tree couldn't be) and `$false` if it had already exited. | `Invoke-PkProcessWithTimeout` and `Start-PkProcess` also take `-WorkingDirectory`. @@ -418,7 +418,7 @@ The ones in this README: the engines, the [single-process functions](#single-pro **Does a program I start get my Ctrl+C too?** -No. Programs start without a window, in a hidden console of their own, so Ctrl+C in your window never reaches them. That's what lets them finish while the kit drains. When it does need to stop one (a timeout, or an abort's force-stop), the kit kills it along with anything it started. +No. Programs start without a window, in a hidden console of their own, so Ctrl+C in your window never reaches them. That's what lets them finish while the kit drains. When it does need to stop one (a timeout, or an abort's force-stop), the kit kills it along with anything it started, through a Job Object of the program's own, so even a child whose parent has already exited goes with it. **Was this written by AI?** From 249162761587b46bede04a77a62e9643df95c4eb Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Thu, 19 Nov 2026 03:09:21 -0500 Subject: [PATCH 3/3] Retry a start without the tree job if Windows refuses it, release it once a kill ends the tree Co-Authored-By: Claude Opus 5.5 --- ParallelKit.Harness.ps1 | 15 +++++++- ParallelKit.ps1 | 78 ++++++++++++++++++++++++++++------------- 2 files changed, 68 insertions(+), 25 deletions(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index abec180..0e0822c 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -4997,9 +4997,10 @@ try { $bfUp = Wait-HarnessTagPings @('4325', '4326') $bfHas = [PkChildProcessJob]::TestHasTree($bfJob['Proc']) $bfKilled = Stop-PkProcessTree -Process $bfJob['Proc'] + $bfReleased = -not [PkChildProcessJob]::TestHasTree($bfJob['Proc']) Start-Sleep -Milliseconds 300 $bfOrphans = @(Get-HarnessTagPings '4325').Count; $bfOwn = @(Get-HarnessTagPings '4326').Count - Test-Assert ($bfUp -and $bfHas -and $bfKilled -and $bfJob['Proc'].HasExited -and $bfOrphans -eq 0 -and $bfOwn -eq 0) ($bfMode + ": Stop-PkProcessTree kills a child whose parent already exited (both pings up: " + $bfUp + ", tree job: " + $bfHas + ", orphans left " + $bfOrphans + ", own left " + $bfOwn + ")") + Test-Assert ($bfUp -and $bfHas -and $bfKilled -and $bfReleased -and $bfJob['Proc'].HasExited -and $bfOrphans -eq 0 -and $bfOwn -eq 0) ($bfMode + ": Stop-PkProcessTree kills a child whose parent already exited, and lets go of the tree job (both pings up: " + $bfUp + ", tree job: " + $bfHas + ", released: " + $bfReleased + ", orphans left " + $bfOrphans + ", own left " + $bfOwn + ")") $null = Complete-PkProcess -Job $bfJob -StreamTimeoutMs 0 -Dispose Stop-HarnessTagPings } @@ -5023,6 +5024,18 @@ try { Test-Assert ($bfB.Aborted -and $bfB.ForceKilledCount -eq 1 -and $bfOrphans -eq 0) ("batch: an abort's force-stop takes the orphan too (force-stopped " + $bfB.ForceKilledCount + ", orphans left " + $bfOrphans + ")") Stop-HarnessTagPings + # If Windows refuses to nest the tree job, the program still starts, + # without one, and a kill goes to taskkill. + [PkChildProcessJob]::TestRefuseTreeJob = $true + try { $bfJob = Start-PkProcess -FilePath $cmdExe -Arguments '/d /c echo refused& ping -n 30 -w 4327 127.0.0.1 >nul' } finally { [PkChildProcessJob]::TestRefuseTreeJob = $false } + $bfUp = Wait-HarnessTagPings @('4327') + $bfHas = [PkChildProcessJob]::TestHasTree($bfJob['Proc']) + $bfKilled = Stop-PkProcessTree -Process $bfJob['Proc'] + Start-Sleep -Milliseconds 300 + $bfR = Complete-PkProcess -Job $bfJob -StreamTimeoutMs 2000 -Dispose + Test-Assert ($bfUp -and -not $bfHas -and $bfKilled -and @(Get-HarnessTagPings '4327').Count -eq 0 -and $bfR.Stdout -match 'refused') ("a start where Windows refuses the tree job runs without one, and taskkill still kills its tree (tree job: " + $bfHas + ", killed: " + $bfKilled + ", output '" + ([string]$bfR.Stdout).Trim() + "')") + Stop-HarnessTagPings + # A child that refuses PROCESS_TERMINATE still goes: the job is ended as a whole. $bfStart = Start-PkProcess -FilePath $cmdExe -Arguments ('/c ""' + $pingExe + '" -n 7 127.0.0.1"') $bfChild = Protect-HarnessPingChild -Parent $bfStart.Proc diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index ba66932..6d5f40f 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -481,8 +481,11 @@ public static class PkChildProcessJob } private static readonly ConditionalWeakTable _treeJobs = new ConditionalWeakTable(); - // Harness only: start programs without a tree job, to test the taskkill path. + // Harness only: start programs without a tree job, to test the taskkill + // path; or have TryStart's first CreateProcess, with the tree job, count + // as refused, to test its retry without one. public static bool TestNoTreeJob = false; + public static bool TestRefuseTreeJob = false; private static void AddTree(Process process, IntPtr treeJob) { @@ -497,14 +500,17 @@ public static class PkChildProcessJob if (process == null) { return false; } TreeJob j; if (!_treeJobs.TryGetValue(process, out j)) { return false; } - bool added = false; + bool added = false, ended = false; try { j.DangerousAddRef(ref added); - return TerminateJobObject(j.DangerousGetHandle(), 1); + ended = TerminateJobObject(j.DangerousGetHandle(), 1); } catch (ObjectDisposedException) { return false; } // released meanwhile: taskkill instead finally { if (added) { j.DangerousRelease(); } } + // The tree is gone: its job's handle can go now rather than with the Process. + if (ended) { ReleaseTree(process); } + return ended; } // Closes the program's tree job: KillTree can't reach its tree after this. @@ -753,6 +759,22 @@ public static class PkChildProcessJob } } + // The start's two attributes: the handles the child inherits and its + // jobs. 0, or the Win32 error (the list is then left uninitialized). + private static int SetStartAttributes(IntPtr attrList, IntPtr attrSize, IntPtr handleArr, int handleCount, IntPtr jobArr, int jobCount) + { + IntPtr size = attrSize; + if (!InitializeProcThreadAttributeList(attrList, 2, 0, ref size)) { return Marshal.GetLastWin32Error(); } + if (UpdateProcThreadAttribute(attrList, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, handleArr, (IntPtr)(IntPtr.Size * handleCount), IntPtr.Zero, IntPtr.Zero) + && UpdateProcThreadAttribute(attrList, 0, PROC_THREAD_ATTRIBUTE_JOB_LIST, jobArr, (IntPtr)(IntPtr.Size * jobCount), IntPtr.Zero, IntPtr.Zero)) + { + return 0; + } + int e = Marshal.GetLastWin32Error(); + DeleteProcThreadAttributeList(attrList); + return e; + } + // How TryStart holds .NET's start lock ("Monitor:" or // "Lock:"), or null when it can't and the kit uses Process.Start. public static string StartLockKind { get { StartLock l = GetStartLock(); return l == null ? null : l.Kind; } } @@ -832,6 +854,8 @@ public static class PkChildProcessJob IntPtr outRead = IntPtr.Zero, outWrite = IntPtr.Zero, errRead = IntPtr.Zero, errWrite = IntPtr.Zero; IntPtr attrList = IntPtr.Zero, handleArr = IntPtr.Zero, jobArr = IntPtr.Zero, treeJob = IntPtr.Zero; + IntPtr attrSize = IntPtr.Zero; + int handleCount = 0, jobCount = 0; bool attrReady = false, created = false, setupFailed = false; IntPtr stdin = IntPtr.Zero; PROCESS_INFORMATION pi = new PROCESS_INFORMATION(); @@ -855,7 +879,7 @@ public static class PkChildProcessJob uint hflags; bool stdinInherit = stdin != IntPtr.Zero && stdin != new IntPtr(-1) && GetHandleInformation(stdin, out hflags) && (hflags & HANDLE_FLAG_INHERIT) != 0; - int handleCount = stdinInherit ? 3 : 2; + handleCount = stdinInherit ? 3 : 2; handleArr = Marshal.AllocHGlobal(IntPtr.Size * handleCount); Marshal.WriteIntPtr(handleArr, 0, outWrite); Marshal.WriteIntPtr(handleArr, IntPtr.Size, errWrite); @@ -864,23 +888,16 @@ public static class PkChildProcessJob // nests in it. No tree job if it can't be made: a kill // then uses taskkill. if (!TestNoTreeJob) { treeJob = CreateJobObject(IntPtr.Zero, null); } - int jobCount = treeJob != IntPtr.Zero ? 2 : 1; - jobArr = Marshal.AllocHGlobal(IntPtr.Size * jobCount); + jobCount = treeJob != IntPtr.Zero ? 2 : 1; + jobArr = Marshal.AllocHGlobal(IntPtr.Size * 2); Marshal.WriteIntPtr(jobArr, _job); if (jobCount == 2) { Marshal.WriteIntPtr(jobArr, IntPtr.Size, treeJob); } - IntPtr size = IntPtr.Zero; - InitializeProcThreadAttributeList(IntPtr.Zero, 2, 0, ref size); // fails by design: returns the size - attrList = Marshal.AllocHGlobal(size); - if (!InitializeProcThreadAttributeList(attrList, 2, 0, ref size)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } + InitializeProcThreadAttributeList(IntPtr.Zero, 2, 0, ref attrSize); // fails by design: returns the size + attrList = Marshal.AllocHGlobal(attrSize); + int attrError = SetStartAttributes(attrList, attrSize, handleArr, handleCount, jobArr, jobCount); + if (attrError != 0) { throw new Win32Exception(attrError); } attrReady = true; - if (!UpdateProcThreadAttribute(attrList, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, handleArr, - (IntPtr)(IntPtr.Size * handleCount), IntPtr.Zero, IntPtr.Zero) - || !UpdateProcThreadAttribute(attrList, 0, PROC_THREAD_ATTRIBUTE_JOB_LIST, jobArr, - (IntPtr)(IntPtr.Size * jobCount), IntPtr.Zero, IntPtr.Zero)) - { - throw new Win32Exception(Marshal.GetLastWin32Error()); - } if (TestFailSetup) { throw new Win32Exception(5); } } catch (Win32Exception e) @@ -902,10 +919,22 @@ public static class PkChildProcessJob // the program is in the job already, so nothing can escape. uint flags = CREATE_NO_WINDOW | CREATE_SUSPENDED | EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT; string dir = String.IsNullOrEmpty(workingDirectory) ? null : workingDirectory; - if (!CreateProcess(null, commandLine, IntPtr.Zero, IntPtr.Zero, true, flags, IntPtr.Zero, dir, ref si, out pi)) + bool started = !TestRefuseTreeJob || jobCount == 1; + if (started) { started = CreateProcess(null, commandLine, IntPtr.Zero, IntPtr.Zero, true, flags, IntPtr.Zero, dir, ref si, out pi); } + if (!started && jobCount == 2) { - throw new Win32Exception(Marshal.GetLastWin32Error()); + // Once more without the tree job, in case that's what Windows + // refused (nesting it): the program still starts, and a kill + // then uses taskkill. A program that can't start at all fails + // again, with its own error. + DeleteProcThreadAttributeList(attrList); attrReady = false; + CloseHandle(treeJob); treeJob = IntPtr.Zero; jobCount = 1; + int retryError = SetStartAttributes(attrList, attrSize, handleArr, handleCount, jobArr, jobCount); + if (retryError != 0) { throw new Win32Exception(retryError); } + attrReady = true; + started = CreateProcess(null, commandLine, IntPtr.Zero, IntPtr.Zero, true, flags, IntPtr.Zero, dir, ref si, out pi); } + if (!started) { throw new Win32Exception(Marshal.GetLastWin32Error()); } created = true; } finally @@ -1703,11 +1732,12 @@ function Stop-PkProcessTree { # .DESCRIPTION # A program the kit started is killed through its own Job Object, with # everything it started, even a child whose parent has already exited, so - # none is left running and holding its output open. Any other program (or - # one Windows won't end that way) goes to taskkill /T, in as few calls as - # Windows' command-line limit allows; if taskkill can't start, fails, or is - # still running after 10 seconds (it's killed then), each program is killed - # directly. + # none is left running and holding its output open. Any other program, one + # without such a job (StartMode NoJob, or Windows refused it), or one + # Windows won't end that way goes to taskkill /T, which misses a child + # whose parent has exited, in as few calls as Windows' command-line limit + # allows; if taskkill can't start, fails, or is still running after 10 + # seconds (it's killed then), each program is killed directly. # .PARAMETER Process # The program(s), such as the Proc that Start-PkProcess returns. # .OUTPUTS