Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 53 additions & 9 deletions src/GeneralUpdate.Bowl/Internal/WindowsSystemInfoProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ namespace GeneralUpdate.Bowl.Internal;
/// </summary>
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(
Expand All @@ -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.");
}
}
2 changes: 1 addition & 1 deletion tests/BowlTest/Internal/SystemInfoProviderFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Create_多次调用_每次返回新实例()
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);
Expand Down
Loading