Unverified Commit 6ef02769 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Turbopack: add a reproduction for the re-export cycle + async module deadlock

Turbopack deadlocks while building the module graph for an import cycle whose
modules are split into facade + locals modules and which contains an async
module. This reproduces on canary with no configuration: a single
`export { x } from './y'` in each cycle member is enough to trigger the split
(`EcmascriptExports::split_locals_and_reexports`), and one top-level `await`
makes the cycle async.

Symptom: the build never finishes. The process sits at ~0.5% CPU with entirely
flat RSS and never spawns a Node process, so it is an await that never resolves
on the Rust side, before any generated code runs -- not a spin loop and not
unbounded graph growth. For a real project this is `next build` hanging with no
output and no error; in CI it shows up only as a job timeout.

Where it stalls, from instrumented tracing:

1. The module-graph builder blocks in `primary_chunkable_referenced_modules` on
   `module.references().await` for the entry module, i.e. inside `analyze()`.
2. Resolving `import { A } from './A'` reaches `apply_reexport_tree_shaking`
   (`turbopack/src/lib.rs`), which calls `follow_reexports(A_facade, "A")`.
3. `follow_reexports` takes exactly two steps -- facade, then `<locals>` -- and
   never completes that iteration.
4. The stall is inside the locals module's `get_exports()`, which awaits the
   original module's `analyze()`, already in flight further up the same import
   cycle. That closes a turbo-tasks await cycle.

`followReexports: false` makes it pass immediately, which pins the stall to the
`follow_reexports` path rather than to async-module handling generally. Note the
topology is deliberately close to `execution/turbopack/async-modules/cycle-2`,
whose header documents an earlier bug in `compute_async_module_info_single` for
the same shape; this is a different failure and that function is never reached.

The fixture is parked under `__hangs__/`, a path that matches neither fixture
glob in `execution.rs`, so nothing runs it automatically. `__skipped__` is not
usable for a hang: `test_skipped_fails` still executes its fixtures (under
`#[should_panic]`), so a fixture that never returns would stall the whole
`test cargo unit` job. The README explains how to arm it by moving it up one
directory; both the inert state (`running 0 tests`) and the armed state
(exit 124) were verified.

Found while enabling export mangling by default in this suite
(#97672 / #97676 / #97770). Mangling makes this far easier to hit, because it
splits every module with exports rather than only re-exporting ones, but the bug
is independent of mangling and predates that work -- hence this reproduction on
plain canary.

<!-- NEXT_JS_LLM -->
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent 76e24273
# `__hangs__` — reproductions that deadlock the bundler
Fixtures in here reproduce bugs where turbopack **hangs** rather than failing.
They deliberately sit at a path that matches **neither** fixture glob in
`tests/execution.rs`:
- `tests/execution/*/*/*/input/index.js` (the normal suite, which also excludes `__skipped__`)
- `tests/execution/*/*/__skipped__/*/input/index.js` (`test_skipped_fails`, which still *executes*
its fixtures under `#[should_panic]`)
so nothing runs them automatically. `__skipped__` is not usable for a hang: a fixture that never
returns hangs `test_skipped_fails` too, and would stall the whole `test cargo unit` job with no
useful output.
## Running one by hand
Move the fixture up one directory so it matches the normal glob, then run it with a timeout — it
will not terminate on its own:
```bash
cd turbopack/crates/turbopack-tests/tests/execution/turbopack/side-effect-optimization
mv __hangs__/reexport-cycle-async-deadlock .
cd -
timeout 120 cargo test -p turbopack-tests --test execution -- reexport_cycle_async_deadlock
# exit code 124 == still deadlocking
```
Remember to move it back.
## `reexport-cycle-async-deadlock`
An import cycle whose modules are split into facade + locals modules, containing an async module.
```
index -> A
^\
| v
C<-B -> asyncFn (asyncFn has a top-level await)
```
`A`, `B` and `C` each carry `export { helper } from './helper'`. That single re-export is enough for
`EcmascriptExports::split_locals_and_reexports` to return true, so each is split into an
`EcmascriptModuleFacadeModule` + `EcmascriptModuleLocalsModule`. **No config option is required** —
this is default canary behaviour.
### Symptom
`cargo test` never finishes. The process sits at roughly 0.5% CPU with completely flat RSS, and no
Node process is ever spawned — so it is an await that never resolves on the Rust side, before any
generated code runs. Not a spin loop, and not unbounded graph growth.
For a real project this means `next build` hanging with no output and no error; in CI it surfaces
only as a job timeout.
### Where it stalls
Instrumented `eprintln!` tracing localizes it as follows:
1. The module-graph builder blocks in `primary_chunkable_referenced_modules` on
`module.references().await` for the entry module — i.e. inside `analyze()`.
2. Resolving `import { A } from './A'` reaches `apply_reexport_tree_shaking`
(`turbopack/src/lib.rs`), which calls `follow_reexports(A_facade, "A")`.
3. `follow_reexports` takes exactly two steps — `A (facade)` → `A <locals>` — and then never
completes the iteration. (The facade's ident renders without a suffix, because
`AssetIdent::to_string` deliberately omits `ModulePart::Facade`.)
4. The stall is inside the locals module's `get_exports()`, which awaits the *original* module's
`analyze()` — already in flight further up the same import cycle. That closes a turbo-tasks
await cycle.
Setting `followReexports: false` in an `options.json` makes it pass immediately, which pins the
stall to the `follow_reexports` path rather than to async-module handling in general.
Note the fixture's shape is deliberately close to `../../async-modules/cycle-2`, whose header
comment documents an earlier bug in `compute_async_module_info_single` for the same topology. This
is a *different* failure: `compute_async_module_info_single` is never reached here.
### Why it matters now
This is reachable on canary today with nothing but a re-export, an import cycle and a top-level
`await`. It was found while enabling export mangling by default in this suite (#97672/#97676/#97770)
— mangling makes it much easier to hit, because it splits every module with exports rather than only
re-exporting ones — but the bug itself is independent of mangling and predates that work.
import { B } from './B'
// This re-export is what forces the facade/locals split for this module
// (`EcmascriptExports::split_locals_and_reexports` returns true as soon as a
// module has any `ImportedBinding`/star re-export). No other option is needed.
export { helper } from './helper'
export function A(n: number) {
if (n > 0) {
B(n - 1)
}
}
import { C } from './C'
import { asyncFn } from './asyncFn'
export { helper } from './helper'
export function B(n: number) {
if (n > 0) {
C(n - 1)
asyncFn()
}
}
import { A } from './A'
export { helper } from './helper'
export function C(n: number) {
if (n > 0) {
A(n - 1)
}
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
// Top-level await makes this an async module, which is what drags the whole
// import cycle into async-module handling.
await sleep(0)
export function asyncFn() {}
import { A } from './A'
/*
* Regression test: turbopack hangs (deadlocks) while building the module graph
* for an import cycle whose modules are split into facade + locals modules and
* which contains an async module.
*
* Topology (same shape as ../../async-modules/cycle-2, plus re-exports):
*
* index -> A
* ^\
* | v
* C<-B -> asyncFn (top-level await)
*
* A, B and C each carry `export { helper } from './helper'`. That re-export is
* enough to make `EcmascriptExports::split_locals_and_reexports` return true,
* so each of them is split into an `EcmascriptModuleFacadeModule` plus an
* `EcmascriptModuleLocalsModule`.
*
* Resolving `import { A } from './A'` goes through `apply_reexport_tree_shaking`
* (module resolution, `turbopack/src/lib.rs`), which calls
* `follow_reexports(A_facade, "A")`. That walks facade -> locals and then stalls
* inside the locals module's `get_exports()`, which awaits the original module's
* `analyze()` — already in flight further up the same import cycle. The result is
* a turbo-tasks await cycle: the process sits at ~0.5% CPU with completely flat
* RSS and never finishes, so `next build` would hang with no output and no error.
*
* Setting `followReexports: false` in options.json makes this pass instantly,
* which is what pins the stall to the `follow_reexports` path.
*/
it('should not deadlock building a re-exporting import cycle with an async module', () => {
A(10)
expect(true).toBe(true)
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment