diff --git a/packages/next/src/cli/next-dev.ts b/packages/next/src/cli/next-dev.ts index 1948d2fcf2..8c1bdfc897 100644 --- a/packages/next/src/cli/next-dev.ts +++ b/packages/next/src/cli/next-dev.ts @@ -219,7 +219,7 @@ const nextDev: CliCommand = async (argv) => { const { validateTurboNextConfig } = require('../lib/turbopack-warning') as typeof import('../lib/turbopack-warning') - const { loadBindings, __isCustomTurbopackBinary } = + const { loadBindings, __isCustomTurbopackBinary, teardownHeapProfiler } = require('../build/swc') as typeof import('../build/swc') const { eventCliSession } = require('../telemetry/events/version') as typeof import('../telemetry/events/version') @@ -286,6 +286,13 @@ const nextDev: CliCommand = async (argv) => { if (!isCustomTurbopack) { await telemetry.flush() } + + // There are some cases like test fixtures teardown that normal flush won't hit. + // Force flush those on those case, but don't wait for it. + ;['SIGTERM', 'SIGINT', 'beforeExit', 'exit'].forEach((event) => + process.on(event, () => teardownHeapProfiler()) + ) + return server } else { let cleanupFns: (() => Promise | void)[] = []