Unverified Commit 975b9cff authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Add `next dev` wait for Turbopack shutdown (#98646)

## Summary

Add `NEXT_DEV_WAIT_FOR_TURBOPACK_SHUTDOWN` to make `next dev` wait for
Turbopack's full project shutdown before exiting. This waits for active
TurboTasks work and cache persistence instead of relying on fixed delays
or the parent's normal 100 ms child-exit timeout.

Use the option in the warm-restart task statistics test. Link documented
`Project` interface methods to their native binding documentation.

## Verification

- `pnpm --filter=next types`
- `npx eslint --config eslint.config.mjs
packages/next/src/cli/next-dev.ts
packages/next/src/server/dev/hot-reloader-turbopack.ts
test/e2e/filesystem-cache/warm-restart-task-stats.test.ts`
- Pre-commit lint-staged checks
- CI tests passed

<!-- NEXT_JS_LLM -->
parent 25fe6f6f
......@@ -14,6 +14,12 @@ import type {
TraceQueryResult,
MemoryEvictionMode,
ServerHmrVersion as NativeServerHmrVersion,
projectCompilationEventsSubscribe,
projectFeatureUsage,
projectInvalidateFileSystemCache,
projectOnExit,
projectShutdown,
projectUpdateInfoSubscribe,
} from './generated-native'
export type { TraceServerHandle, TraceQueryOptions, TraceQueryResult }
......@@ -336,6 +342,8 @@ export interface Project {
* end of the build, after `writeAllEntrypointsToDisk`. The Rust implementation
* walks the whole-app module graph and will error if invoked from a
* development project, because dev builds do not produce a complete graph.
*
* @see {@link projectFeatureUsage}
*/
featureUsage(): Promise<BuildFeatureUsage[]>
......@@ -359,6 +367,7 @@ export interface Project {
getSourceForAsset(filePath: string): Promise<string | null>
getSourceMap(filePath: string): Promise<string | null>
getSourceMapSync(filePath: string): string | null
traceSource(
......@@ -366,19 +375,24 @@ export interface Project {
currentDirectoryFileUrl: string
): Promise<TurbopackStackFrame | null>
/** @see {@link projectUpdateInfoSubscribe} */
updateInfoSubscribe(
aggregationMs: number
): AsyncIterableIterator<UpdateMessage>
/** @see {@link projectCompilationEventsSubscribe} */
compilationEventsSubscribe(
eventTypes?: string[]
): AsyncIterableIterator<CompilationEvent>
/** @see {@link projectInvalidateFileSystemCache} */
invalidateFileSystemCache(): Promise<void>
shutdown(): Promise<void>
/** @see {@link projectShutdown} */
shutdown(): ReturnType<typeof projectShutdown>
onExit(): Promise<void>
/** @see {@link projectOnExit} */
onExit(): ReturnType<typeof projectOnExit>
}
export type Route =
......
......@@ -102,6 +102,8 @@ const CHILD_EXIT_TIMEOUT_MS = parseInt(
process.env.NEXT_EXIT_TIMEOUT_MS ?? '100',
10
)
const shouldWaitForChildExit =
process.env.NEXT_DEV_WAIT_FOR_TURBOPACK_SHUTDOWN === '1'
const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
if (signal != null && child?.pid) child.kill(signal)
......@@ -118,11 +120,14 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
child.exitCode === null &&
child.signalCode === null
) {
let exitTimeout = setTimeout(() => {
child?.kill('SIGKILL')
}, CHILD_EXIT_TIMEOUT_MS)
let exitTimeout: NodeJS.Timeout | undefined
if (!shouldWaitForChildExit) {
exitTimeout = setTimeout(() => {
child?.kill('SIGKILL')
}, CHILD_EXIT_TIMEOUT_MS)
}
await once(child, 'exit').catch(() => {})
clearTimeout(exitTimeout)
if (exitTimeout) clearTimeout(exitTimeout)
}
sessionSpan.stop()
......
......@@ -569,7 +569,11 @@ export async function createHotReloaderTurbopack(
opts.onDevServerCleanup?.(async () => {
setBundlerFindSourceMapImplementation(() => undefined)
setBundlerFindSourceMapURLImplementation(() => null)
await project.onExit()
if (process.env.NEXT_DEV_WAIT_FOR_TURBOPACK_SHUTDOWN === '1') {
await project.shutdown()
} else {
await project.onExit()
}
await lockfile?.unlock()
})
// Subscription detects route additions/removals; returned endpoints stay lazy.
......
......@@ -38,12 +38,8 @@ describe('warm-restart task statistics', () => {
// minimum-compilation-time threshold.
'TURBO_ENGINE_SNAPSHOT_MIN_ACTIVE_TIME_MILLIS=0',
`NEXT_TURBOPACK_TASK_STATISTICS=${STATS_RELATIVE_PATH}`,
// The task-statistics file is written by an `on_exit` handler in the
// napi binding. In dev that handler only runs if the child process
// gets a chance to clean up (i.e. SIGTERM, not SIGKILL). The parent
// `next dev` process gives the child 100ms by default before
// escalating to SIGKILL — bump that so the on-exit handler can flush.
'NEXT_EXIT_TIMEOUT_MS=30000',
// Wait for turbo-tasks to persist the cache before the dev process exits.
'NEXT_DEV_WAIT_FOR_TURBOPACK_SHUTDOWN=1',
].join(' ')
const { next } = nextTestSetup({
......@@ -69,10 +65,8 @@ describe('warm-restart task statistics', () => {
async function stop() {
if (isNextDev) {
// Persistent cache snapshot is on a 1s idle timer; give it room.
await waitFor(3000)
// SIGTERM (not the harness default SIGKILL) so the dev server gets
// to run its cleanup, which is what flushes the task-stats file.
// to run its cleanup and settle the Turbopack cache.
await next.stop('SIGTERM')
} else {
await next.stop()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment