Error recovery repeats the same cycle millions of times on a 14-byte input
AI Policy
- I have read the AI Policy and this issue complies with it.
Problem
With a 9-rule grammar, parsing the 14-byte input <<a/a>{[:aaaaa takes about 16 seconds and 2 GB of memory. Without the last a, it takes 0.1 ms.
Error recovery repeats the same 70-line cycle of the debug log about 3.5 million times. In each cycle, every stack version pauses on an error. ts_parser__condense_stack then resumes the paused version that sorts first, which is at column 8, and removes the other paused versions, including the ones that reached column 13. The resumed version recovers along the same path, so the next cycle is identical.
The final tree has 7,040,927 zero-width MISSING "!" nodes at byte 8. At 610 each (ERROR_COST_PER_MISSING_TREE + ERROR_COST_PER_RECOVERY), they cost 4,294,965,470, just below 2^32. So the loop seems to stop only because the 32-bit error cost is about to overflow. With a larger real-world grammar, a 15-byte input kept allocating memory and had not finished after 6 minutes.
Steps to reproduce
git clone --depth=1 https://github.com/exKAZUu/tree-sitter-error-recovery-loop-repro
cd tree-sitter-error-recovery-loop-repro
npm install
npm run repro.tmp/short.txt Parse: 0.09 ms 142 bytes/ms (ERROR [0, 0] - [0, 13])
.tmp/input.txt Parse: 5000.01 ms (timed out)The README shows one cycle of the debug log.
Expected behavior
Error recovery moves forward through the input, so the parse finishes about as fast as it does without the last a.
Tree-sitter version (tree-sitter --version)
tree-sitter 0.27.0 (also reproduces with lib/ at master dcdc8cc5)
Operating system/version
macOS 27.0
Note: I used Claude Code with Opus 5.5 to reduce the grammar and the input from a fuzzer finding, and to write the reproduction repository and the draft of this issue. I checked the reproduction myself.
Source: tree-sitter/tree-sitter