fix(scripting): #999 K-7 a script-thrown TimeoutException propagates as itself, not as a disposed-CTS fault or a guard timeout

This commit is contained in:
Joseph Doherty
2026-12-03 10:40:44 -05:00
parent 626d0b33c5
commit ef782c29ae
4 changed files with 62 additions and 3 deletions
@@ -301,11 +301,17 @@ public sealed class ScriptRunGuard
{
result = await runTask.WaitAsync(timeout, ct).ConfigureAwait(false);
}
catch (TimeoutException)
catch (TimeoutException tex) when (!ScriptTimeouts.IsScriptOwn(runTask, tex))
{
// WaitAsync's synthesized timeout only (#999 / review K-7): a TimeoutException the SCRIPT threw (incl.
// RegexMatchTimeoutException) faulted runTask with that very instance and falls through to the generic
// handler below — it is the script's failure, not a guard timeout, and must never count as a strike.
// The caller's cancel wins even when it lands a few µs after the timeout fired.
if (ct.IsCancellationRequested) throw new OperationCanceledException(ct);
runCts.Cancel();
// The run may have finished in the instant the timeout fired; its continuation then already disposed
// runCts. Cancelling a finished run is moot, so the dispose race must not replace the timeout.
try { runCts.Cancel(); }
catch (ObjectDisposedException) { }
if (NoteTimeout(key, state, timeout))
throw new ScriptQuarantinedException(key, state.QuarantineReason!);
throw new ScriptTimeoutException(timeout);
@@ -461,3 +467,16 @@ public sealed class ScriptQuarantinedException : Exception
Key = key;
}
}
/// <summary>Shared timeout classification for the script run wrappers (#999 / review K-7).</summary>
internal static class ScriptTimeouts
{
/// <summary>
/// True when <paramref name="caught"/> is the exception the script itself threw (it faulted
/// <paramref name="runTask"/>), false when it is <c>Task.WaitAsync</c>'s synthesized timeout. Both are
/// <see cref="TimeoutException"/>s (a script may throw one directly, or hit a <c>Regex</c> match timeout), so the
/// type alone cannot tell a guard timeout from the script's own failure.
/// </summary>
internal static bool IsScriptOwn(Task runTask, Exception caught)
=> runTask.IsFaulted && runTask.Exception!.InnerExceptions.Contains(caught);
}