Skip to content

fix: prevent catastrophic regex backtracking in _extractImagesFromOutput#307447

Merged
justschen merged 2 commits intomicrosoft:mainfrom
mossgowild:fix/extractImagesFromOutput-regex-backtracking
Apr 2, 2026
Merged

fix: prevent catastrophic regex backtracking in _extractImagesFromOutput#307447
justschen merged 2 commits intomicrosoft:mainfrom
mossgowild:fix/extractImagesFromOutput-regex-backtracking

Conversation

@mossgowild
Copy link
Copy Markdown
Contributor

Fixes #307431

Problem

_extractImagesFromOutput in RunInTerminalTool freezes the renderer main thread for 15–20 seconds on large terminal output. The root cause is a two-step pattern:

  1. Line 1291 output.replace(/\r?\n/g, '') strips all newlines, producing a single mega-string.
  2. Line 1294 the regex /(?:[^\s]*[\/\\][^\s]*\.(?:png|jpe?g|gif|webp|bmp))/gi has ambiguity — [^\s]* can consume / and \, overlapping with the [\/\\] separator. On a long string the engine explores an exponential number of split-points, causing O(2ⁿ) backtracking.

Profiler evidence: 14/14 renderer-hang samples land on RegExpStringIterator.next inside _extractImagesFromOutput.

Fix

  • Process per-line instead of collapsing newlines — file paths never span lines, so collapsing was unnecessary and is the direct trigger of the mega-string.
  • Skip lines longer than 10 000 chars — defensive guard against pathological single lines.
  • Rewrite regex atoms from [^\s]* to [^\s/\\]* — each atom can no longer consume path separators, making the [/\\] tokens unambiguous and eliminating backtracking entirely.

Copy link
Copy Markdown
Collaborator

@justschen justschen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for taking a look!

@justschen justschen enabled auto-merge (squash) April 2, 2026 18:30
@justschen justschen merged commit ccf5e83 into microsoft:main Apr 2, 2026
19 checks passed
@vs-code-engineering vs-code-engineering bot added this to the 1.115.0 milestone Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

run_in_terminal tool: _extractImagesFromOutput regex causes catastrophic backtracking and UI freeze

3 participants