Skip to content

Commit 54f23d2

Browse files
committed
test(mxc): stabilize bounded cleanup coverage
Keep lossless output draining on normal completion while bounding only the cancellation path. Synchronize cancellation after launcher startup and assert the semantic cleanup contract with a scheduler-tolerant ceiling. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 27dc9d2f-ee94-48f1-bf06-8f3d10414fc9
1 parent 2791b48 commit 54f23d2

2 files changed

Lines changed: 50 additions & 12 deletions

File tree

‎src/OpenClaw.Shared/Mxc/MxcExecutor.cs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ private async Task<MxcResult> RunWithArgumentsAsync(
191191
try { _processTreeKiller(process); }
192192
catch (Exception ex) { Trace.WriteLine($"MxcExecutor: process kill (cancellation path) failed: {ex.Message}"); }
193193
if (!await WaitForCleanupAsync(processExited.Task, stdoutClosed.Task, stderrClosed.Task, _cleanupTimeout))
194-
Trace.WriteLine("MxcExecutor: cancellation cleanup timed out.");
194+
Trace.WriteLine($"MxcExecutor: cancellation cleanup timed out (pid={process.Id}).");
195195

196196
sw.Stop();
197197
string capturedOut;
@@ -207,8 +207,12 @@ private async Task<MxcResult> RunWithArgumentsAsync(
207207
};
208208
}
209209

210-
if (!await WaitForCleanupAsync(processExited.Task, stdoutClosed.Task, stderrClosed.Task, _cleanupTimeout))
211-
Trace.WriteLine("MxcExecutor: post-exit output drain timed out.");
210+
// Normal completion keeps the established lossless drain behavior.
211+
// The bounded cleanup exists specifically for cancellation, where
212+
// availability takes precedence over waiting indefinitely for a
213+
// launcher or inherited pipe handle that ignored termination.
214+
try { process.WaitForExit(); }
215+
catch (Exception ex) { Trace.WriteLine($"MxcExecutor: post-exit drain failed: {ex.Message}"); }
212216

213217
sw.Stop();
214218
string outRaw, errRaw;

‎tests/OpenClaw.Shared.Tests/Mxc/MxcExecutorTests.cs‎

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,38 +46,49 @@ public async Task RunAsync_CancellationCleanupIsBounded_WhenProcessTreeKillDoesN
4646
return;
4747

4848
var launcherPid = 0;
49+
var killAttempted = false;
50+
Process? launcher = null;
4951
var cmdPath = Path.Combine(
5052
Environment.GetFolderPath(Environment.SpecialFolder.System),
5153
"cmd.exe");
5254
var executor = new MxcExecutor(
5355
cmdPath,
5456
stdoutCapBytes: null,
5557
stderrCapBytes: null,
56-
processFactory: _ => CreateProcess(
58+
processFactory: _ => launcher = CreateProcess(
5759
cmdPath,
5860
"echo launcher-started & ping -n 31 127.0.0.1 >nul"),
59-
processTreeKiller: process => launcherPid = process.Id,
61+
processTreeKiller: process =>
62+
{
63+
killAttempted = true;
64+
launcherPid = process.Id;
65+
},
6066
cleanupTimeout: TimeSpan.FromMilliseconds(100));
61-
using var cancellation = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
62-
var stopwatch = Stopwatch.StartNew();
67+
using var cancellation = new CancellationTokenSource();
6368

6469
try
6570
{
66-
var result = await executor.RunAsync(
71+
var run = executor.RunAsync(
6772
new MxcConfig
6873
{
6974
ContainerId = "bounded-cleanup-test",
7075
Process = new MxcProcess { CommandLine = "ignored-by-test-process" },
7176
},
72-
cancellation.Token).WaitAsync(TimeSpan.FromSeconds(2));
73-
77+
cancellation.Token);
78+
await WaitForProcessStartAsync(() => launcher, TimeSpan.FromSeconds(5));
79+
launcherPid = launcher!.Id;
80+
var stopwatch = Stopwatch.StartNew();
81+
cancellation.Cancel();
82+
var result = await run.WaitAsync(TimeSpan.FromSeconds(5));
7483
stopwatch.Stop();
84+
85+
Assert.True(killAttempted);
7586
Assert.True(result.TimedOut);
7687
Assert.Equal(-1, result.ExitCode);
7788
Assert.Contains("cancelled", result.Error, StringComparison.OrdinalIgnoreCase);
7889
Assert.True(
79-
stopwatch.Elapsed < TimeSpan.FromSeconds(1),
80-
$"Cancellation cleanup took {stopwatch.ElapsedMilliseconds} ms.");
90+
stopwatch.Elapsed < TimeSpan.FromSeconds(2),
91+
$"Post-cancel cleanup took {stopwatch.ElapsedMilliseconds} ms.");
8192
}
8293
finally
8394
{
@@ -99,6 +110,29 @@ public async Task WaitForCleanupAsync_ReturnsTrue_WhenProcessAndPipesComplete()
99110
Assert.True(result);
100111
}
101112

113+
private static async Task WaitForProcessStartAsync(
114+
Func<Process?> processProvider,
115+
TimeSpan timeout)
116+
{
117+
var deadline = DateTime.UtcNow + timeout;
118+
while (DateTime.UtcNow < deadline)
119+
{
120+
try
121+
{
122+
if (processProvider()?.Id > 0)
123+
return;
124+
}
125+
catch (InvalidOperationException)
126+
{
127+
// Process.Start has not completed yet.
128+
}
129+
130+
await Task.Delay(10);
131+
}
132+
133+
throw new TimeoutException("Test launcher did not start within the expected time.");
134+
}
135+
102136
private static Process CreateProcess(string cmdPath, string command)
103137
{
104138
var startInfo = new ProcessStartInfo(cmdPath)

0 commit comments

Comments
 (0)