AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Fix Rust Panic In Next Dev With Rspack When Source-Mapping During Rebuild

Next.js dev server using rspack panics and aborts when error overlay or browser log forwarding requests original stack frames during a rebuild, due to stale compilation handle aliasing the in-progress compilation.

highConfidence 95%Next.jsAffected V16.3.3Affected V16.3.4Affected V16.4.0-Canary.15

Origin Analysis

In middleware-webpack.ts, getModuleById accesses compilation.modules and chunkGraph.getModuleId without checking if the compiler is rebuilding. Under rspack, Stats.compilation is a raw pointer to the underlying Rust compilation object which is replaced during rebuild, leading to dereferencing stolen StealCell artifacts and panic.
1. Clone https://github.com/Dwlad90/next-rspack-stale-compilation-repro 2. Run npm install 3. Run npm run generate 4. Run npm run repro Alternatively, start next dev with rspack, open a page with console.error, and edit files repeatedly.

Fixing Code Block

Edge Case Audit

This fix relies on watcher.running being accurately set by the bundler. It may cause source maps to be temporarily unavailable during rebuilds, but requests will retry later. If watcher implementation changes or running is not updated synchronously, the guard could be bypassed. Also, multiple compilers (e.g., client/server) are not individually accounted for; but in dev mode with a single compiler this is fine. Rollback: remove the guard, but then the panic returns. No breaking changes to API.

Ecosystem Topology