diff --git a/src/GeneralUpdate.Bowl/Internal/WindowsSystemInfoProvider.cs b/src/GeneralUpdate.Bowl/Internal/WindowsSystemInfoProvider.cs index e9136078..1b9052f7 100644 --- a/src/GeneralUpdate.Bowl/Internal/WindowsSystemInfoProvider.cs +++ b/src/GeneralUpdate.Bowl/Internal/WindowsSystemInfoProvider.cs @@ -12,8 +12,11 @@ namespace GeneralUpdate.Bowl.Internal; /// internal sealed class WindowsSystemInfoProvider : ISystemInfoProvider { + private const int ExportTimeoutMs = 30_000; + public Task ExportAsync(string outputDirectory, CancellationToken ct) { + ct.ThrowIfCancellationRequested(); GeneralTracer.Info($"WindowsSystemInfoProvider.ExportAsync: exporting to {outputDirectory}."); var appDir = Path.Combine( @@ -30,25 +33,66 @@ public Task ExportAsync(string outputDirectory, CancellationToken ct) using var process = Process.Start(new ProcessStartInfo { FileName = batPath, - Arguments = outputDirectory, + Arguments = $"\"{outputDirectory}\"", UseShellExecute = true, CreateNoWindow = true, - }); + }) ?? throw new InvalidOperationException("Failed to start export.bat."); - if (process != null) + GeneralTracer.Info("WindowsSystemInfoProvider: export.bat started."); + var stopwatch = Stopwatch.StartNew(); + while (!process.WaitForExit(250)) { - GeneralTracer.Info("WindowsSystemInfoProvider: export.bat started."); - var exited = process.WaitForExit(30_000); - if (exited) + if (ct.IsCancellationRequested) { - GeneralTracer.Info($"WindowsSystemInfoProvider: export.bat finished, exit code {process.ExitCode}."); + TerminateProcessTree(process); + ct.ThrowIfCancellationRequested(); } - else + + if (stopwatch.ElapsedMilliseconds >= ExportTimeoutMs) { - GeneralTracer.Warn("WindowsSystemInfoProvider: export.bat timed out after 30s."); + GeneralTracer.Warn($"WindowsSystemInfoProvider: export.bat timed out after {ExportTimeoutMs}ms; terminating its process tree."); + TerminateProcessTree(process); + break; } } + if (process.HasExited) + GeneralTracer.Info($"WindowsSystemInfoProvider: export.bat finished, exit code {process.ExitCode}."); return Task.CompletedTask; } + + private static void TerminateProcessTree(Process process) + { + if (process.HasExited) + return; + + var taskkillPath = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.System), "taskkill.exe"); + using var taskkill = Process.Start(new ProcessStartInfo + { + FileName = taskkillPath, + Arguments = $"/PID {process.Id} /T /F", + UseShellExecute = false, + CreateNoWindow = true + }); + if (taskkill == null) + throw new InvalidOperationException("Failed to start taskkill for the timed-out export process."); + + if (!taskkill.WaitForExit(10_000)) + { + try + { + taskkill.Kill(); + taskkill.WaitForExit(); + } + catch (Exception ex) + { + GeneralTracer.Error("WindowsSystemInfoProvider: failed to stop taskkill.", ex); + } + throw new TimeoutException("Timed out while terminating the system-info export process tree."); + } + + if (!process.WaitForExit(5_000)) + throw new TimeoutException($"System-info export process {process.Id} remained active after taskkill."); + } } diff --git a/tests/BowlTest/Internal/SystemInfoProviderFactoryTests.cs b/tests/BowlTest/Internal/SystemInfoProviderFactoryTests.cs index f2547cbd..f636cf53 100644 --- a/tests/BowlTest/Internal/SystemInfoProviderFactoryTests.cs +++ b/tests/BowlTest/Internal/SystemInfoProviderFactoryTests.cs @@ -40,7 +40,7 @@ public class SystemInfoProviderFactoryTests public async Task 返回的Provider_ExportAsync不抛出异常() { var provider = SystemInfoProviderFactory.Create(); - var tempDir = Path.Combine(Path.GetTempPath(), $"BowlTest_{Guid.NewGuid():N}"); + var tempDir = Path.Combine(Path.GetTempPath(), $"Bowl Test_{Guid.NewGuid():N}"); try { Directory.CreateDirectory(tempDir);