Skip to content

Circular dependencies in SSR not detected and causes indefinite hang #2491

@antimatter15

Description

@antimatter15

⚠️ IMPORTANT ⚠️ Please do not ignore this template. If you do, your issue will be closed immediately.

Describe the bug

Let's say we have three files: A, B, and C, where A is the entry point. B and C cyclically depend on each other. A depends on both B and C. In this situation, Vite will not detect the cyclic dependency and simply hang indefinitely.

Reproduction

Create the following 3 files inside of the src/pages directory of the ssr-react playground. Attempt to load the /A page, and notice that it hangs forever and the logs are empty.

// A.jsx
import { addFour } from './B'
import { addTwo } from './C'

export default function Main(){
    return addFour(addTwo(1))
}

// B.js
import { addTwo } from './C'
export function addOne(x){
    return x + 1
}
export function addFour(x){
    return addTwo(addTwo(x))
}

// C.js
import { addOne } from './A'
export function addTwo(x){
    return addOne(addOne(x))
}

Explanation

This bug happens because ssrLoadModule first gets called with A, the entry point. Within instantiateModule, it then tries to load the dependencies B and C in parallel, and caches their promises in the pendingModules map.

When instantiateModule finally runs on B, it blocks until ssrLoadModule completes for its sole dependency: C. When ssrLoadModule is called, the urlStack only has [A, B] so it bypasses the circular dependency test. Since C is already in the pendingModules map, it returns the existing promise waiting for C to load.

Essentially this results in a state where A depends on B and C, while B and C both depend on each other. At this point ssrLoadModule will never complete.

System Info

  • vite version: 2.0.5
  • Operating System: macOS 11.2
  • Node version: 15.11
  • Package manager (npm/yarn/pnpm) and version: Yarn 1.22.10

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions