test(script-worker): #1104 size ContainmentTests' non-heartbeat latency bounds for ~1 s whole-process stalls

Same treatment #1100 gave the heartbeat bounds, each assertion keeping its meaning:
- Runaway soft answer (was softElapsed < T + 400 ms): now an ORDERING fact - the worker has not
  exited when the caller is answered (answered before the hard kill) - plus < T + G. Grace 500 ms ->
  2.5 s so a stall cannot push the soft notice past T + G ("script worker unresponsive").
- Kill timing: lower bound T + G - 50 ms unchanged (stall-proof); upper slack 1.5 s -> 2 s, still
  < 2 x G and well under the 10 s Exited wait.
- Host CPU window: 1.5 s window / 1 s CPU absolute -> 3 s window, CPU < half the MEASURED wall time
  (a spinning orphan burns ~wall; a stall stretches the window, never adds CPU).
- Pipe-close exit (was < 2 s): bound dropped; the meaning is the exit CODE (ExitPipeClosed is only the
  worker's own EOF exit), waits 5 s -> 10 s.

Stall check (temporary harness, not committed: SIGSTOP/SIGCONT of the test host + worker pids for
1.2 s every ~2.3 s): new bounds passed 3/3; the old file failed the runaway test 2/2 under the same
harness.
This commit is contained in:
Joseph Doherty
2026-12-04 20:31:25 -05:00
parent d81990bcbf
commit 79d080c4a9
@@ -52,12 +52,19 @@ public sealed class ContainmentTests
/// the caller gets the usual timeout at T, the worker is killed at T + G, the host burns no CPU on it, and the /// the caller gets the usual timeout at T, the worker is killed at T + G, the host burns no CPU on it, and the
/// sibling on the same shard keeps working. Mutation check: with the hard-deadline kill disabled the worker is /// sibling on the same shard keeps working. Mutation check: with the hard-deadline kill disabled the worker is
/// never killed and the Exited wait times out. /// never killed and the Exited wait times out.
/// <para>
/// lmxopcua#1104: every wall-clock bound here is sized so a ~1 s whole-process stall (the loaded-box failure
/// mode #1100 audited for the heartbeat bounds) cannot fail it, and the soft answer is checked as an ORDERING
/// fact — it arrives while the worker is still alive, i.e. before the hard kill — rather than as T + 400 ms.
/// The grace is therefore wide (2.5 s): the soft answer must beat T + G by more than a stall, and a stall that
/// delayed the worker's soft-timeout notice past T + G would turn it into "script worker unresponsive".
/// </para>
/// </summary> /// </summary>
[Fact] [Fact]
public async Task A_runaway_script_is_killed_at_its_hard_deadline_and_its_sibling_keeps_working() public async Task A_runaway_script_is_killed_at_its_hard_deadline_and_its_sibling_keeps_working()
{ {
var softT = TimeSpan.FromMilliseconds(400); var softT = TimeSpan.FromMilliseconds(400);
var grace = TimeSpan.FromMilliseconds(500); var grace = TimeSpan.FromMilliseconds(2500);
await using var runner = Scripts.Runner(Scripts.Options(hardGrace: grace, quarantineAfterKills: 2)); await using var runner = Scripts.Runner(Scripts.Options(hardGrace: grace, quarantineAfterKills: 2));
(await runner.RunAsync(Scripts.Vt("sibling", Healthy), Ct)).Value.ShouldBe(7); (await runner.RunAsync(Scripts.Vt("sibling", Healthy), Ct)).Value.ShouldBe(7);
@@ -70,23 +77,36 @@ public sealed class ContainmentTests
var sw = Stopwatch.StartNew(); var sw = Stopwatch.StartNew();
var outcome = await runner.RunAsync(Scripts.Vt("runaway", runaway, softT, Scripts.Input("spin", 1)), Ct); var outcome = await runner.RunAsync(Scripts.Vt("runaway", runaway, softT, Scripts.Input("spin", 1)), Ct);
var softElapsed = sw.Elapsed; var softElapsed = sw.Elapsed;
var killedBeforeAnswer = worker.Exited.IsCompleted;
outcome.Status.ShouldBe(ScriptRunStatus.Failed); outcome.Status.ShouldBe(ScriptRunStatus.Failed);
// The soft timeout, not the transport deadline ("script worker unresponsive") and not the kill's fallout.
outcome.Reason!.ShouldStartWith("script timed out after "); outcome.Reason!.ShouldStartWith("script timed out after ");
softElapsed.ShouldBeLessThan(softT + TimeSpan.FromMilliseconds(400), "the caller is answered at the soft deadline"); // Ordering, not a tight bound: the caller is answered at the soft deadline, before the hard kill. A defect that
// only answered the caller once the worker was killed would see Exited already completed here.
killedBeforeAnswer.ShouldBeFalse("the caller is answered at the soft deadline, before the hard kill");
softElapsed.ShouldBeLessThan(softT + grace, "the caller is answered before the hard deadline");
var exit = await worker.Exited.WaitAsync(TimeSpan.FromSeconds(10), Ct); var exit = await worker.Exited.WaitAsync(TimeSpan.FromSeconds(10), Ct);
var killElapsed = sw.Elapsed; var killElapsed = sw.Elapsed;
exit.Reason.ShouldBe(WorkerExitReason.KilledDeadline); exit.Reason.ShouldBe(WorkerExitReason.KilledDeadline);
exit.CulpritKey.ShouldBe(InProcessScriptRunner.ScopedKey(ScriptContextKind.VirtualTag, "runaway")); exit.CulpritKey.ShouldBe(InProcessScriptRunner.ScopedKey(ScriptContextKind.VirtualTag, "runaway"));
// Not before the hard deadline (a stall only makes the kill later, so this lower bound is stall-proof)…
killElapsed.ShouldBeGreaterThanOrEqualTo(softT + grace - TimeSpan.FromMilliseconds(50)); killElapsed.ShouldBeGreaterThanOrEqualTo(softT + grace - TimeSpan.FromMilliseconds(50));
// Design budget is T + G + 100 ms; the slack here is for a loaded CI box (process reap + exit notification). // …and AT it, not at some later backstop. Design budget is T + G + 100 ms; the slack covers process reap + exit
killElapsed.ShouldBeLessThan(softT + grace + TimeSpan.FromMilliseconds(1500)); // notification on a loaded box plus a ~1 s stall, and stays well under the 10 s Exited wait and under 2 × G, so a
// kill driven by a second deadline period (or never) still fails.
killElapsed.ShouldBeLessThan(softT + grace + TimeSpan.FromMilliseconds(2000));
// No orphan in the host: an in-process while(true) would burn one host core for the whole window. // No orphan in the host: an in-process while(true) would burn one host core for the whole window, i.e. CPU time
// ≈ the wall time it ran. Asserted relative to the MEASURED window (a stall that stretches the window cannot fail
// it, and gives a spinning orphan more CPU, not less), over a window long enough that ordinary host background
// work (GC, JIT, the runner's timers) stays far below half of it.
var cpuBefore = Process.GetCurrentProcess().TotalProcessorTime; var cpuBefore = Process.GetCurrentProcess().TotalProcessorTime;
await Task.Delay(TimeSpan.FromMilliseconds(1500), Ct); var window = Stopwatch.StartNew();
await Task.Delay(TimeSpan.FromMilliseconds(3000), Ct);
var cpuUsed = Process.GetCurrentProcess().TotalProcessorTime - cpuBefore; var cpuUsed = Process.GetCurrentProcess().TotalProcessorTime - cpuBefore;
cpuUsed.ShouldBeLessThan(TimeSpan.FromMilliseconds(1000), "the runaway's thread died with its worker"); var wall = window.Elapsed;
cpuUsed.ShouldBeLessThan(wall / 2, $"the runaway's thread died with its worker ({cpuUsed.TotalMilliseconds:F0} ms CPU in {wall.TotalMilliseconds:F0} ms)");
// The sibling recovers on a fresh worker. // The sibling recovers on a fresh worker.
(await runner.RunAsync(Scripts.Vt("sibling", Healthy), Ct)).Value.ShouldBe(7); (await runner.RunAsync(Scripts.Vt("sibling", Healthy), Ct)).Value.ShouldBe(7);
@@ -291,11 +311,13 @@ public sealed class ContainmentTests
var worker = runner.Shards[0].Current.ShouldNotBeNull(); var worker = runner.Shards[0].Current.ShouldNotBeNull();
using var process = Process.GetProcessById(worker.Pid); using var process = Process.GetProcessById(worker.Pid);
var sw = Stopwatch.StartNew();
worker.SeverPipeForTest(); worker.SeverPipeForTest();
await process.WaitForExitAsync(Ct).WaitAsync(TimeSpan.FromSeconds(5), Ct); // lmxopcua#1104: no tight wall-clock bound (it was 2 s, exposed to a ~1 s whole-process stall). What this test
sw.Elapsed.ShouldBeLessThan(TimeSpan.FromSeconds(2)); // means is carried by the exit CODE: ExitPipeClosed is only the worker's own EOF exit, so a worker that ignored
var exit = await worker.Exited.WaitAsync(TimeSpan.FromSeconds(5), Ct); // the EOF would either never exit (the wait times out) or be killed by the host (another code). The wait is
// generous for the same reason; nothing else ends the process in that window.
await process.WaitForExitAsync(Ct).WaitAsync(TimeSpan.FromSeconds(10), Ct);
var exit = await worker.Exited.WaitAsync(TimeSpan.FromSeconds(10), Ct);
exit.ExitCode.ShouldBe(ScriptWorkerEnvironment.ExitPipeClosed, "it noticed the EOF; the host did not have to kill it"); exit.ExitCode.ShouldBe(ScriptWorkerEnvironment.ExitPipeClosed, "it noticed the EOF; the host did not have to kill it");
} }
@@ -421,12 +443,12 @@ public sealed class ContainmentTests
public async Task Unsupported_input_and_result_types_are_explicit_failures() public async Task Unsupported_input_and_result_types_are_explicit_failures()
{ {
await using var runner = Scripts.Runner(Scripts.Options()); await using var runner = Scripts.Runner(Scripts.Options());
// The one result shape the materializer passes (the OPC UA publish path carries it as an array of Variants) but // An unpublishable RESULT is refused by ScriptResultMaterializer in both runners alike (lmxopcua#1100), so it is a
// the wire does not: a jagged array. Every other unpublishable result is refused by ScriptResultMaterializer in // "script threw: … cannot carry …" failure, not "unsupported type" — since lmxopcua#1104 a jagged array too (it
// both runners alike since lmxopcua#1100 ("script threw: … cannot carry …", see ParityTests). // was the one result shape the materializer passed and the wire could not carry; see ParityTests).
var result = await runner.RunAsync(Scripts.Vt("jagged", "return new[] { new[] { 1 } };"), Ct); var result = await runner.RunAsync(Scripts.Vt("jagged", "return new[] { new[] { 1 } };"), Ct);
result.Status.ShouldBe(ScriptRunStatus.Failed); result.Status.ShouldBe(ScriptRunStatus.Failed);
result.Reason!.ShouldStartWith("unsupported type: script returned a value of unsupported type System.Int32[][]"); result.Reason!.ShouldStartWith("script threw: The script returned a value of type 'Int32[][]', which an OPC UA node cannot carry: an array of arrays");
var pair = await runner.RunAsync(Scripts.Vt("pair", "return new System.Collections.Generic.KeyValuePair<int, int>(1, 2);"), Ct); var pair = await runner.RunAsync(Scripts.Vt("pair", "return new System.Collections.Generic.KeyValuePair<int, int>(1, 2);"), Ct);
pair.Status.ShouldBe(ScriptRunStatus.Failed); pair.Status.ShouldBe(ScriptRunStatus.Failed);