You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix telemetry event loss on build failures and server shutdown (#85867)
## Problem
Telemetry events were not being captured in three scenarios:
### 1. MCP Telemetry Lost on Dev Server Shutdown
Two telemetry instances were created during dev server startup. MCP
events were recorded to one instance but shutdown flushed a different
instance.
**Fix:** Reuse the existing telemetry instance from `traceGlobals`.
### 2. Process Exit Timeout Killing Async Telemetry
The CLI force-kills child processes after 100ms
(`NEXT_EXIT_TIMEOUT_MS`). Async telemetry operations were interrupted
before completion.
**Fix:** Use `flushDetached()` to write events to disk and spawn a
detached process for async submission, allowing immediate parent exit.
### 3. Build Errors Calling `process.exit()` Before Telemetry Flush
`process.exit()` calls in error handlers (SWC failures, missing
dependencies, route conflicts) bypassed finally blocks and killed
processes before telemetry completed.
**Fix:** Throw errors instead of calling `process.exit()`, add finally
blocks with `await telemetry.flush()` in critical paths.
## Changes
**Core:**
- `server/dev/next-dev-server.ts`: Reuse telemetry instance
- `server/lib/start-server.ts`: Use `flushDetached()` for shutdown
- `build/swc/index.ts`: Remove `process.exit()` calls
- `build/turbopack-build/impl.ts`: Add telemetry flush in worker finally
block
- `build/index.ts`: Add telemetry flush in finally block, throw instead
of exit
- `lib/verify-partytown-setup.ts`: Throw instead of exit
**Supporting:**
- `telemetry/storage.ts`: Generate unique event files per PID
- `telemetry/detached-flush.ts`: Accept optional eventsFile parameter
- `errors.json`: Update detached-flush error message
## Testing
All telemetry tests now pass:
- MCP telemetry on dev server shutdown
- SWC load failures in worker threads
- Build configuration errors
✅ Non-blocking shutdown
✅ Timeout-independent
✅ No race conditions
✅ Works in worker threads
Copy file name to clipboardExpand all lines: packages/next/errors.json
+3-1Lines changed: 3 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -905,5 +905,7 @@
905
905
"904": "The file \"%s\" must export a function, either as a default export or as a named \"%s\" export.",
906
906
"905": "Page \"%s\" cannot use \\`export const unstable_prefetch = ...\\` without enabling \\`cacheComponents\\`.",
907
907
"906": "Bindings not loaded yet, but they are being loaded, did you forget to await?",
908
-
"907": "bindings not loaded yet. Either call `loadBindings` to wait for them to be available or ensure that `installBindings` has already been called."
908
+
"907": "bindings not loaded yet. Either call `loadBindings` to wait for them to be available or ensure that `installBindings` has already been called.",
909
+
"908": "Invalid flags should be run as node detached-flush dev ./path-to/project [eventsFile]",
910
+
"909": "Failed to load SWC binary for %s/%s, see more info here: https://nextjs.org/docs/messages/failed-loading-swc"
0 commit comments