diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting/ScriptRunGuard.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting/ScriptRunGuard.cs index 9421b42b7..979e477fd 100644 --- a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting/ScriptRunGuard.cs +++ b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting/ScriptRunGuard.cs @@ -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; } } + +/// Shared timeout classification for the script run wrappers (#999 / review K-7). +internal static class ScriptTimeouts +{ + /// + /// True when is the exception the script itself threw (it faulted + /// ), false when it is Task.WaitAsync's synthesized timeout. Both are + /// s (a script may throw one directly, or hit a Regex match timeout), so the + /// type alone cannot tell a guard timeout from the script's own failure. + /// + internal static bool IsScriptOwn(Task runTask, Exception caught) + => runTask.IsFaulted && runTask.Exception!.InnerExceptions.Contains(caught); +} diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting/TimedScriptEvaluator.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting/TimedScriptEvaluator.cs index 7a860436e..eac8dbc6d 100644 --- a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting/TimedScriptEvaluator.cs +++ b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting/TimedScriptEvaluator.cs @@ -82,8 +82,9 @@ public sealed class TimedScriptEvaluator { return await runTask.WaitAsync(Timeout, ct).ConfigureAwait(false); } - catch (TimeoutException) + catch (TimeoutException tex) when (!ScriptTimeouts.IsScriptOwn(runTask, tex)) { + // #999 / review K-7: a TimeoutException the script threw itself faulted runTask and propagates unchanged. // WaitAsync's synthesized timeout — the inner task may still be running // on its thread-pool thread (known leak documented in the class summary). // Wrap so callers can distinguish from user-written timeout logic. diff --git a/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests/ScriptRunGuardTests.cs b/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests/ScriptRunGuardTests.cs index 99ae422ec..2bc92e2dd 100644 --- a/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests/ScriptRunGuardTests.cs +++ b/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests/ScriptRunGuardTests.cs @@ -541,6 +541,33 @@ public sealed class ScriptRunGuardTests guard.QuarantinedCount.ShouldBe(1); } + /// + /// #999 / review K-7: a the SCRIPT threw (directly, or a + /// RegexMatchTimeoutException) is the script's own failure — it propagates unchanged, is not a guard + /// timeout, and never quarantines. Before the fix the guard's catch (TimeoutException) took it for + /// WaitAsync's timeout and called Cancel() on the already-disposed run CTS, surfacing + /// . + /// + [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(() => + 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); + } + /// Releases the held script (signal), waits for the orphan to drain, and re-arms the gate for the next attempt. private static async Task ReleaseAndDrain(ScriptRunGuard guard, FakeScriptContext held) { diff --git a/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests/TimedScriptEvaluatorTests.cs b/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests/TimedScriptEvaluatorTests.cs index 3ae5b2803..78cc3b20a 100644 --- a/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests/TimedScriptEvaluatorTests.cs +++ b/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests/TimedScriptEvaluatorTests.cs @@ -47,6 +47,18 @@ public sealed class TimedScriptEvaluatorTests result.ShouldBe(42.0); } + /// #999 / review K-7: a TimeoutException the script threw itself is not the wrapper's timeout. + [Fact] + public async Task A_script_thrown_TimeoutException_is_not_reported_as_a_script_timeout() + { + var inner = ScriptEvaluator.Compile("""throw new System.TimeoutException("mine");"""); + var timed = new TimedScriptEvaluator(inner, TimeSpan.FromSeconds(5)); + + var ex = await Should.ThrowAsync(() => timed.RunAsync(new FakeScriptContext(), TestContext.Current.CancellationToken)); + ex.GetType().ShouldBe(typeof(TimeoutException)); + ex.Message.ShouldBe("mine"); + } + /// Verifies that scripts longer than timeout throw ScriptTimeoutException. [Fact] public async Task Script_longer_than_timeout_throws_ScriptTimeoutException()