fix(scripting): resolve Medium code-review finding (Core.Scripting-007)
In TimedScriptEvaluator.RunAsync, the catch (TimeoutException) block now checks ct.IsCancellationRequested before throwing ScriptTimeoutException, so a caller cancellation that races a timeout deterministically surfaces as OperationCanceledException regardless of which WaitAsync observes first. Regression test Caller_cancellation_wins_even_when_timeout_fires_first added. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -76,6 +76,14 @@ public sealed class TimedScriptEvaluator<TContext, TResult>
|
||||
// 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.
|
||||
//
|
||||
// The class docs guarantee "caller-supplied cancel wins over timeout".
|
||||
// When both fire at nearly the same time, WaitAsync observes them in
|
||||
// non-deterministic order, so a cancel that arrives a few µs after the
|
||||
// timeout still reaches here as TimeoutException. Re-check the token so
|
||||
// the guarantee holds regardless of race ordering. (Core.Scripting-007.)
|
||||
if (ct.IsCancellationRequested)
|
||||
throw new OperationCanceledException(ct);
|
||||
throw new ScriptTimeoutException(Timeout);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user