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

fix(turbopack): avoid reexport cycle deadlock

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 6ef02769
......@@ -35,6 +35,7 @@ use crate::{
module_fragments::part::module::EcmascriptModulePartAsset,
references::esm::base::ReferencedAsset,
runtime_functions::{TURBOPACK_DYNAMIC, TURBOPACK_ESM},
side_effect_optimization::locals::module::EcmascriptModuleLocalsModule,
utils::module_id_to_lit,
};
......@@ -159,6 +160,19 @@ pub async fn follow_reexports(
let mut module = module;
let mut export_name = export_name;
loop {
// A locals module only exposes local bindings, so there are no more reexports to follow.
// Returning it still preserves its evaluation and side effects. Avoid asking it for side
// effects or exports from the original module: resolving the facade's synthetic locals
// reference can happen while that module is still being analyzed, and either request would
// create a dependency cycle.
if ResolvedVc::try_downcast_type::<EcmascriptModuleLocalsModule>(module).is_some() {
return Ok(FollowExportsResult::cell(FollowExportsResult {
module,
export_name: Some(export_name),
ty: FoundExportType::Found,
}));
}
if !ignore_side_effects
&& *module.side_effects().await? != ModuleSideEffects::SideEffectFree
{
......
# `__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 { 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.
* Regression test: turbopack used to hang (deadlock) while building the module
* graph for an import cycle whose modules are split into facade + locals
* modules.
*
* Topology (same shape as ../../async-modules/cycle-2, plus re-exports):
*
......@@ -19,14 +19,15 @@ import { A } from './A'
*
* 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.
* `follow_reexports(A_facade, "A")`. That walks facade -> locals, and the locals
* step used to ask the locals module for its side effects, which are derived
* from the original module's `analyze()` — already in flight further up the same
* import cycle. The result was a turbo-tasks await cycle: the process sat at
* ~0.5% CPU with completely flat RSS and never finished, 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.
* The async module is not required to trigger this; see
* `../reexport-cycle-deadlock` for the same cycle without a top-level `await`.
*/
it('should not deadlock building a re-exporting import cycle with an async module', () => {
......
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 { syncFn } from './syncFn'
export { helper } from './helper'
export function B(n: number) {
if (n > 0) {
C(n - 1)
syncFn()
}
}
import { A } from './A'
export { helper } from './helper'
export function C(n: number) {
if (n > 0) {
A(n - 1)
}
}
import { A } from './A'
/*
* Regression test: turbopack used to hang (deadlock) while building the module
* graph for an import cycle whose modules are split into facade + locals
* modules.
*
* Topology:
*
* index -> A
* ^\
* | v
* C<-B -> syncFn
*
* 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`.
*
* This is the same bug as `../reexport-cycle-async-deadlock`, but without any
* async module: a top-level `await` anywhere in the cycle is not required to
* trigger it. The re-export cycle alone is enough.
*/
it('should not deadlock building a re-exporting import cycle', () => {
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