Fix infinite loop in symbolWalker during extract function refactoring#62907
Fix infinite loop in symbolWalker during extract function refactoring#62907Dino0204 wants to merge 6 commits intomicrosoft:mainfrom
Conversation
|
@microsoft-github-policy-service agree |
There was a problem hiding this comment.
Pull request overview
This PR fixes a stack overflow crash in the TypeScript language server that occurs during refactoring operations on code with deeply recursive generic types. The fix adds a recursion depth limit to the symbolWalker traversal logic to complement the existing cycle detection mechanism.
Key changes:
- Added depth tracking (
levelcounter andmaxRecursionDepthconstant) tovisitTypefunction in symbolWalker - Wrapped type visiting logic in try-finally to ensure proper depth counter cleanup
- Added a fourslash test to verify the fix prevents crashes
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/compiler/symbolWalker.ts | Adds recursion depth tracking with a limit of 100 levels to prevent stack overflow when traversing deeply nested recursive types |
| tests/cases/fourslash/extractFunctionSymbolWalkerInfiniteLoop.ts | Adds regression test for the symbolWalker infinite loop fix |
|
With 6.0 out as the final release vehicle for this codebase, we're closing all PRs that don't fit the merge criteria for post-6.0 patches. If you think this was a mistake and this PR fits the post-6.0 patch criteria, please post to the 6.0 iteration issue with details (specifically, which PR and which patch criteria it satisfies). Next steps for PRs:
|
Fixes #62471
Description
This PR fixes a
RangeError: Maximum call stack size exceededcrash in the TypeScript Server (VS Code Extension) that occurs when invoking "Refactor..." actions on code involving recursive generic types.The issue was caused by
symbolWalkerentering an infinite loop when traversing recursive types (e.g., a type referencing itself in a property). While the walker already had cycle detection viavisitedTypesandvisitedSymbols, it lacked a depth limit. This allowed cases where the recursion depth exceeded the JavaScript call stack limit before the cycle detection could take effect.Reproduction
The following code triggers the crash when trying to use "Refactor..." (e.g., Extract Function) on the selection:
Behavior:
RangeError: Maximum call stack size exceeded.*Implementation Details
I modified
src/compiler/symbolWalker.tsto add recursion depth tracking in thevisitTypefunction.Key Changes:
levelcounter and amaxRecursionDepthconstant (set to 100) to track the current recursion depth.level > maxRecursionDepth, preventing the stack overflow.try-finallyblock to ensure thelevelcounter is properly decremented even on early returns or errors.Why this approach (Defense-in-Depth)?
This works in conjunction with the existing cycle detection (
visitedTypes) to provide a robust safeguard:visitedTypesarray prevents infinite loops on direct cycles.Test Plan
tests/cases/fourslash/extractFunctionSymbolWalkerInfiniteLoop.ts.verify.not.refactorAvailable("Extract function")on the recursive type pattern shown above.hereby runtestslocally.Baseline Changes
symbolWalker.