fix(turbopack): avoid panics from transient chunk group indices (#100928)
### What? In `next dev` with Turbopack lazy dynamic imports (`experimental.turbopackLazyDynamicImports` and/or `turbopackLazyDynamicImportsSSR`), apps with nested `React.lazy(() => import(...))` client components log Rust panics while lazy imports activate: ``` thread 'tokio-rt-worker' panicked at turbopack/crates/turbopack-core/src/module_graph/chunk_group_info.rs:111:51: index out of bounds: the len is 31 but the index is 31 ``` The dev server recovers and pages render, but the output is noisy and the panics hide real errors. This PR turns that out-of-range lookup into a contextual, transient error and adds regression coverage. It doesn't touch #100705 or canonicalization (#100705 was reverted in #100898). The panic happens on canary without it. ### Why? `compute_chunk_group_info` creates the `module_chunk_groups` and `chunk_group_indices` child cells with `ResolvedVc::cell(...)` while it builds the result, and creates the outer `ChunkGroupInfo` cell, which holds `chunk_groups` inline, at the end. Each cell is published when it is written (`turbo-tasks-backend/src/backend/operation/update_cell.rs`). So while lazy activation recomputes the graph, an eventually consistent reader of `get_chunk_groups_for_module` (or `get_chunk_group`) can pair the **old** inline `chunk_groups` with a **new** child cell. If the new graph has more chunk groups, `self.chunk_groups[index]` is out of range. I confirmed this with temporary instrumentation that wasn't committed, on a fixture like the new test: - 34 `compute_chunk_group_info` executions. Each was internally consistent (max index = len − 1). - 1147 reads, of which 59 paired cells from different executions. 3 were out of range (the panics), and 56 had in-range indices. - For all 26 affected modules, the last read was consistent and newer than every torn read. The reader depends on both cells and is recomputed, so strongly consistent consumers end up with the correct result. ### How? - `ChunkGroupInfo::chunk_group_at` replaces direct indexing in `get_chunk_groups_for_module` and `get_chunk_group`. - An index from a child cell that the inline `chunk_groups` doesn't have becomes an `anyhow` error with context, never an empty or partial result. - The dependencies and keyed reads are the same as before. - **This is a guard, not a consistent snapshot.** Torn reads with in-range indices are still possible in the middle of a recomputation, and they are corrected by recomputation as before. Making them impossible needs coherent publication or snapshot identity across the cells, which is a separate design. **Tests** - `test/development/app-dir/lazy-dynamic-nested-imports`: nested `React.lazy` imports with `x` reached from `a`, `c` and `y`, with both lazy flags enabled. - Checks every level in the SSR HTML. - Hydrates every level in the browser with `retry()`, and asserts that no request returns ≥ 400 and that there are no transport errors. Browser cancellations (`net::ERR_ABORTED`) are ignored; they happen without this change too. - Asserts that the CLI output contains no `panicked` and no `⨯`. - Without the fix it fails on the panic assertion (39 panics). With the fix it passed 3 runs in a row. - A deterministic Rust test (`test_chunk_groups_for_module_with_mixed_generation_cells`) pairs a real graph's membership cell with a truncated inline group list. It panics before the fix and returns the error after it. **Verification** (on canary `a14cbc44`) - 12 dev sessions of a nested lazy fixture: 2 shapes × client lazy / SSR lazy / both × 2 sessions. All clean: 0 panics, 0 `⨯`, SSR and hydration OK. Before the fix almost every lazy session panicked. - `test/development/app-dir/lazy-dynamic-imports` (19/19) and `lazy-dynamic-chunk-list-collision` (1/1). - `cargo nextest -p turbopack-core -p next-core`: 289 passed. `-p turbopack-tests`: 458 passed, no snapshot changes. fmt, check and clippy pass with `-D warnings`. - Production output is unchanged: for the fixture's 25 client files, path and SHA-256 are identical before and after. **Not fixed here** - A build with extra logging `await`s still hit an SSR `ChunkLoadError` for a lazy proxy chunk in 1 of 3 sessions, with no panic. That is a separate issue that I'm tracking separately. <!-- NEXT_JS_LLM --> <!-- fleet 4f67c6f7-bbe2-454b-9a17-822a60ef8f7e --> --------- Co-authored-by:vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com> Co-authored-by:
Tobias Koppers <1365881+sokra@users.noreply.github.com> Co-authored-by:
Luke Sandberg <210140+lukesandberg@users.noreply.github.com>
Showing
Please
register
or
sign in
to comment