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

`Next Build` Blocks On Telemetry Flush Instead Of Using Detached Process

The build process awaits `telemetry.flush()`, causing unnecessary delay before returning. Telemetry submission should be detached like in dev mode.

mediumConfidence 90%Next.jsAffected V16.3.3Affected V16.4.0-Canary.8

Origin Analysis

In `packages/next/src/build/index.ts`, the build pipeline synchronously waits for `telemetry.flush()` to complete, while the dev server uses `flushDetached` which spawns a detached process. The `flushDetached` function currently only accepts mode `'dev'` and throws for others, preventing its reuse in build.
1. Clone https://github.com/refirst11/nextjs-turbopack-feature-usage-repro 2. Run `pnpm install` 3. Ensure telemetry enabled: `npx next telemetry status` 4. Run `pnpm benchmark` and observe build wall time difference with `NEXT_TELEMETRY_DISABLED=1`.

Fixing Code Block

--- a/packages/next/src/build/index.ts +++ b/packages/next/src/build/index.ts @@ -1,5 +1,6 @@ +import { flushDetached } from '../telemetry/flush-detached' ... - await telemetry.flush() + flushDetached('build', process.cwd()) --- a/packages/next/src/telemetry/flush-detached.ts +++ b/packages/next/src/telemetry/flush-detached.ts @@ -1,6 +1,6 @@ -export function flushDetached(mode: 'dev', dir: string): void { - if (mode !== 'dev') { - throw new Error('flushDetached is only supported in dev mode') - } +export function flushDetached(mode: 'dev' | 'build', dir: string): void { + if (mode !== 'dev' && mode !== 'build') { + throw new Error('flushDetached only supports dev or build mode') + }
The build script now calls the same detached flush function used by the dev server, avoiding blocking on the telemetry HTTP request. The function signature is widened to accept 'build' mode, and the guard now allows both modes.

Edge Case Audit

Detached child processes may still be running when the parent build exits, which could lead to orphaned processes in rare cases. On Windows, detached spawn may not fully detach and can cause console window flashes or process leaks. If telemetry delivery becomes unreliable, rollback by restoring `await telemetry.flush()` and consider adding a timeout instead of full detachment. Also, concurrent builds may spawn multiple detached processes, increasing resource usage.

Ecosystem Topology