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 { ...@@ -14,6 +14,12 @@ import type {
TraceQueryResult, TraceQueryResult,
MemoryEvictionMode, MemoryEvictionMode,
ServerHmrVersion as NativeServerHmrVersion, ServerHmrVersion as NativeServerHmrVersion,
projectCompilationEventsSubscribe,
projectFeatureUsage,
projectInvalidateFileSystemCache,
projectOnExit,
projectShutdown,
projectUpdateInfoSubscribe,
} from './generated-native' } from './generated-native'
export type { TraceServerHandle, TraceQueryOptions, TraceQueryResult } export type { TraceServerHandle, TraceQueryOptions, TraceQueryResult }
...@@ -336,6 +342,8 @@ export interface Project { ...@@ -336,6 +342,8 @@ export interface Project {
* end of the build, after `writeAllEntrypointsToDisk`. The Rust implementation * end of the build, after `writeAllEntrypointsToDisk`. The Rust implementation
* walks the whole-app module graph and will error if invoked from a * walks the whole-app module graph and will error if invoked from a
* development project, because dev builds do not produce a complete graph. * development project, because dev builds do not produce a complete graph.
*
* @see {@link projectFeatureUsage}
*/ */
featureUsage(): Promise<BuildFeatureUsage[]> featureUsage(): Promise<BuildFeatureUsage[]>
...@@ -359,6 +367,7 @@ export interface Project { ...@@ -359,6 +367,7 @@ export interface Project {
getSourceForAsset(filePath: string): Promise<string | null> getSourceForAsset(filePath: string): Promise<string | null>
getSourceMap(filePath: string): Promise<string | null> getSourceMap(filePath: string): Promise<string | null>
getSourceMapSync(filePath: string): string | null getSourceMapSync(filePath: string): string | null
traceSource( traceSource(
...@@ -366,19 +375,24 @@ export interface Project { ...@@ -366,19 +375,24 @@ export interface Project {
currentDirectoryFileUrl: string currentDirectoryFileUrl: string
): Promise<TurbopackStackFrame | null> ): Promise<TurbopackStackFrame | null>
/** @see {@link projectUpdateInfoSubscribe} */
updateInfoSubscribe( updateInfoSubscribe(
aggregationMs: number aggregationMs: number
): AsyncIterableIterator<UpdateMessage> ): AsyncIterableIterator<UpdateMessage>
/** @see {@link projectCompilationEventsSubscribe} */
compilationEventsSubscribe( compilationEventsSubscribe(
eventTypes?: string[] eventTypes?: string[]
): AsyncIterableIterator<CompilationEvent> ): AsyncIterableIterator<CompilationEvent>
/** @see {@link projectInvalidateFileSystemCache} */
invalidateFileSystemCache(): Promise<void> 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 = export type Route =
......
...@@ -102,6 +102,8 @@ const CHILD_EXIT_TIMEOUT_MS = parseInt( ...@@ -102,6 +102,8 @@ const CHILD_EXIT_TIMEOUT_MS = parseInt(
process.env.NEXT_EXIT_TIMEOUT_MS ?? '100', process.env.NEXT_EXIT_TIMEOUT_MS ?? '100',
10 10
) )
const shouldWaitForChildExit =
process.env.NEXT_DEV_WAIT_FOR_TURBOPACK_SHUTDOWN === '1'
const handleSessionStop = async (signal: NodeJS.Signals | number | null) => { const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
if (signal != null && child?.pid) child.kill(signal) if (signal != null && child?.pid) child.kill(signal)
...@@ -118,11 +120,14 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => { ...@@ -118,11 +120,14 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
child.exitCode === null && child.exitCode === null &&
child.signalCode === null child.signalCode === null
) { ) {
let exitTimeout = setTimeout(() => { let exitTimeout: NodeJS.Timeout | undefined
child?.kill('SIGKILL') if (!shouldWaitForChildExit) {
}, CHILD_EXIT_TIMEOUT_MS) exitTimeout = setTimeout(() => {
child?.kill('SIGKILL')
}, CHILD_EXIT_TIMEOUT_MS)
}
await once(child, 'exit').catch(() => {}) await once(child, 'exit').catch(() => {})
clearTimeout(exitTimeout) if (exitTimeout) clearTimeout(exitTimeout)
} }
sessionSpan.stop() sessionSpan.stop()
......
...@@ -569,7 +569,11 @@ export async function createHotReloaderTurbopack( ...@@ -569,7 +569,11 @@ export async function createHotReloaderTurbopack(
opts.onDevServerCleanup?.(async () => { opts.onDevServerCleanup?.(async () => {
setBundlerFindSourceMapImplementation(() => undefined) setBundlerFindSourceMapImplementation(() => undefined)
setBundlerFindSourceMapURLImplementation(() => null) 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() await lockfile?.unlock()
}) })
// Subscription detects route additions/removals; returned endpoints stay lazy. // Subscription detects route additions/removals; returned endpoints stay lazy.
......
...@@ -38,12 +38,8 @@ describe('warm-restart task statistics', () => { ...@@ -38,12 +38,8 @@ describe('warm-restart task statistics', () => {
// minimum-compilation-time threshold. // minimum-compilation-time threshold.
'TURBO_ENGINE_SNAPSHOT_MIN_ACTIVE_TIME_MILLIS=0', 'TURBO_ENGINE_SNAPSHOT_MIN_ACTIVE_TIME_MILLIS=0',
`NEXT_TURBOPACK_TASK_STATISTICS=${STATS_RELATIVE_PATH}`, `NEXT_TURBOPACK_TASK_STATISTICS=${STATS_RELATIVE_PATH}`,
// The task-statistics file is written by an `on_exit` handler in the // Wait for turbo-tasks to persist the cache before the dev process exits.
// napi binding. In dev that handler only runs if the child process 'NEXT_DEV_WAIT_FOR_TURBOPACK_SHUTDOWN=1',
// 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',
].join(' ') ].join(' ')
const { next } = nextTestSetup({ const { next } = nextTestSetup({
...@@ -69,10 +65,8 @@ describe('warm-restart task statistics', () => { ...@@ -69,10 +65,8 @@ describe('warm-restart task statistics', () => {
async function stop() { async function stop() {
if (isNextDev) { 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 // 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') await next.stop('SIGTERM')
} else { } else {
await next.stop() 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