Uh oh!
There was an error while loading. Please reload this page.
fix(executor): eliminate quadratic backtracking in linkify_markdown - #423
Merged
Jason Robert (jrob5756) merged 2 commits intoAug 13, 2026
Merged
Conversation
Guard against pathological inputs that stalled the event loop: - Use a possessive quantifier on the fenced-code-block regex opener so an unterminated run of backticks/tildes no longer backtracks character-by-character (O(n^2) -> O(n) per starting position). - Replace the existing-markdown-link regex with a linear single-pass scanner (_find_existing_link_spans) that is fuzz-verified equivalent to the regex it replaces but avoids quadratic blowup on long runs of "[" characters. - Add a defensive MAX_LINKIFY_CHARS cap (256K) so any future pathological shape still degrades gracefully instead of hanging, while still normalizing whitespace above the cap. Closes#395
Blocking:
- _find_existing_link_spans was still O(n^2) on "[](" * k input: a
failed find(")") re-scans to the end of the string while the cursor
advances only ~3 chars. Precompute rfind("]")/rfind(")") bounds so a
lookup is skipped once no more delimiters exist ahead, and correct
the comments that claimed this shape was linear/insurance-only.
- All 8 TestExistingLinkProtection tests were vacuous (return [] from
the scanner left them green). Added cases that embed real linkable
content inside/after candidate links so span-removal, the bracket-ref
alternative, and the span end-boundary are each independently pinned.
- The possessive quantifier silently stops protecting a fence whose
opener is longer than its closer. Documented the semantic effect in
the regex comment and added tests pinning the new (CommonMark-correct)
behaviour for both backtick and tilde fences.
- test_quote_token_completes_quickly passed against the pre-fix code
(budget was ~20x too loose). Tightened to a threshold calibrated
against the fixed code's actual runtime.
- test_bracket_wall_completes_quickly never reached the skip-past-"]"
path (no "]" in the input at all). Added a companion test with a
balanced bracket wall that does, plus a "[](" * 30_000 test for the
newly-fixed quadratic shape.
Recommendations applied (comment-only, no behaviour change):
- Corrected the "O(1) per starting position" claim to describe the real
O(n) mechanism (opener consumed once instead of retried).
- Documented that the trailing [^\n]*\n in the fence regex is inert
given the ^...^\1 MULTILINE anchoring, retained only for explicitness.
Reformatted test_linkify.py per `make lint`.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>Jason Robert (jrob5756)
marked this pull request as ready for review
August 12, 2026 23:29
Uh oh!
There was an error while loading. Please reload this page.
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 freeto 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.
Summary
Fixes catastrophic-backtracking / quadratic-time behavior in
linkify_markdownwhen handed pathological input (e.g. long unterminated runs of backticks/tildes or[characters), which could stall the event loop.Changes
_find_existing_link_spans), fuzz-verified equivalent to the regex it replaces.MAX_LINKIFY_CHARScap (256K) so any future pathological shape still degrades gracefully.Testing
Added
TestPathologicalInputPerformanceand fuzz-equivalence tests intests/test_executor/test_linkify.py.Closes#395