Skip to content

fix: add timeout to tree-sitter parsing to prevent unbounded memory growth (APP-4323)#11640

Draft
warp-dev-github-integration[bot] wants to merge 1 commit into
masterfrom
fix/APP-4323-tree-sitter-parse-timeout
Draft

fix: add timeout to tree-sitter parsing to prevent unbounded memory growth (APP-4323)#11640
warp-dev-github-integration[bot] wants to merge 1 commit into
masterfrom
fix/APP-4323-tree-sitter-parse-timeout

Conversation

@warp-dev-github-integration
Copy link
Copy Markdown

Description

Add a 5-second timeout to tree-sitter parsing to prevent unbounded memory growth from pathological error recovery.

Root Cause

Sentry issue 7259255054 shows "Excessive memory usage detected" with the heap profile revealing tree-sitter's ts_parser__recover allocating 6.1 GiB across just 2 samples, plus ts_subtree_new_leaf at 2.4 GiB. Total tree-sitter-related allocations exceeded 8.8 GiB.

The problem: SyntaxTreeState::parse_text called parse_with_options without any timeout or cancellation mechanism. When tree-sitter encounters error recovery scenarios on certain inputs, it can allocate unbounded memory.

Fix

Uses the ParseOptions progress callback to enforce a 5-second deadline (PARSE_TIMEOUT). When the deadline is exceeded, the progress callback returns false, causing tree-sitter to stop parsing and return None. The caller preserves the existing (edited but un-reparsed) syntax tree, maintaining approximate highlighting until the next successful parse.

Relationship to PR #11344

This is complementary to the file-size guard in PR #11344, which skips tree-sitter entirely for files >1 MiB. This timeout protects against pathological parse times even on files under the size threshold — error recovery can spike memory on smaller inputs too.

Linked Issue

Testing

  • cargo check -p syntax_tree passes
  • cargo clippy -p syntax_tree -- -D warnings passes
  • cargo fmt passes

Agent Mode

  • Warp Agent Mode - This PR was created via Warp's AI Agent Mode

CHANGELOG-BUG-FIX: Fixed excessive memory usage from tree-sitter parsing on pathological inputs

Conversation: https://staging.warp.dev/conversation/9ec872fd-2e84-464c-9871-7a23c7c998d2
Run: https://oz.staging.warp.dev/runs/019e5b94-d27c-700c-b7a8-68ba622d802b

This PR was generated with Oz.

…rowth (APP-4323)

Tree-sitter's error recovery (ts_parser__recover) can allocate unbounded
memory when parsing pathological inputs. The heap profile from Sentry
issue 7259255054 shows ts_parser__recover allocating 6+ GiB across
multiple samples.

This adds a 5-second timeout via ParseOptions progress callback. When
the deadline is exceeded the parser returns None and we keep the
existing (edited but un-reparsed) tree, preserving approximate
highlighting until the next successful parse.

This is complementary to the file-size guard in PR #11344 — it protects
against pathological parse times even for files under the size threshold.

Co-Authored-By: Oz <oz-agent@warp.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant