Enforce custom regex-engine timeout via an inline deadline instead of a thread-pool timer#2686
Merged
Merged
Conversation
…-pool timer The custom (QuickJS-port) regex engine enforced its match timeout with `new CancellationTokenSource(timeout)`, whose timer callback runs on the thread pool. Under a saturated pool — e.g. parallel test runs on CI — that callback can be starved for many seconds, so the abort fires far past the requested timeout. This is the root cause of the flaky RegExpTests.PreparedScriptHonorsRegexTimeoutForCustomEngine failure, where a 1s prepare-time timeout was observed to take 17.5s (assertion threshold: 15s). The delay is unbounded under load, so no test threshold can be made reliable while the timer approach stands. Enforce the timeout with an inline monotonic deadline (Stopwatch.GetTimestamp) checked at the interpreter's existing interrupt checkpoints instead — exactly how .NET's own Regex enforces MatchTimeout (the code already claimed to mirror it). The interpreter thread is always scheduled while it spins, so the abort fires promptly regardless of pool pressure. Also removes a per-call CancellationTokenSource allocation + timer registration on the custom-engine path. Verified under a flooded thread pool (net472): the regex 1s timeout now fires in ~1.3s, whereas a CancellationTokenSource(1s) timer was starved for 25s+ under the same load. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
lahma
force-pushed
the
regrex-test-hardening
branch
from
July 14, 2026 08:11
259b2eb to
0e7b838
Compare
lahma
enabled auto-merge (squash)
July 14, 2026 08:11
This was referenced Jul 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The flaky test
RegExpTests.PreparedScriptHonorsRegexTimeoutForCustomEngineintermittently fails on Windows CI (example run):A 1s prepare-time regex timeout was observed to fire only after 17.5s.
Root cause
The custom (QuickJS-port) regex engine — used for patterns .NET
Regexcan't handle, like the catastrophic-backtrackingTestRegex— enforced its match timeout withnew CancellationTokenSource(timeout). That source's timer callback runs on the thread pool. When the pool is saturated (parallel xUnit runs on a CI runner), the callback is starved and the token isn't cancelled until much later, so the interpreter keeps backtracking well past the requested timeout.The delay is unbounded under load, so no test-side threshold can be made reliable while the timer approach stands — the fix has to be at the source.
I reproduced this directly under a flooded thread pool (net472):
CancellationTokenSource(1s)timerStopwatchdeadlineFix
Enforce the timeout with an inline monotonic deadline (
Stopwatch.GetTimestamp()), checked at the interpreter's existing interrupt checkpoints (once per 10,000 opcodes) — exactly how .NET's ownRegexenforcesMatchTimeout(the code already claimed to "mirror" it). The interpreter thread is always scheduled while it spins, so the abort fires promptly regardless of pool pressure.RegExpInterpreter/JintRegExpEngine: thread along deadline(absoluteStopwatch.GetTimestamp()value,NoDeadline= no timeout) instead of aCancellationToken; each checkpoint now callsCheckDeadline(deadline).RegExpPrototype.ExecuteWithTimeout/IsMatchWithTimeout: compute the deadline from the configured timeout instead of allocating aCancellationTokenSource+ timer per call. On expiry anOperationCanceledExceptionis still mapped to the same richRegexMatchTimeoutException(input, source, timeout).Bonus: removes a per-call
CancellationTokenSourceallocation + timer registration on the custom-engine path.Internal-only change; no public API surface affected.
Testing
Jint.Testsfull suite — net10.0 3597 / net472 3534, all greenJint.Tests.PublicInterface— 114/114 on both TFMs~RegExpsubset — 3930/0TestRegexstill routes to the custom engine after Prefer .NET Regex for quantified groups without capture or lookaround hazards #2682's routing rework (so this test genuinely exercises the fixed path)🤖 Generated with Claude Code