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
@@ -541,6 +541,33 @@ public sealed class ScriptRunGuardTests
guard.QuarantinedCount.ShouldBe(1);
}
/// <summary>
/// #999 / review K-7: a <see cref="TimeoutException"/> the SCRIPT threw (directly, or a
/// <c>RegexMatchTimeoutException</c>) is the script's own failure — it propagates unchanged, is not a guard
/// timeout, and never quarantines. Before the fix the guard's <c>catch (TimeoutException)</c> took it for
/// <c>WaitAsync</c>'s timeout and called <c>Cancel()</c> on the already-disposed run CTS, surfacing
/// <see cref="ObjectDisposedException"/>.
/// </summary>
[Theory]
[InlineData("throw new System.TimeoutException(\"mine\");", typeof(TimeoutException))]
[InlineData(
"return System.Text.RegularExpressions.Regex.IsMatch(new string('a', 40000) + \"!\", \"^(a+)+$\", System.Text.RegularExpressions.RegexOptions.None, System.TimeSpan.FromMilliseconds(20)) ? 1 : 0;",
typeof(System.Text.RegularExpressions.RegexMatchTimeoutException))]
public async Task A_script_thrown_TimeoutException_propagates_and_is_not_a_guard_timeout(string source, Type expected)
{
var guard = new ScriptRunGuard(new ScriptingOptions { QuarantineAfterConsecutiveTimeouts = 2 });
using var evaluator = Compile(source);
for (var i = 0; i < 4; i++)
{
var ex = await Should.ThrowAsync<Exception>(() =>
Run(guard, "own-timeout", source, evaluator, TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken));
ex.GetType().ShouldBe(expected, $"run {i}: {ex}");
}
guard.IsQuarantined("own-timeout", out _).ShouldBeFalse("a script's own exception is not a timeout strike");
guard.QuarantinedCount.ShouldBe(0);
}
/// <summary>Releases the held script (signal), waits for the orphan to drain, and re-arms the gate for the next attempt.</summary>
private static async Task ReleaseAndDrain(ScriptRunGuard guard, FakeScriptContext held)
{
@@ -47,6 +47,18 @@ public sealed class TimedScriptEvaluatorTests
result.ShouldBe(42.0);
}
/// <summary>#999 / review K-7: a TimeoutException the script threw itself is not the wrapper's timeout.</summary>
[Fact]
public async Task A_script_thrown_TimeoutException_is_not_reported_as_a_script_timeout()
{
var inner = ScriptEvaluator<FakeScriptContext, double>.Compile("""throw new System.TimeoutException("mine");""");
var timed = new TimedScriptEvaluator<FakeScriptContext, double>(inner, TimeSpan.FromSeconds(5));
var ex = await Should.ThrowAsync<Exception>(() => timed.RunAsync(new FakeScriptContext(), TestContext.Current.CancellationToken));
ex.GetType().ShouldBe(typeof(TimeoutException));
ex.Message.ShouldBe("mine");
}
/// <summary>Verifies that scripts longer than timeout throw ScriptTimeoutException.</summary>
[Fact]
public async Task Script_longer_than_timeout_throws_ScriptTimeoutException()