fix(host-daemon): avoid stack overflow listing large directory trees - #2363
Open
yazydzhi wants to merge 1 commit into
Open
fix(host-daemon): avoid stack overflow listing large directory trees#2363yazydzhi wants to merge 1 commit into
yazydzhi wants to merge 1 commit into
Conversation
listPathsRecursively merged each subdirectory's results with results.push(...childResults). Array.push spreads its arguments onto the call stack, so a subtree with enough entries (~100k+ depending on the engine) throws "RangeError: Maximum call stack size exceeded" instead of returning. This broke file listing/search entirely for large workspaces (e.g. a big Obsidian vault with tens of thousands of files), surfacing as an opaque HTTP 502 from any plugin or route that calls files.listPaths/listTree. Replace the spread-push with a plain loop, which has no argument-count limit. Added a regression test that recreates a 150k-file subtree and asserts listPathsRecursively returns instead of throwing. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Open
3 tasks
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 free
to 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.
Fixes #2362.
Problem
`listPathsRecursively` in `apps/host-daemon/src/command-handlers/file-list.ts` merges each subdirectory's results with:
```ts
results.push(...(await listPathsRecursively({ ...args, dir: fullPath })));
```
`Array.prototype.push(...arr)` spreads every element as a call argument. Once an accumulated subtree crosses V8's argument-count ceiling (~100k, engine-dependent), this throws `RangeError: Maximum call stack size exceeded` instead of returning. Any caller of file listing/search (a plugin's `listTree`, project file search, etc.) on a large enough workspace — a big Obsidian vault, a large monorepo — sees this surface as an opaque `HTTP 502`.
Fix
Replace the spread-push with a plain loop, which has no argument-count limit:
```ts
const childResults = await listPathsRecursively({ ...args, dir: fullPath });
for (const childResult of childResults) results.push(childResult);
```
Testing
Added a regression test that creates a 150k-file subtree and asserts `listPathsRecursively` returns instead of throwing.
Test plan