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
@@ -281,18 +281,16 @@ internal static class ScriptRunLauncher
var emitPerRunEvents = ScriptRunEventPolicy.ShouldEmitPerRun(effectiveOptions, instanceName, scriptName);
var runStopwatch = Stopwatch.StartNew();
// Identify the worker this body is holding, so the watchdog can detach and
// replace it if the body wedges. Read in the FIRST synchronous segment — a script
// that later hops threads on an await no longer holds this worker, and the run-stamp
// guard in TryDetachWorker will (correctly) refuse to detach it.
var workerSlot = ScriptExecutionScheduler.CurrentWorkerSlot;
var workerRunStamp = workerSlot is { } slot ? executionScheduler.CurrentRunStamp(slot) : 0L;
// Track the worker this body is holding — not just in this first segment but after
// every await (SRT-1): each continuation runs on whichever worker dequeues it, under a
// new run stamp, so the watchdog reads the LATEST (slot, stamp) when it fires.
var workerTracker = ScriptRunWorkerTracker.Begin(executionScheduler);
IDisposable? logScope = null;
var completed = 0;
ArmStuckScriptWatchdog(
cts, options, logger, siteEventLogger, executionScheduler,
workerSlot, workerRunStamp, timeout,
workerTracker, timeout,
() => Volatile.Read(ref completed) != 0,
$"Script '{scriptName}' on instance '{instanceName}'",
instanceName, $"ScriptActor:{scriptName}");
@@ -480,7 +478,11 @@ internal static class ScriptRunLauncher
// Notify the owning ScriptActor of completion (also releases its in-flight slot).
completionTarget.Tell(new ScriptActor.ScriptExecutionCompleted(scriptName, true, null));
}
catch (OperationCanceledException)
// Only the run's OWN deadline is a timeout (SRT-10). Any other cancellation — an HTTP
// client's internal timeout, a script's own CancellationTokenSource, a cancelled read —
// falls through to the general failure path below with its real exception and stack,
// instead of being reported as "timed out after N s" for a run that lasted 5 s.
catch (OperationCanceledException) when (cts.IsCancellationRequested)
{
healthCollector?.IncrementScriptError();
summaryRecorder?.RecordTimedOut(instanceName, scriptName, runStopwatch.ElapsedMilliseconds);
@@ -714,18 +716,16 @@ internal static class ScriptRunLauncher
var scriptName = $"AlarmActor:{alarmName}";
var runStopwatch = Stopwatch.StartNew();
// Identify the worker this body is holding, so the
// stuck-script watchdog can detach and replace it if the body wedges — same
// mechanism, same gauge, as the instance-script path. Read in the FIRST synchronous
// segment, before any await.
var workerSlot = ScriptExecutionScheduler.CurrentWorkerSlot;
var workerRunStamp = workerSlot is { } slot ? executionScheduler.CurrentRunStamp(slot) : 0L;
// Track the worker this body is holding, so the stuck-script watchdog can detach and
// replace it if the body wedges — same mechanism, same gauge, as the instance-script
// path, and likewise followed across awaits (SRT-1).
var workerTracker = ScriptRunWorkerTracker.Begin(executionScheduler);
IDisposable? logScope = null;
var completed = 0;
ArmStuckScriptWatchdog(
cts, options, logger, siteEventLogger, executionScheduler,
workerSlot, workerRunStamp, timeout,
workerTracker, timeout,
() => Volatile.Read(ref completed) != 0,
$"Alarm on-trigger script for '{alarmName}' on instance '{instanceName}'",
instanceName, scriptName);
@@ -802,7 +802,8 @@ internal static class ScriptRunLauncher
summaryRecorder?.RecordCompleted(instanceName, scriptName, runStopwatch.ElapsedMilliseconds);
completionTarget.Tell(new AlarmActor.AlarmExecutionCompleted(alarmName, true));
}
catch (OperationCanceledException)
// Only the run's own deadline is a timeout (SRT-10); see RunScriptAsync.
catch (OperationCanceledException) when (cts.IsCancellationRequested)
{
healthCollector?.IncrementScriptError();
summaryRecorder?.RecordTimedOut(instanceName, scriptName, runStopwatch.ElapsedMilliseconds);
@@ -854,8 +855,7 @@ internal static class ScriptRunLauncher
ILogger logger,
ISiteEventLogger? siteEventLogger,
ScriptExecutionScheduler scheduler,
int? workerSlot,
long workerRunStamp,
ScriptRunWorkerTracker workerTracker,
TimeSpan timeout,
Func<bool> isCompleted,
string subject,
@@ -871,6 +871,8 @@ internal static class ScriptRunLauncher
var stuckMsg = $"{subject} exceeded its {timeout.TotalSeconds:F0}s timeout and is STILL EXECUTING — " +
"its dedicated script-execution thread is blocked (cooperative cancellation not observed).";
// The worker the run holds NOW — after any number of awaits (SRT-1).
var (workerSlot, workerRunStamp) = workerTracker.Snapshot();
var outcome = workerSlot is { } slot
? scheduler.TryDetachWorker(slot, workerRunStamp)
: WorkerDetachOutcome.NotRunning;