-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathProcessRunner.cs
More file actions
99 lines (85 loc) · 3.78 KB
/
Copy pathProcessRunner.cs
File metadata and controls
99 lines (85 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using GeneralUpdate.Bowl;
namespace GeneralUpdate.Bowl.Strategies;
/// <summary>
/// Async wrapper for running a child process with stdout/stderr capture and a timeout.
/// </summary>
internal static class ProcessRunner
{
/// <summary>
/// Starts the process described by <paramref name="startInfo"/>, captures output lines,
/// and waits for exit or timeout.
/// </summary>
/// <param name="startInfo">Process start configuration.</param>
/// <param name="timeoutMs">Maximum wait time in milliseconds.</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>Exit code and captured output lines.</returns>
public static async Task<ProcessExitResult> RunAsync(
ProcessStartInfo startInfo, int timeoutMs, CancellationToken ct = default)
{
GeneralTracer.Info($"ProcessRunner.RunAsync: starting '{startInfo.FileName} {startInfo.Arguments}'");
var outputLines = new List<string>();
using var process = new Process { StartInfo = startInfo };
using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(ct);
var tcs = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
process.OutputDataReceived += (_, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
GeneralTracer.Debug($"ProcessRunner: {e.Data}");
lock (outputLines) outputLines.Add(e.Data);
}
};
process.ErrorDataReceived += (_, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
GeneralTracer.Debug($"ProcessRunner(stderr): {e.Data}");
lock (outputLines) outputLines.Add(e.Data);
}
};
process.EnableRaisingEvents = true;
process.Exited += (_, _) => tcs.TrySetResult(process.ExitCode);
if (!process.Start())
{
GeneralTracer.Fatal("ProcessRunner.RunAsync: failed to start process.");
throw new InvalidOperationException("Failed to start process.");
}
GeneralTracer.Info($"ProcessRunner.RunAsync: process started, PID={process.Id}");
process.BeginOutputReadLine();
process.BeginErrorReadLine();
// Wait for exit or timeout/cancellation.
// Cancel the delay when the process exits first to avoid a timer leak.
var delayTask = Task.Delay(timeoutMs, timeoutCts.Token);
var completedTask = await Task.WhenAny(tcs.Task, delayTask);
// Cancel the opposing task so timers/resources are reclaimed promptly.
timeoutCts.Cancel();
if (completedTask == tcs.Task)
{
try { await delayTask; } catch (OperationCanceledException) { /* cancelled — expected */ }
var exitCode = await tcs.Task;
GeneralTracer.Info($"ProcessRunner.RunAsync: process exited, ExitCode={exitCode}");
// Snapshot output under lock to avoid race with in-flight handlers
IReadOnlyList<string> snapshot;
lock (outputLines) { snapshot = outputLines.ToArray(); }
return new ProcessExitResult { ExitCode = exitCode, OutputLines = snapshot };
}
// Timeout or cancellation — kill the process
GeneralTracer.Warn($"ProcessRunner.RunAsync: process timed out after {timeoutMs}ms, killing.");
try
{
process.Kill();
}
catch (Exception ex)
{
GeneralTracer.Error("ProcessRunner.RunAsync: error killing process.", ex);
}
ct.ThrowIfCancellationRequested();
throw new TimeoutException(
$"Process '{startInfo.FileName}' did not exit within {timeoutMs}ms.");
}
}