|
| 1 | +using System.Diagnostics; |
| 2 | +using PolyPilot.Services; |
| 3 | + |
| 4 | +namespace PolyPilot.Tests; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Tests for ProcessHelper — safe wrappers for Process lifecycle operations |
| 8 | +/// that prevent InvalidOperationException / UnobservedTaskException crashes |
| 9 | +/// when a process is disposed while background tasks are still monitoring it. |
| 10 | +/// </summary> |
| 11 | +public class ProcessHelperTests |
| 12 | +{ |
| 13 | + // ===== SafeHasExited ===== |
| 14 | + |
| 15 | + [Fact] |
| 16 | + public void SafeHasExited_NullProcess_ReturnsTrue() |
| 17 | + { |
| 18 | + Assert.True(ProcessHelper.SafeHasExited(null)); |
| 19 | + } |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public void SafeHasExited_DisposedProcess_ReturnsTrue() |
| 23 | + { |
| 24 | + // Start a short-lived process and dispose it immediately |
| 25 | + var psi = new ProcessStartInfo |
| 26 | + { |
| 27 | + FileName = OperatingSystem.IsWindows() ? "cmd.exe" : "/bin/sh", |
| 28 | + Arguments = OperatingSystem.IsWindows() ? "/c echo test" : "-c \"echo test\"", |
| 29 | + UseShellExecute = false, |
| 30 | + RedirectStandardOutput = true, |
| 31 | + CreateNoWindow = true |
| 32 | + }; |
| 33 | + var process = Process.Start(psi)!; |
| 34 | + process.WaitForExit(5000); |
| 35 | + process.Dispose(); |
| 36 | + |
| 37 | + // After disposal, HasExited would throw InvalidOperationException. |
| 38 | + // SafeHasExited must return true instead. |
| 39 | + Assert.True(ProcessHelper.SafeHasExited(process)); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void SafeHasExited_ExitedProcess_ReturnsTrue() |
| 44 | + { |
| 45 | + var psi = new ProcessStartInfo |
| 46 | + { |
| 47 | + FileName = OperatingSystem.IsWindows() ? "cmd.exe" : "/bin/sh", |
| 48 | + Arguments = OperatingSystem.IsWindows() ? "/c echo done" : "-c \"echo done\"", |
| 49 | + UseShellExecute = false, |
| 50 | + RedirectStandardOutput = true, |
| 51 | + CreateNoWindow = true |
| 52 | + }; |
| 53 | + var process = Process.Start(psi)!; |
| 54 | + process.WaitForExit(5000); |
| 55 | + |
| 56 | + Assert.True(ProcessHelper.SafeHasExited(process)); |
| 57 | + process.Dispose(); |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public void SafeHasExited_RunningProcess_ReturnsFalse() |
| 62 | + { |
| 63 | + // Start a long-running process |
| 64 | + var psi = new ProcessStartInfo |
| 65 | + { |
| 66 | + FileName = OperatingSystem.IsWindows() ? "cmd.exe" : "/bin/sh", |
| 67 | + Arguments = OperatingSystem.IsWindows() ? "/c ping -n 30 127.0.0.1 > nul" : "-c \"sleep 30\"", |
| 68 | + UseShellExecute = false, |
| 69 | + CreateNoWindow = true |
| 70 | + }; |
| 71 | + var process = Process.Start(psi)!; |
| 72 | + try |
| 73 | + { |
| 74 | + Assert.False(ProcessHelper.SafeHasExited(process)); |
| 75 | + } |
| 76 | + finally |
| 77 | + { |
| 78 | + try { process.Kill(true); } catch { } |
| 79 | + process.Dispose(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // ===== SafeKill ===== |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public void SafeKill_NullProcess_DoesNotThrow() |
| 87 | + { |
| 88 | + ProcessHelper.SafeKill(null); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public void SafeKill_DisposedProcess_DoesNotThrow() |
| 93 | + { |
| 94 | + var psi = new ProcessStartInfo |
| 95 | + { |
| 96 | + FileName = OperatingSystem.IsWindows() ? "cmd.exe" : "/bin/sh", |
| 97 | + Arguments = OperatingSystem.IsWindows() ? "/c echo test" : "-c \"echo test\"", |
| 98 | + UseShellExecute = false, |
| 99 | + CreateNoWindow = true |
| 100 | + }; |
| 101 | + var process = Process.Start(psi)!; |
| 102 | + process.WaitForExit(5000); |
| 103 | + process.Dispose(); |
| 104 | + |
| 105 | + // Must not throw |
| 106 | + ProcessHelper.SafeKill(process); |
| 107 | + } |
| 108 | + |
| 109 | + [Fact] |
| 110 | + public void SafeKill_RunningProcess_KillsIt() |
| 111 | + { |
| 112 | + var psi = new ProcessStartInfo |
| 113 | + { |
| 114 | + FileName = OperatingSystem.IsWindows() ? "cmd.exe" : "/bin/sh", |
| 115 | + Arguments = OperatingSystem.IsWindows() ? "/c ping -n 30 127.0.0.1 > nul" : "-c \"sleep 30\"", |
| 116 | + UseShellExecute = false, |
| 117 | + CreateNoWindow = true |
| 118 | + }; |
| 119 | + var process = Process.Start(psi)!; |
| 120 | + |
| 121 | + ProcessHelper.SafeKill(process); |
| 122 | + process.WaitForExit(5000); |
| 123 | + Assert.True(process.HasExited); |
| 124 | + process.Dispose(); |
| 125 | + } |
| 126 | + |
| 127 | + // ===== SafeKillAndDispose ===== |
| 128 | + |
| 129 | + [Fact] |
| 130 | + public void SafeKillAndDispose_NullProcess_DoesNotThrow() |
| 131 | + { |
| 132 | + ProcessHelper.SafeKillAndDispose(null); |
| 133 | + } |
| 134 | + |
| 135 | + [Fact] |
| 136 | + public void SafeKillAndDispose_AlreadyDisposed_DoesNotThrow() |
| 137 | + { |
| 138 | + var psi = new ProcessStartInfo |
| 139 | + { |
| 140 | + FileName = OperatingSystem.IsWindows() ? "cmd.exe" : "/bin/sh", |
| 141 | + Arguments = OperatingSystem.IsWindows() ? "/c echo test" : "-c \"echo test\"", |
| 142 | + UseShellExecute = false, |
| 143 | + CreateNoWindow = true |
| 144 | + }; |
| 145 | + var process = Process.Start(psi)!; |
| 146 | + process.WaitForExit(5000); |
| 147 | + process.Dispose(); |
| 148 | + |
| 149 | + // Calling SafeKillAndDispose on already-disposed process must not throw |
| 150 | + ProcessHelper.SafeKillAndDispose(process); |
| 151 | + } |
| 152 | + |
| 153 | + [Fact] |
| 154 | + public void SafeKillAndDispose_RunningProcess_KillsAndDisposes() |
| 155 | + { |
| 156 | + var psi = new ProcessStartInfo |
| 157 | + { |
| 158 | + FileName = OperatingSystem.IsWindows() ? "cmd.exe" : "/bin/sh", |
| 159 | + Arguments = OperatingSystem.IsWindows() ? "/c ping -n 30 127.0.0.1 > nul" : "-c \"sleep 30\"", |
| 160 | + UseShellExecute = false, |
| 161 | + CreateNoWindow = true |
| 162 | + }; |
| 163 | + var process = Process.Start(psi)!; |
| 164 | + var pid = process.Id; |
| 165 | + |
| 166 | + ProcessHelper.SafeKillAndDispose(process); |
| 167 | + |
| 168 | + // Verify the process is no longer running |
| 169 | + try |
| 170 | + { |
| 171 | + var check = Process.GetProcessById(pid); |
| 172 | + // Process might still be there for a moment — give it time |
| 173 | + check.WaitForExit(2000); |
| 174 | + } |
| 175 | + catch (ArgumentException) |
| 176 | + { |
| 177 | + // Process already gone — expected |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + // ===== Race condition regression test ===== |
| 182 | + |
| 183 | + [Fact] |
| 184 | + public void SafeHasExited_ConcurrentDispose_NoUnobservedTaskException() |
| 185 | + { |
| 186 | + // Regression test: simulates the race condition where a background task |
| 187 | + // checks HasExited while another thread disposes the process. |
| 188 | + using var unobservedSignal = new ManualResetEventSlim(false); |
| 189 | + Exception? unobservedException = null; |
| 190 | + EventHandler<UnobservedTaskExceptionEventArgs> handler = (sender, args) => |
| 191 | + { |
| 192 | + if (args.Exception?.InnerException is InvalidOperationException) |
| 193 | + { |
| 194 | + unobservedException = args.Exception; |
| 195 | + unobservedSignal.Set(); |
| 196 | + } |
| 197 | + }; |
| 198 | + |
| 199 | + TaskScheduler.UnobservedTaskException += handler; |
| 200 | + try |
| 201 | + { |
| 202 | + for (int i = 0; i < 5; i++) |
| 203 | + { |
| 204 | + var psi = new ProcessStartInfo |
| 205 | + { |
| 206 | + FileName = OperatingSystem.IsWindows() ? "cmd.exe" : "/bin/sh", |
| 207 | + Arguments = OperatingSystem.IsWindows() ? "/c ping -n 10 127.0.0.1 > nul" : "-c \"sleep 10\"", |
| 208 | + UseShellExecute = false, |
| 209 | + CreateNoWindow = true |
| 210 | + }; |
| 211 | + var process = Process.Start(psi)!; |
| 212 | + |
| 213 | + // Background task monitoring HasExited (like DevTunnel's fire-and-forget tasks) |
| 214 | + _ = Task.Run(() => |
| 215 | + { |
| 216 | + for (int j = 0; j < 50; j++) |
| 217 | + { |
| 218 | + if (ProcessHelper.SafeHasExited(process)) |
| 219 | + break; |
| 220 | + Thread.Sleep(10); |
| 221 | + } |
| 222 | + }); |
| 223 | + |
| 224 | + // Simulate concurrent disposal (like Stop() being called) |
| 225 | + Thread.Sleep(50); |
| 226 | + ProcessHelper.SafeKillAndDispose(process); |
| 227 | + } |
| 228 | + |
| 229 | + // Force GC to surface any unobserved task exceptions |
| 230 | + GC.Collect(); |
| 231 | + GC.WaitForPendingFinalizers(); |
| 232 | + GC.Collect(); |
| 233 | + |
| 234 | + unobservedSignal.Wait(TimeSpan.FromMilliseconds(500)); |
| 235 | + Assert.Null(unobservedException); |
| 236 | + } |
| 237 | + finally |
| 238 | + { |
| 239 | + TaskScheduler.UnobservedTaskException -= handler; |
| 240 | + } |
| 241 | + } |
| 242 | +} |
0 commit comments