From b8480f28a5d60addbd43c23db649893a69d2d4cb Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:19:15 -0500 Subject: [PATCH 1/2] Count a tree kill as done when taskkill reports a partial failure taskkill /T exits non-zero if any process in the tree couldn't be killed, even when it killed the program itself. For a single program, Stop-PkProcessTree then returned $false, so a job killed for its time limit could show TimedOut = $false. It happened most often with `cmd /c`: taskkill kills the children first, cmd ends on its own as soon as its child dies, and taskkill then finds it already gone. It also happens when a child can't be killed. The "any listed process is now gone" check, which already covered several programs, now covers one too. Co-Authored-By: Claude Opus 5.5 --- ParallelKit.Harness.ps1 | 71 +++++++++++++++++++++++++++++++++++++++++ ParallelKit.ps1 | 12 ++++--- 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index 1832331..ade5d9d 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -3146,6 +3146,77 @@ Test-Assert ($null -ne $errProp -and @($errProp.Value).Count -eq 0) "an empty ru Reset-Harness Write-Host "" +Write-Host "=== AJ: a tree kill counts a program it killed, even if taskkill reports failure ===" -ForegroundColor Cyan + +# taskkill /T exits non-zero when any process in the tree couldn't be killed, +# even though the program itself was. A child that refuses PROCESS_TERMINATE +# (its DACL denies it) makes that happen every time. The Job Object still +# ends it when this process exits, and it's a short ping anyway. An elevated +# taskkill may ignore the DACL; then the child dies too, and the check below +# still holds but doesn't exercise the failure path (the message says so). +if (-not ([System.Management.Automation.PSTypeName]'PkHarnessProcSec').Type) { + Add-Type -TypeDefinition @' +using System; +using System.Runtime.InteropServices; +using System.Security.AccessControl; +using System.Security.Principal; +public static class PkHarnessProcSec { + [DllImport("advapi32.dll")] + static extern uint SetSecurityInfo(IntPtr handle, int objectType, uint securityInfo, IntPtr owner, IntPtr group, byte[] dacl, IntPtr sacl); + // Everyone: everything but PROCESS_TERMINATE. Returns the Win32 error, 0 = done. + public static uint DenyTerminate(IntPtr handle) { + var everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null); + var dacl = new RawAcl(2, 2); + dacl.InsertAce(0, new CommonAce(AceFlags.None, AceQualifier.AccessAllowed, 0x1FFFFE, everyone, false, null)); + dacl.InsertAce(0, new CommonAce(AceFlags.None, AceQualifier.AccessDenied, 0x0001, everyone, false, null)); + byte[] bytes = new byte[dacl.BinaryLength]; + dacl.GetBinaryForm(bytes, 0); + return SetSecurityInfo(handle, 6 /* SE_KERNEL_OBJECT */, 4 /* DACL_SECURITY_INFORMATION */, IntPtr.Zero, IntPtr.Zero, bytes, IntPtr.Zero); + } +} +'@ +} +function Protect-HarnessPingChild { + # Waits for the ping that $Parent started and makes it refuse to be + # terminated. Returns its Process, or $null if it couldn't. + param([System.Diagnostics.Process]$Parent) + $child = $null + for ($i = 0; $i -lt 50 -and $null -eq $child; $i++) { + Start-Sleep -Milliseconds 100 + $child = Get-CimInstance Win32_Process -Filter ("ParentProcessId = " + $Parent.Id + " AND Name = 'PING.EXE'") + } + if ($null -eq $child) { return $null } + $childProc = [System.Diagnostics.Process]::GetProcessById([int]$child.ProcessId) + if ([PkHarnessProcSec]::DenyTerminate($childProc.Handle) -ne 0) { return $null } + return $childProc +} + +Reset-Harness +$tkStart = Start-PkProcess -FilePath $cmdExe -Arguments ('/c ""' + $pingExe + '" -n 7 127.0.0.1"') +$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" } +Test-Assert ($null -ne $tkChild -and $tkKilled -and $tkStart.Proc.HasExited) ("Stop-PkProcessTree returns `$true for a program it killed when part of its tree couldn't be (returned " + $tkKilled + "; " + $tkNote + ")") +$tkStart.Proc.Dispose() + +# The symptom: a job killed for its time limit showed TimedOut = $false. +$tkFeed = @{ Given = $false } +$tkLog = [System.Collections.Generic.List[object]]::new() +$null = Invoke-PkProcessDispatch -MaxConcurrent 1 -TimeoutSeconds 1 ` + -GroupProvider { if ($tkFeed.Given) { return $null }; $tkFeed.Given = $true; return 'g' }.GetNewClosure() ` + -JobBuilder { param($g) return @(@{ FilePath = $cmdExe; Arguments = ('/c ""' + $pingExe + '" -n 7 127.0.0.1"') }) }.GetNewClosure() ` + -JobStarter { + param($job) + $s = Start-PkProcess -FilePath $job['FilePath'] -Arguments $job['Arguments'] + $job['Proc'] = $s.Proc; $job['StdoutTask'] = $s.StdoutTask; $job['StderrTask'] = $s.StderrTask; $job['Stopwatch'] = $s.Stopwatch + $job['Child'] = Protect-HarnessPingChild -Parent $s.Proc + return $job + } ` + -JobFinalize { param($job, $q) $tkLog.Add(@{ TimedOut = $job['TimedOut']; Protected = ($null -ne $job['Child']) }) }.GetNewClosure() +Test-Assert ($tkLog.Count -eq 1 -and $tkLog[0].Protected -and $tkLog[0].TimedOut -eq $true) ("a job killed for its time limit has TimedOut = `$true even when part of its tree survives (" + (@($tkLog | ForEach-Object { 'TimedOut=' + $_.TimedOut }) -join ',') + ")") +Reset-Harness +Write-Host "" + Set-PkMessageHandler -Handler $null Reset-PkAbortState diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index 7735628..456a111 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -1023,10 +1023,14 @@ function Stop-PkProcessTree { if ($taskkill.WaitForExit(10000)) { $killed = ($taskkill.ExitCode -eq 0) } } finally { $taskkill.Dispose() } } catch { } # taskkill missing or blocked: the fallback below still kills each process itself - # With several PIDs taskkill reports failure if any one was already gone - # (it exited, or was a child in another listed tree) even when it killed - # the rest; count it as killing them if any listed process is now gone. - if (-not $killed -and $alive.Count -gt 1) { + # taskkill reports failure if any process in the trees couldn't be killed, + # even when it killed the program itself: one already gone (a child in + # another listed tree, or a `cmd /c` that ended on its own once taskkill + # killed its child, which it does first), or a child it wasn't allowed + # to kill. Count it as killing them if any listed process is now gone. + # A program that exited on its own in the moment since the check above + # counts as killed too; the callers only kill one that ran too long. + if (-not $killed) { foreach ($p in $alive) { if ($p.HasExited) { $killed = $true; break } } } foreach ($p in $alive) { From 0f2a42717af64abd8d19ac4f18a388e7592937ec Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:25:00 -0500 Subject: [PATCH 2/2] Correct a comment; dispose the tree-kill test's child processes Co-Authored-By: Claude Opus 5.5 --- ParallelKit.Harness.ps1 | 5 +++-- ParallelKit.ps1 | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index ade5d9d..19d5525 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -3198,6 +3198,7 @@ $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" } Test-Assert ($null -ne $tkChild -and $tkKilled -and $tkStart.Proc.HasExited) ("Stop-PkProcessTree returns `$true for a program it killed when part of its tree couldn't be (returned " + $tkKilled + "; " + $tkNote + ")") $tkStart.Proc.Dispose() +if ($null -ne $tkChild) { $tkChild.Dispose() } # The symptom: a job killed for its time limit showed TimedOut = $false. $tkFeed = @{ Given = $false } @@ -3212,8 +3213,8 @@ $null = Invoke-PkProcessDispatch -MaxConcurrent 1 -TimeoutSeconds 1 ` $job['Child'] = Protect-HarnessPingChild -Parent $s.Proc return $job } ` - -JobFinalize { param($job, $q) $tkLog.Add(@{ TimedOut = $job['TimedOut']; Protected = ($null -ne $job['Child']) }) }.GetNewClosure() -Test-Assert ($tkLog.Count -eq 1 -and $tkLog[0].Protected -and $tkLog[0].TimedOut -eq $true) ("a job killed for its time limit has TimedOut = `$true even when part of its tree survives (" + (@($tkLog | ForEach-Object { 'TimedOut=' + $_.TimedOut }) -join ',') + ")") + -JobFinalize { param($job, $q) $tkLog.Add(@{ TimedOut = $job['TimedOut']; Protected = ($null -ne $job['Child']) }); if ($null -ne $job['Child']) { $job['Child'].Dispose() } }.GetNewClosure() +Test-Assert ($tkLog.Count -eq 1 -and $tkLog[0].Protected -and $tkLog[0].TimedOut -eq $true) ("a job killed for its time limit has TimedOut = `$true even when part of its tree survives (" + (@($tkLog | ForEach-Object { 'TimedOut=' + $_.TimedOut + ', child protected=' + $_.Protected }) -join '; ') + ")") Reset-Harness Write-Host "" diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index 456a111..142f565 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -1029,7 +1029,8 @@ function Stop-PkProcessTree { # killed its child, which it does first), or a child it wasn't allowed # to kill. Count it as killing them if any listed process is now gone. # A program that exited on its own in the moment since the check above - # counts as killed too; the callers only kill one that ran too long. + # counts as killed too. Of the kit's callers only Stop-PkTimedOutJob reads + # the result, and it only kills a program already past its time limit. if (-not $killed) { foreach ($p in $alive) { if ($p.HasExited) { $killed = $true; break } } }