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:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -82,8 +82,9 @@ public sealed class TimedScriptEvaluator<TContext, TResult>
|
||||
{
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user