Repository navigation
Turbopack: scale down the Node.js pool after the module graph is built in production - #102211
lukesandberg wants to merge 4 commits into
Conversation
…t in production In production builds, whole_app_module_graphs connected the module graph operation without waiting for it, then scaled the Node.js process pool to zero. The scale-down ran before the graph's loaders had spawned any processes, so they all stayed alive until the build exited. Wait for the graph first, as development mode already does.
|
Checked (6)
|
Failing test suitesCommit: f282410 | About building and testing Next.js
Expand output● next analyze source maps › preserves emitted assets with none source maps ● next analyze source maps › preserves emitted assets with shallow source maps
Expand output● deferred-entries › should build deferred entry successfully ● deferred-entries › should render timestamp written by onBeforeDeferredEntries in deferred source file ● deferred-entries › should build pages router routes when using deferred entries ● deferred-entries › should build pages router getStaticProps routes when using deferred entries ● deferred-entries › should build pages router dynamic getStaticPaths/getStaticProps route when using deferred entries ● deferred-entries › should build pages router getServerSideProps route when using deferred entries ● deferred-entries › should build pages router route with no data fetching when using deferred entries ... truncated ...
Expand output● next analyze source maps › preserves emitted assets with none source maps ● next analyze source maps › preserves emitted assets with shallow source maps
Expand output● turbopack-portable-build-cache › minimal app › reuses the build cache after moving the project ● turbopack-portable-build-cache › minimal app › reuses the build cache after moving the project ● turbopack-portable-build-cache › monorepo with all features › reuses the build cache after moving the project ● turbopack-portable-build-cache › monorepo with all features › reuses the build cache after moving the project
Expand output● webpack-loader-traced-imports › stops loader processes after compilation in a warm build
Expand output● webpack-loader-traced-imports › stops loader processes after compilation in a warm build
Expand output● webpack-loader-traced-imports › stops loader processes after compilation in a warm build
Expand output● next analyze source maps › preserves emitted assets with none source maps ... truncated to fit in one GitHub comment ... |
…_dropping_issues, add regression test Use the Vc returned by resolve() (which already connects the operation) instead of connecting again, apply the same wait to the MCP variant, and check from runAfterProductionCompile that no loader processes outlive compilation.
Mark whole_app_module_graphs session dependent. With a warm cache, loaders run again for changed modules, but the graph's own tasks don't execute again, so the pool was never scaled down and their processes stayed alive until the build exited. Also scale down when computing the graph fails. Make the backends' scale_down/scale_zero infallible (they never failed), and debug-assert at shutdown that no idle Node.js processes are left in builds that don't watch the file system.
…ocesses HeapQueue locks its heap before ACTIVE_POOLS, so release ACTIVE_POOLS before reading the heaps, as scale_down/scale_zero already do.
What?
In production builds, Turbopack now shuts down its Node.js loader processes (the ones that run webpack loaders and PostCSS) as soon as the module graph is built, in cold and warm builds. Before, they stayed alive until
next buildexited.Why?
These processes hold a lot of memory that is never used again. On a large app (vercel/front's
next-site, 14 cores), 16–18 loader processes peak at ~7 GB combined during compilation. They kept ~3.4 GB through chunking, code generation, type checking, collecting page data and static generation, all memory-intensive phases. Build machines have more cores, so more of these processes.How?
Project::whole_app_module_graphsscales the Node.js pool down after computing the module graph. There were two problems:connect()ed the graph operation, which doesn't wait for it. So the scale-down ran before the graph's loaders had spawned any processes. Both modes now wait withresolve().strongly_consistent(), as dev already did. They return the resolvedVc, which is already connected, so production issues still reach the caller.whole_app_module_graphsnor the graph operation executes again, even when an import changed. So the scale-down never ran. Both module-graph functions are nowsession_dependent, so they execute once per session. Re-executing is cheap: the graph is read from the cache and the returned cell only changes if the graph did.Also:
NodeBackend::scale_down/scale_zero, and the worker-pool operations behind them, are infallible. They never returned an error.project_shutdowndebug-asserts that no idle Node.js processes are left in builds that don't watch the file system.test/production/app-dir/webpack-loader-traced-importscounts the build process's own loader processes fromcompiler.runAfterProductionCompile, after a cold build and after a warm rebuild with an edited page.Verification
With release natives built from this branch: the cold-build test fails without the first commit ("Expected 0, Received 1"), and the warm-build test fails without
session_dependent("Expected 0, Received 2"). Both pass with the full change. They're skipped under webpack and on Windows.pnpm test-dev-turbo test/e2e/turbopack-loader-config test/development/app-dir/turbopack-loader-file-dependencies(existing tests, dev loaders unaffected)A Turbopack trace (
NEXT_TURBOPACK_TRACING=turbo-tasks, withtrace_task_dirtyenabled for the trial) showswhole_app_module_graphsexecuting in warm builds (activate_dirty) after the edited module'sevaluate_webpack_loader.On
next-sitein vercel/front, 3 interleaved coldnext buildruns each with and without the first commit. Loader processes were counted frompsevery 200 ms.No process was respawned later in the build. Compile and static generation times were within this machine's noise.
cargo clippy -p turbopack-nodewithprocess_pool,worker_pool, and both;cargo clippy -p next-api -p next-napi-bindingsNot run: the new debug assertion at runtime (a debug native build didn't fit on disk).