// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: MIT using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Globalization; namespace Progmasoft.Tazel.Driver; /// /// Stops a process Tazel started and the processes it started in turn, and nothing else. /// /// /// On Windows this is with the whole tree. Elsewhere Tazel finds the descendants /// itself, from one listing of every process and its parent made before the first signal, and signals only those: /// on macOS the whole-tree kill of .NET has ended the processes of the CI runner that ran Tazel's tests. /// internal static class ProcessTree { /// Stops and its descendants; one that has already ended is left alone. public static void Kill(Process process) { ArgumentNullException.ThrowIfNull(process); if (OperatingSystem.IsWindows()) { KillQuietly(process, entireProcessTree: true); return; } int root; try { if (process.HasExited) { return; } root = process.Id; } catch (InvalidOperationException) { return; } var descendants = Descendants(root, Parents()); // The parent first, so that it starts nothing more; then what it had started. KillQuietly(process, entireProcessTree: false); foreach (var id in descendants) { try { using var child = Process.GetProcessById(id); KillQuietly(child, entireProcessTree: false); } catch (ArgumentException) { // It ended in the meantime. } } } /// /// The descendants of in , children before their children. The /// listing gives each process its parent; Tazel itself, its ancestors and the first process are never among them. /// internal static List Descendants(int root, IReadOnlyDictionary parents) { ArgumentNullException.ThrowIfNull(parents); var children = new Dictionary>(); foreach (var (id, parent) in parents) { if (!children.TryGetValue(parent, out var list)) { children[parent] = list = []; } list.Add(id); } var protectedIds = Protected(parents); var found = new List(); var seen = new HashSet { root }; var queue = new Queue([root]); while (queue.Count > 0) { if (!children.TryGetValue(queue.Dequeue(), out var list)) { continue; } foreach (var id in list) { if (id > 1 && !protectedIds.Contains(id) && seen.Add(id)) { found.Add(id); queue.Enqueue(id); } } } return found; } /// Tazel's own process and its ancestors, which a stop never reaches. private static HashSet Protected(IReadOnlyDictionary parents) { var ids = new HashSet(); var id = Environment.ProcessId; while (id > 1 && ids.Add(id) && parents.TryGetValue(id, out var parent)) { id = parent; } return ids; } /// Every process and its parent, from ps; empty when ps cannot be run. private static Dictionary Parents() { var parents = new Dictionary(); var start = new ProcessStartInfo("ps") { RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true, }; foreach (var argument in new[] { "-A", "-o", "pid=", "-o", "ppid=" }) { start.ArgumentList.Add(argument); } try { using var ps = Process.Start(start); if (ps is null) { return parents; } var text = ps.StandardOutput.ReadToEnd(); ps.WaitForExit(); foreach (var line in text.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) { var parts = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); if (parts.Length == 2 && int.TryParse(parts[0], NumberStyles.None, CultureInfo.InvariantCulture, out var id) && int.TryParse(parts[1], NumberStyles.None, CultureInfo.InvariantCulture, out var parent)) { parents[id] = parent; } } } catch (Win32Exception) { // No ps: only the process itself is stopped. } return parents; } private static void KillQuietly(Process process, bool entireProcessTree) { try { process.Kill(entireProcessTree); } catch (InvalidOperationException) { // It ended by itself. } catch (Win32Exception) { // It ended, or it is not Tazel's to stop. } } }