Skip to content

fix #63092: stack overflow in isReachableFlowNodeWorker with deeply nested flow graphs#63595

Closed
Amilliox wants to merge 2 commits into
microsoft:mainfrom
Amilliox:fix-63092-flow-stack-overflow
Closed

fix #63092: stack overflow in isReachableFlowNodeWorker with deeply nested flow graphs#63595
Amilliox wants to merge 2 commits into
microsoft:mainfrom
Amilliox:fix-63092-flow-stack-overflow

Conversation

@Amilliox

Copy link
Copy Markdown

Fixes #63092

Root cause: isReachableFlowNodeWorker uses recursion via some() for BranchLabel flow nodes. Deeply nested flow graphs (triggered by syntactically invalid code like as ; after a for-loop) cause stack overflow.

Fix: Replace recursive some() with an explicit worklist. When a worklist element is itself a BranchLabel, its antecedents are inlined into the worklist instead of being recursed into. This prevents unbounded stack growth.

Verification:

  • Before: RangeError: Maximum call stack size exceeded
  • After: Compiler reports TS2304: Cannot find name as``
  • All 348 conformance/controlFlow tests pass

Copilot AI review requested due to automatic review settings June 28, 2026 11:43
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Jun 28, 2026
@typescript-automation typescript-automation Bot added For Backlog Bug PRs that fix a backlog bug labels Jun 28, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a compiler crash in control-flow reachability analysis by removing recursion in isReachableFlowNodeWorker for deeply nested FlowFlags.BranchLabel graphs, replacing it with an explicit worklist to prevent stack overflows.

Changes:

  • Replaced recursive some() traversal for BranchLabel reachability with an iterative worklist approach.
  • Inlined nested BranchLabel antecedents into the worklist to avoid unbounded call stack growth.

Comment thread src/compiler/checker.ts Outdated
Comment on lines +28902 to +28907
const node = worklist.pop()!;
if (node.flags & FlowFlags.BranchLabel) {
const ant = (node as FlowLabel).antecedent;
if (ant) worklist.push(...ant);
}
else if (isReachableFlowNodeWorker(node, /*noCacheCheck*/ false)) {
Comment thread src/compiler/checker.ts Outdated
Comment on lines +28898 to +28901
// Use explicit worklist to avoid stack overflow on deeply nested flow graphs.
const antecedents = (flow as FlowLabel).antecedent;
const worklist = antecedents ? [...antecedents] : [];
while (worklist.length > 0) {
@Amilliox Amilliox force-pushed the fix-63092-flow-stack-overflow branch from e977562 to 4348d8b Compare June 28, 2026 12:30
@Amilliox

Copy link
Copy Markdown
Author

Thanks for the review. Addressed all three points:

  1. Cache check: added cache lookup for Shared nodes before pushing to worklist
  2. Spread to loop: replaced worklist.push(...ant) with for loop
  3. Regression test: added tests/cases/compiler/flowNodeStackOverflow.ts that reproduces the original crash

… deeply nested flow graphs

Replaces recursive some() for BranchLabel with an explicit worklist.
Adds cache lookup for Shared nodes and a regression test.
@Amilliox Amilliox force-pushed the fix-63092-flow-stack-overflow branch from 4348d8b to 95e4e40 Compare June 28, 2026 12:39
@typescript-automation

Copy link
Copy Markdown

It looks like you've sent a pull request to update some generated declaration files related to the DOM. These files aren't meant to be edited by hand, as they are synchronized with files in the TypeScript-DOM-lib-generator repository. You can read more here. For house-keeping purposes, this pull request will be closed.

@github-project-automation github-project-automation Bot moved this from Not started to Done in PR Backlog Jun 28, 2026
@microsoft-github-policy-service

Copy link
Copy Markdown

You are not allowed to delete the mandatory files in this repo.

Total execution time: 6.72 seconds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Crash: RangeError: Maximum call stack size exceeded in isReachableFlowNodeWorker with complex for loop headers

2 participants