-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix: prevent stack overflow when listing files in deep directory structures #7589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
dbcb52c
2293eb8
3963e38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -401,7 +401,20 @@ async function listFilteredDirectories( | |||||
| ignoreInstance, | ||||||
| } | ||||||
|
|
||||||
| async function scanDirectory(currentPath: string, context: ScanContext): Promise<void> { | ||||||
| // Use iterative approach with a queue to avoid stack overflow on deep directory structures | ||||||
| interface QueueItem { | ||||||
| path: string | ||||||
| context: ScanContext | ||||||
| } | ||||||
|
|
||||||
| const queue: QueueItem[] = [{ path: absolutePath, context: initialContext }] | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this iterative approach successfully prevents stack overflow, could we consider adding a queue size limit or batch processing for extremely large directory structures? The queue could potentially consume significant memory if it grows unbounded. |
||||||
|
|
||||||
| while (queue.length > 0) { | ||||||
| const item = queue.shift() | ||||||
|
||||||
| const item = queue.shift() | |
| const item = queue.pop() |
You'd need to reverse the order when pushing to maintain FIFO behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we consider adding a specific test case that simulates a very deep directory structure (like 10,000+ levels) to ensure this iterative approach handles extreme edge cases properly? The existing tests pass, but they might not stress-test the depth that caused the original issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add more detailed documentation here explaining why we switched from recursion to iteration? Future maintainers (including future me) would benefit from understanding the context of this architectural decision.