Skip to content

fix: avoid quadratic path normalization on long virtual URIs - #3502

Open
net-sky wants to merge 1 commit into
LuaLS:masterfrom
net-sky:fix/uri-normalize-backtracking
Open

net-sky wants to merge 1 commit into
LuaLS:masterfrom
net-sky:fix/uri-normalize-backtracking

Conversation

@net-sky

@net-sky net-sky commented Nov 16, 2026

Copy link
Copy Markdown

Avoid quadratic path normalization on long virtual URIs

Closes #3501.

Problem

A textDocument/didOpen whose URI embeds a long payload (virtual diff documents do this: the URI carries the whole file as URL-encoded base64) blocks the single LSP thread for minutes at 100% of one core. In my session, didOpen itself took 376 s and every queued documentColor / inlayHint / codeAction / codeLens / semanticTokens / documentSymbol / foldingRange / $/cancelRequest request returned afterwards, all of them taking 216 s.

The didOpen handler calls files.open(), which dispatches the open watchers synchronously; the diagnostics watcher reaches workspace.isIgnored() → getRelativePath() → files.normalize(). Because the URI does not start with file:, the whole URI is treated as a filesystem path and normalized. The ..-folding loop then runs

path:gsub('[^/\\]+[/\\]+%.%.[/\\]', '/', 1)

on a ~100 KB string that contains no /, \ or . (URL-encoded base64 escapes both + and /), so the pattern engine scans to the end of the string and backtracks from every start position: O(n²), ~5.4×10⁹ steps for 104 k characters.

Lua.diagnostics.enableScheme (default ["file"]) does not help, because it is checked after isIgnored().

Changes

  • script/files.lua: run the ..-folding loop only when the string contains .. and is at most 4096 characters long. The gsub can never match without a literal .., so skip it with a plain (linear) find. The length limit also protects other callers from the same pattern on absurdly long inputs; real filesystem paths are far below it.
  • script/workspace/workspace.lua: in isIgnored, return false for non-file: schemes before any path handling. Ignore patterns are filesystem paths, so matching a virtual document URI against them is meaningless — and currently very expensive.
  • test/basic/uri-normalize.lua: regression test. It asserts that files.normalize on a 100 k-character separator-free URI returns unchanged within 2 s, that workspace.isIgnored returns false quickly for such a URI, and that ordinary paths still fold .. segments.
  • changelog.md: one FIX entry under Unreleased.

Measurements

Case Before After
104,129-char URI, didOpen + follow-up request > 300 s, one core pegged answers within ~3 s (harness latency included)
100,000-char URI, files.normalize minutes < 1 ms
same length with / every 64 chars 46 ms 46 ms
normal workspace load, real file diagnostics unchanged unchanged

Behavior

No behavior change for real paths: when the string has no .., the folding loop could not have matched anyway; when it has .. and is shorter than 4096 characters, the code path is exactly the old one. Non-file: URIs were never meaningfully matched by files.exclude / .gitignore, which are filesystem pattern sets.

Opening a document whose URI embeds a long payload (e.g. an AI diff virtual document) made files.normalize scan the whole string from every start position, because the '..' folding pattern requires a path separator that such a URI does not contain. The default 'Lua.diagnostics.enableScheme' check did not help since it runs after ws.isIgnored().

Only fold when the path can contain '..' and is short enough to be a real path, and skip path handling entirely for non-file URIs in ws.isIgnored().
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.

Long virtual document URI causes quadratic path normalization

1 participant