fix(site-runtime): watchdog follows the run across awaits; only the run's own deadline is a timeout (SRT-1, SRT-10, WP3 task 3.6)

This commit is contained in:
Joseph Doherty
2026-12-03 10:06:01 -05:00
parent c000ee3203
commit a730bc0558
4 changed files with 193 additions and 19 deletions
@@ -127,6 +127,67 @@ public class StuckScriptWatchdogTests : TestKit, IDisposable
Assert.Equal(1, scheduler.MaximumConcurrencyLevel);
}
/// <summary>
/// SRT-1: a script that AWAITS before it wedges. Its continuation runs on whichever worker
/// dequeues it, under a new run stamp, so the (slot, stamp) captured in the first synchronous
/// segment no longer matches; pre-fix the watchdog answered NotRunning and never detached the
/// worker the continuation was actually blocking. Nearly every real script awaits an attribute
/// read before doing its I/O.
/// </summary>
[Fact]
public void ScriptWedgedAfterAnAwait_StillDetachesTheWorkerItIsBlocking()
{
using var scheduler = new ScriptExecutionScheduler(1);
var siteLog = new FakeSiteEventLogger();
var options = WatchdogOptions();
var wedged = BuildScriptActor("AwaitThenWedge", CompileRaw(
"await Task.Delay(20); " +
"ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests.Actors.WatchdogHooks.Gate.Wait(); return null;"),
options, scheduler, new SingleServiceProvider(siteLog));
wedged.Tell(new ScriptCallRequest("AwaitThenWedge", null, 0, "corr-await"), ActorRefs.NoSender);
AwaitAssert(
() => Assert.Equal(1, scheduler.DetachedThreadCount), // pre-fix: stays 0
TimeSpan.FromSeconds(15));
AwaitAssert(
() => Assert.Contains(siteLog.OfType("script"), r =>
r.Message.Contains("AwaitThenWedge") && r.Message.Contains("DETACHED")),
TimeSpan.FromSeconds(5));
// The replacement worker serves new work while the continuation stays wedged.
var healthy = BuildScriptActor("HealthyAfterAwait", CompileRaw("return 3;"), options, scheduler, null);
var caller = CreateTestProbe();
healthy.Tell(new ScriptCallRequest("HealthyAfterAwait", null, 0, "corr-h"), caller.Ref);
Assert.True(caller.ExpectMsg<ScriptCallResult>(TimeSpan.FromSeconds(15)).Success);
WatchdogHooks.Gate.Release();
AwaitAssert(() => Assert.Equal(0, scheduler.DetachedThreadCount), TimeSpan.FromSeconds(15));
}
/// <summary>
/// SRT-10: a cancellation that is NOT the run's own deadline (an HTTP client's internal
/// timeout, a script's own token) is a failure with its real message — not "timed out after
/// N s" for a run that never reached its timeout.
/// </summary>
[Fact]
public void ForeignCancellation_IsReportedAsAFailure_NotATimeout()
{
using var scheduler = new ScriptExecutionScheduler(1);
var options = new SiteRuntimeOptions { ScriptExecutionTimeoutSeconds = 60, StuckScriptGraceMs = 200, MaxScriptCallDepth = 10 };
var actor = BuildScriptActor("OwnCancel", CompileRaw(
"throw new System.OperationCanceledException(\"upstream request was cancelled\");"),
options, scheduler, null);
var caller = CreateTestProbe();
actor.Tell(new ScriptCallRequest("OwnCancel", null, 0, "corr-cancel"), caller.Ref);
var result = caller.ExpectMsg<ScriptCallResult>(TimeSpan.FromSeconds(15));
Assert.False(result.Success);
Assert.DoesNotContain("timed out", result.ErrorMessage); // pre-fix: "timed out after 60s"
Assert.Contains("upstream request was cancelled", result.ErrorMessage);
}
/// <summary>Verifies that at the detach cap no further replacement is started and an error event is emitted.</summary>
[Fact]
public void AtTheDetachCap_NoFurtherReplacementIsStarted_AndAnErrorEventIsEmitted()