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

Detached-Flush Never Exits, Leaking Reparented Process Per Next Dev Shutdown

Regression in Next.js 16.0.3: detached-flush child spawned on dev server shutdown hangs after submitting telemetry when next.config leaves a live handle, causing orphaned processes to accumulate and consume large memory/CPU.

highConfidence 93%Next.jsAffected V16.0.3Affected V16.1.xAffected V16.2.xAffected V16.3.1Affected V16.4.0-Canary.0

Origin Analysis

Commit ad93e452 removed process.exit(0) after unlinkSync in packages/next/src/telemetry/detached-flush.ts. Because loadConfig(PHASE_DEVELOPMENT_SERVER, dir) evaluates the entire user next.config and can leave a ref'd libuv handle (e.g., setInterval), the detached child never exits after completing telemetry. Spawned with detached:true, it is reparented to init and never supervised.
1. Scaffold a Next.js app with next@16.0.3 or later. 2. Add setInterval(() => {}, 1000); to next.config.ts/module.exports (or any config plugin that keeps a live handle). 3. Run next dev, issue at least one request to queue telemetry. 4. Send SIGINT/Ctrl+C to the dev server. 5. Observe a detached-flush.js child remains alive after parent exit (ps -o pid,ppid,rss,command). It never exits. Or use the provided repro: git clone https://github.com/gesposito/nextjs-detached-flush-orphan && cd nextjs-detached-flush-orphan && npm install && npm run repro.

Fixing Code Block

Edge Case Audit

This hotfix force-exits the detached process. However, process.exit does not automatically kill child processes spawned during config evaluation; those grandchildren may be reparented and continue running. A more complete fix would avoid evaluating next.config in the flush process or use detached:true with process group termination, but that is a larger change. Rollback: if this patch causes NEW issues with telemetry debug output or other side effects, revert by pinning next@16.0.2 or manually pkill -f 'telemetry/detached-flush.js'. Add test coverage for both normal exit and lingering-handle scenarios.

Ecosystem Topology