diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index f26c018..22ea0be 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -5969,6 +5969,26 @@ try { try { Test-HarnessKillTree 'no tree job, a second time' { Invoke-HarnessTreeBatch @{ KillTreeOnExit = $true; StreamTimeoutMs = 500 } @{} $bjHeld } -Left } finally { [PkChildProcessJob]::TestNoTreeJob = $false } Test-Assert (@($script:messages | Where-Object { $_.Message -like '*-KillTreeOnExit*' }).Count -eq 1) ("and only once per process (" + @($script:messages | Where-Object { $_.Message -like '*-KillTreeOnExit*' }).Count + " warning(s) after a second program)") + # A tree job Windows won't end (TerminateJobObject fails) leaves what the + # program started running too: one warning naming the process, once per + # process, in each engine. + $bjFailTerminate = @( + @{ Name = 'tree job not ended, batch'; Run = { Invoke-HarnessTreeBatch @{ KillTreeOnExit = $true; StreamTimeoutMs = 500 } @{} $bjHeld } } + @{ Name = 'tree job not ended, dispatcher'; Run = { Invoke-HarnessTreeDispatch @{ KillTreeOnExit = $true; StreamTimeoutMs = 500 } @{} $bjHeld } } + @{ Name = 'tree job not ended, one program'; Run = { Invoke-PkProcessWithTimeout -FilePath $cmdExe -Arguments $bjHeld -KillTreeOnExit -StreamTimeoutMs 500 } } + ) + foreach ($bjCase in $bjFailTerminate) { + Reset-Harness + [PkChildProcessJob]::TestResetJobWarnings() + [PkChildProcessJob]::TestFailTerminate = $true + try { Test-HarnessKillTree $bjCase.Name $bjCase.Run -Left } finally { [PkChildProcessJob]::TestFailTerminate = $false } + $bjWarned = @($script:messages | Where-Object { $_.Level -eq 'Warn' -and $_.Message -like '*-KillTreeOnExit*process *may still be running*' }) + Test-Assert ($script:messages.Count -eq 1 -and $bjWarned.Count -eq 1) ("and says so in one warning naming the process (" + $script:messages.Count + " message(s): " + (@($script:messages | ForEach-Object { $_.Message }) -join ' | ') + ")") + } + [PkChildProcessJob]::TestFailTerminate = $true + try { Test-HarnessKillTree 'tree job not ended, a second time' { Invoke-HarnessTreeBatch @{ KillTreeOnExit = $true; StreamTimeoutMs = 500 } @{} $bjHeld } -Left } finally { [PkChildProcessJob]::TestFailTerminate = $false } + Test-Assert (@($script:messages | Where-Object { $_.Message -like '*may still be running*' }).Count -eq 1) ("and only once per process (" + @($script:messages | Where-Object { $_.Message -like '*may still be running*' }).Count + " warning(s) after a second program)") + # A force-stop: the program has exited, but the caller's poll never says # done, so neither the poll nor Complete-PkProcess kills what it left. Reset-Harness diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index 3783a54..edf169b 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -507,9 +507,11 @@ public static class PkChildProcessJob // 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. + // as refused, to test its retry without one. TestFailTerminate makes + // KillTree's TerminateJobObject count as refused, leaving the tree running. public static bool TestNoTreeJob = false; public static bool TestRefuseTreeJob = false; + public static bool TestFailTerminate = false; private static void AddTree(Process process, IntPtr treeJob) { @@ -528,7 +530,7 @@ public static class PkChildProcessJob try { j.DangerousAddRef(ref added); - ended = TerminateJobObject(j.DangerousGetHandle(), 1); + ended = !TestFailTerminate && TerminateJobObject(j.DangerousGetHandle(), 1); } catch (ObjectDisposedException) { return false; } // released meanwhile: taskkill instead finally { if (added) { j.DangerousRelease(); } } @@ -863,12 +865,14 @@ public static class PkChildProcessJob public static void TestResetFallbackWarning() { Interlocked.Exchange(ref _fallbackWarned, 0); } // The same, once each, for a Job Object that couldn't be set up when // first needed, for a running program Windows wouldn't add to it, and for - // -KillTreeOnExit on a program without a tree job. - private static int _initWarned = 0, _assignWarned = 0, _noTreeWarned = 0; + // -KillTreeOnExit on a program without a tree job, or whose tree job + // Windows wouldn't end. + private static int _initWarned = 0, _assignWarned = 0, _noTreeWarned = 0, _killTreeWarned = 0; public static bool ClaimInitWarning() { return Interlocked.Exchange(ref _initWarned, 1) == 0; } public static bool ClaimAssignWarning() { return Interlocked.Exchange(ref _assignWarned, 1) == 0; } public static bool ClaimNoTreeWarning() { return Interlocked.Exchange(ref _noTreeWarned, 1) == 0; } - public static void TestResetJobWarnings() { Interlocked.Exchange(ref _initWarned, 0); Interlocked.Exchange(ref _assignWarned, 0); Interlocked.Exchange(ref _noTreeWarned, 0); } + public static bool ClaimKillTreeWarning() { return Interlocked.Exchange(ref _killTreeWarned, 1) == 0; } + public static void TestResetJobWarnings() { Interlocked.Exchange(ref _initWarned, 0); Interlocked.Exchange(ref _assignWarned, 0); Interlocked.Exchange(ref _noTreeWarned, 0); Interlocked.Exchange(ref _killTreeWarned, 0); } private static StringBuilder BuildCommandLine(string fileName, string arguments) { @@ -2059,8 +2063,9 @@ function Stop-PkExitedJobTree { # running, through its tree job. Does nothing while it runs, or for a .Proc # that isn't a Process. A program without a tree job (Windows wouldn't # give it one, the kit has no Job Object, or a custom starter started it) - # can't be reached: that warns, once per process. One try only: the - # dispatcher's poll runs this every pass. + # can't be reached: that warns, once per process, and so does a tree job + # Windows won't end. One try only: the dispatcher's poll runs this every + # pass. param([hashtable]$Job) $proc = $Job['Proc'] if ($proc -isnot [System.Diagnostics.Process] -or $proc.HasExited -ne $true) { return } @@ -2071,7 +2076,11 @@ function Stop-PkExitedJobTree { } return } - $null = [PkChildProcessJob]::KillTree($proc) + # Windows refusing to end the tree job has no fallback: with the program + # gone, taskkill /T can't find its children. + if (-not [PkChildProcessJob]::KillTree($proc) -and [PkChildProcessJob]::ClaimKillTreeWarning()) { + Write-PkMessage ("[WARNING] -KillTreeOnExit couldn't stop what process " + $proc.Id + " left running: Windows wouldn't end the program's own Job Object, so anything it started may still be running. It still ends when PowerShell does. Shown once.") -Level Warn + } } function Wait-PkProcess { diff --git a/README.md b/README.md index 73fc3ec..e438a7b 100644 --- a/README.md +++ b/README.md @@ -310,7 +310,7 @@ The per-command keys go on the command itself, for example one that keeps all of `-StreamTimeoutMs` (default 15000) is how long to wait for a program's output after it exits; see `StreamTimedOut` in [Process results](#process-results). -`-KillTreeOnExit` kills whatever a program started that's still running when the program itself exits, whether or not it holds the program's output open. It's off by default, since some tools leave a build server or a daemon running on purpose. A command's own `KillTreeOnExit` key, `$true` or `$false`, overrides it either way. `Invoke-PkProcessWithTimeout` and the dispatcher take it too; `Start-PkProcess` doesn't, since it returns before there's an exit to act on. It goes through the program's own Job Object, so for a program without one (`StartMode` `NoJob`, or Windows refused it) it can't kill anything; it warns once per session instead. `StartMode` `CreatedInJob` describes how programs start, not whether each one got its own Job Object. In `AddedToJob` mode it can miss a child the program started straight away. +`-KillTreeOnExit` kills whatever a program started that's still running when the program itself exits, whether or not it holds the program's output open. It's off by default, since some tools leave a build server or a daemon running on purpose. A command's own `KillTreeOnExit` key, `$true` or `$false`, overrides it either way. `Invoke-PkProcessWithTimeout` and the dispatcher take it too; `Start-PkProcess` doesn't, since it returns before there's an exit to act on. It goes through the program's own Job Object, so for a program without one (`StartMode` `NoJob`, or Windows refused it) it can't kill anything; it warns once per session instead. If Windows won't end a program's Job Object, what it started may still be running, and that warns once per session too. `StartMode` `CreatedInJob` describes how programs start, not whether each one got its own Job Object. In `AddedToJob` mode it can miss a child the program started straight away. diff --git a/Source/PkChildProcessJob.cs b/Source/PkChildProcessJob.cs index 4150445..42ae14a 100644 --- a/Source/PkChildProcessJob.cs +++ b/Source/PkChildProcessJob.cs @@ -265,9 +265,11 @@ private sealed class TreeJob : SafeHandleZeroOrMinusOneIsInvalid // 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. + // as refused, to test its retry without one. TestFailTerminate makes + // KillTree's TerminateJobObject count as refused, leaving the tree running. public static bool TestNoTreeJob = false; public static bool TestRefuseTreeJob = false; + public static bool TestFailTerminate = false; private static void AddTree(Process process, IntPtr treeJob) { @@ -286,7 +288,7 @@ public static bool KillTree(Process process) try { j.DangerousAddRef(ref added); - ended = TerminateJobObject(j.DangerousGetHandle(), 1); + ended = !TestFailTerminate && TerminateJobObject(j.DangerousGetHandle(), 1); } catch (ObjectDisposedException) { return false; } // released meanwhile: taskkill instead finally { if (added) { j.DangerousRelease(); } } @@ -621,12 +623,14 @@ public static string StartMode public static void TestResetFallbackWarning() { Interlocked.Exchange(ref _fallbackWarned, 0); } // The same, once each, for a Job Object that couldn't be set up when // first needed, for a running program Windows wouldn't add to it, and for - // -KillTreeOnExit on a program without a tree job. - private static int _initWarned = 0, _assignWarned = 0, _noTreeWarned = 0; + // -KillTreeOnExit on a program without a tree job, or whose tree job + // Windows wouldn't end. + private static int _initWarned = 0, _assignWarned = 0, _noTreeWarned = 0, _killTreeWarned = 0; public static bool ClaimInitWarning() { return Interlocked.Exchange(ref _initWarned, 1) == 0; } public static bool ClaimAssignWarning() { return Interlocked.Exchange(ref _assignWarned, 1) == 0; } public static bool ClaimNoTreeWarning() { return Interlocked.Exchange(ref _noTreeWarned, 1) == 0; } - public static void TestResetJobWarnings() { Interlocked.Exchange(ref _initWarned, 0); Interlocked.Exchange(ref _assignWarned, 0); Interlocked.Exchange(ref _noTreeWarned, 0); } + public static bool ClaimKillTreeWarning() { return Interlocked.Exchange(ref _killTreeWarned, 1) == 0; } + public static void TestResetJobWarnings() { Interlocked.Exchange(ref _initWarned, 0); Interlocked.Exchange(ref _assignWarned, 0); Interlocked.Exchange(ref _noTreeWarned, 0); Interlocked.Exchange(ref _killTreeWarned, 0); } private static StringBuilder BuildCommandLine(string fileName, string arguments) {