test(script-worker): #1092 the two "slow caller does not starve the reader" tests use a 1.5 s silence limit (3.5 s block) — measured worker-side whole-process stalls, not heartbeat starvation

SupervisionTests.A_caller_that_blocks_after_its_result_does_not_starve_the_reader and its
WorkerSoftClockTests sibling killed a healthy worker for "sent nothing for ~344 ms" at a
300 ms limit (3 x 100 ms) on a loaded macOS box. Investigated before loosening, with
send-stamped heartbeats (temporary instrumentation, not committed):
- the late beats were late at the WORKER (290-380 ms between sends, 1-150 ms transit), mid
  run, not during startup;
- a cold worker beat every 100 +/- 8 ms from its first beat in 50 isolated runs at load
  60-200, with and without 28 CPU burners, and in full-suite runs under 40 burners, with the
  heartbeat on the thread pool or on a dedicated thread alike (A/B) — no JIT / Roslyn /
  pool-injection starvation to fix;
- the stalls appeared only in a full-suite run under the real load of other agents, with the
  box 24 of 25.6 GB into swap: whole-process stalls no worker change removes.
So the tests take a 1.5 s limit (production: 3 s) and block the caller 3.5 s. Mutation still
caught: with callers resumed inline on the reader both fail (the Supervision test by the
heartbeat kill itself). 20/20 runs of the pair pass.
This commit is contained in:
Joseph Doherty
2026-12-04 15:40:40 -05:00
parent 61546d5aab
commit cb5250face
2 changed files with 33 additions and 7 deletions
@@ -546,11 +546,23 @@ public sealed class SupervisionTests
/// <summary>
/// lmxopcua#789: callers resume on the connection's result dispatcher, never on the pipe reader — so a consumer
/// that blocks after its await (the Calculation pump publishes to OPC UA subscribers there) cannot stop the reader
/// from taking heartbeats and get a healthy worker killed for silence. Here a 100 ms heartbeat (kill after 300 ms
/// of silence) and a continuation that blocks for 1.5 s. Mutation check: resume callers inline on the reader (a
/// from taking heartbeats and get a healthy worker killed for silence. Here a 100 ms heartbeat (kill after 1.5 s
/// of silence) and a continuation that blocks for 3.5 s. Mutation check: resume callers inline on the reader (a
/// synchronous-continuation TaskCompletionSource completed by the reader, no dispatcher) and the worker is killed
/// (KilledHeartbeat).
/// </summary>
/// <remarks>
/// lmxopcua#1092: the limit used to be three 100 ms intervals (300 ms), and this pair of tests failed
/// intermittently on a loaded macOS dev box (about 1 full-suite run in 3–5), the worker killed for "sent nothing
/// for 344 ms" while the reader and the dispatcher worked correctly. Measured with send-stamped heartbeats: the
/// WORKER sent a beat 290–380 ms late (1–150 ms in transit), mid-run, not at startup — a cold worker beat every
/// 100 ± 8 ms from its first beat in 50 isolated runs at load 60–200, with and without 28 CPU burners, and no
/// run outside that real-load full suite (24 of 25.6 GB into swap at the time) stalled at all, whether the
/// heartbeat ran on the thread pool or on a dedicated thread. Those are whole-process stalls of the box, not
/// starvation a worker change can remove. So the limit is 1.5 s (production's is 3 s) and the block 3.5 s: a
/// reader held by the caller still goes silent 2 s past the limit, so the mutation is still killed, and the
/// resumedOn assertion pins the thread outright.
/// </remarks>
[Fact]
public async Task A_caller_that_blocks_after_its_result_does_not_starve_the_reader()
{
@@ -558,6 +570,7 @@ public sealed class SupervisionTests
var heartbeat = TimeSpan.FromMilliseconds(100);
await using var runner = TightHeartbeatRunner(heartbeat, TimeSpan.FromSeconds(60), sink);
var worker = WarmTight(runner, await runner.RunAsync(Scripts.Vt("warm", Healthy), Ct), heartbeat, sink);
worker.SilenceLimit = TimeSpan.FromMilliseconds(1_500); // see remarks (#1092)
string? resumedOn = null;
await runner.RunAsync(Scripts.Vt("warm", Healthy), Ct).ContinueWith(t =>
@@ -565,7 +578,7 @@ public sealed class SupervisionTests
// Runs wherever RunAsync's own continuation completed it: the thread that resumed the caller.
resumedOn = Thread.CurrentThread.Name;
t.Result.Value.ShouldBe(7);
Thread.Sleep(1_500);
Thread.Sleep(3_500);
}, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
// Behaviour first: the worker was not killed for silence while the caller blocked.
@@ -271,9 +271,22 @@ public sealed class WorkerSoftClockTests
/// <summary>
/// Review F1: the soft notice is handled on the pipe READER, and the caller's resumption is the caller's code. It
/// must go through the result dispatcher like a result does: a continuation that blocks for 1.5 s must not hold up
/// the reader, or the next heartbeats sit unread and a healthy worker is killed for silence (300 ms here). Before
/// the fix the continuation ran inline on the reader and the worker was killed (KilledHeartbeat).
/// the reader, or the next heartbeats sit unread and a healthy worker is killed for silence (1.5 s here, with a
/// 3.5 s block). Before the fix the continuation ran inline on the reader and the worker was killed (KilledHeartbeat).
/// </summary>
/// <remarks>
/// Same limit as <c>SupervisionTests.A_caller_that_blocks_after_its_result_does_not_starve_the_reader</c>:
/// lmxopcua#1092: the limit used to be three 100 ms intervals (300 ms), and this pair of tests failed
/// intermittently on a loaded macOS dev box (about 1 full-suite run in 3–5), the worker killed for "sent nothing
/// for 344 ms" while the reader and the dispatcher worked correctly. Measured with send-stamped heartbeats: the
/// WORKER sent a beat 290–380 ms late (1–150 ms in transit), mid-run, not at startup — a cold worker beat every
/// 100 ± 8 ms from its first beat in 50 isolated runs at load 60–200, with and without 28 CPU burners, and no
/// run outside that real-load full suite (24 of 25.6 GB into swap at the time) stalled at all, whether the
/// heartbeat ran on the thread pool or on a dedicated thread. Those are whole-process stalls of the box, not
/// starvation a worker change can remove. So the limit is 1.5 s (production's is 3 s) and the block 3.5 s: a
/// reader held by the caller still goes silent 2 s past the limit, so the mutation is still killed, and the
/// resumedOn assertion pins the thread outright.
/// </remarks>
[Fact]
public async Task A_slow_continuation_of_a_soft_timeout_does_not_stall_the_reader()
{
@@ -286,7 +299,7 @@ public sealed class WorkerSoftClockTests
var entry = await connection.EnsureLoadedAsync("k", ContextKind.VirtualTag, Runaway,
SHA256.HashData(Encoding.UTF8.GetBytes(Runaway)), []).WaitAsync(TimeSpan.FromSeconds(60), Ct);
entry.Outcome.ShouldNotBeNull().Ok.ShouldBeTrue();
connection.SilenceLimit = heartbeat * 3; // armed only once the cold start is over (see SupervisionTests)
connection.SilenceLimit = TimeSpan.FromMilliseconds(1_500); // armed once the cold start is over; see remarks (#1092)
var softT = TimeSpan.FromMilliseconds(200);
var pending = connection.Submit("k",
@@ -297,7 +310,7 @@ public sealed class WorkerSoftClockTests
{
resumedOn = Thread.CurrentThread.Name;
t.Result.ShouldBeSameAs(PendingEval.SoftTimedOut);
Thread.Sleep(1_500);
Thread.Sleep(3_500);
}, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
await slowConsumer.WaitAsync(TimeSpan.FromSeconds(10), Ct);