next.js
d0c901ea - Turbopack: own Node.js pool state with EvaluatePool (#102345)

Commit
5 hours ago
Turbopack: own Node.js pool state with EvaluatePool (#102345) ### What? Make each Turbopack Node.js evaluation pool own its idle workers. Each pool holds a handle that owns its idle state: the handle publishes the state to the global registry while the pool is alive, and when the pool is dropped it removes the state from the registry and closes it. This covers both backends: - **Child processes:** `ChildProcessPool` holds an `IdleProcessQueueHandle`, which owns the idle queue and derefs to it. When the pool is dropped, the handle removes the queue from `ACTIVE_POOLS` and closes it. Closing the queue kills the idle processes. A process that comes back to the closed queue later, from an operation that was still running or from pre-warming, is killed instead of being kept idle. - **Worker threads:** each `WorkerThreadPool` now has its own idle state. Before, the state was shared by filename and working directory. The state is owned by a `PoolRegistration`, which derefs to it and keeps it registered while the pool is alive. When the pool is dropped, its idle workers are terminated, and so are workers that operations still in progress return later. `scale_down` and `scale_zero` keep their current API and behaviour. They now only see pools that are still alive. ### Why? Pool state used to live in process-global registries with no owner. When an `EvaluatePool` cell went away (it was invalidated and replaced, evicted, or dropped when turbo-tasks shut down), its idle Node.js processes or workers could stay registered and running. Tying the registration to the cell that owns the pool means cleanup follows the cell's lifetime. There is no extra process-wide closed state that later or deferred compilation could run into. This is the first step in reworking how Node.js pools are managed. It avoids the process-wide close flag that caused the deferred-entries problem in #102211. ### How? The idle state is closed under its own lock. Returning or pushing a worker uses the same lock. So a worker that is returned after the pool was dropped is either seen by the close, or sees that the state is already closed and is stopped by the caller. Registry locks are released before queues are closed or workers are terminated. Both registries are sets of entries compared and hashed by the address of the state they hold (`IdleProcessQueue` for child processes, `RegisteredPool` for worker threads). The worker-thread bookkeeping lives in a small module that doesn't depend on N-API, so its lifecycle can be unit tested without a Node.js worker host. A process-pool unit test checks that dropping a pool while it pre-warms a process doesn't leave its idle queue referenced. The `webpack-loader-traced-imports` production test now checks that Turbopack's Node.js loader processes exit after compilation, in a cold build and in a warm build where a page was edited. A `runAfterProductionCompile` hook in its `next.config.mjs` counts the build's `[turbopack-node]` child processes with `ps`. Because the processes exit while Turbopack shuts down, which runs concurrently with the hook, the hook waits up to 10s for them to exit and records how many are left. The test sets `TURBO_ENGINE_IGNORE_DIRTY` so that locally built native bindings keep the persistent cache that the warm build needs. It is gated to Turbopack and skipped on Windows, which has no `ps`. ### Verification - `pnpm test-start-turbo test/production/app-dir/webpack-loader-traced-imports/webpack-loader-traced-imports.test.ts` with native bindings built from this branch: 5/5 runs passed. - The same test with the published canary bindings, which have the previous behaviour, fails: 1 loader process is still running 10s after a cold build, and 2 after the warm build. - `cargo test -p turbopack-node` - `turbopack-node` clippy with `-D warnings` for default features, worker threads only, both backends, and no default features <!-- fleet a3633673-4f4d-4244-a677-6d0a7f3d86c1 --> --------- Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com> Co-authored-by: Luke Sandberg <210140+lukesandberg@users.noreply.github.com>
Author
Parents
Loading