Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dev server to exit cleanly (SIGINT/SIGTERM) #67165

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions packages/next/src/cli/next-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
} from '../lib/helpers/get-reserved-port'
import os from 'os'
import { once } from 'node:events'
import { clearTimeout } from 'timers'

export type NextDevOptions = {
turbo?: boolean
Expand All @@ -57,13 +58,29 @@ let traceUploadUrl: string
let sessionStopHandled = false
let sessionStarted = Date.now()

// How long should we wait for the child to cleanly exit after sending
// SIGINT/SIGTERM to the child process before sending SIGKILL?
const CHILD_EXIT_TIMEOUT_MS = parseInt(
process.env.NEXT_EXIT_TIMEOUT_MS ?? '100',
10
)

const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
if (child?.pid) child.kill(signal ?? 0)
if (signal != null && child?.pid) child.kill(signal)
if (sessionStopHandled) return
sessionStopHandled = true

if (child?.pid && child.exitCode === null && child.signalCode === null) {
if (
signal != null &&
child?.pid &&
child.exitCode === null &&
child.signalCode === null
) {
let exitTimeout = setTimeout(() => {
child?.kill('SIGKILL')
}, CHILD_EXIT_TIMEOUT_MS)
await once(child, 'exit').catch(() => {})
clearTimeout(exitTimeout)
}

try {
Expand Down Expand Up @@ -124,8 +141,8 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
process.exit(0)
}

process.on('SIGINT', () => handleSessionStop('SIGKILL'))
process.on('SIGTERM', () => handleSessionStop('SIGKILL'))
process.on('SIGINT', () => handleSessionStop('SIGINT'))
process.on('SIGTERM', () => handleSessionStop('SIGTERM'))

// exit event must be synchronous
process.on('exit', () => child?.kill('SIGKILL'))
Expand Down Expand Up @@ -291,7 +308,9 @@ const nextDev = async (
}
return startServer(startServerOptions)
}
await handleSessionStop(signal)
// Call handler (e.g. upload telemetry). Don't try to send a signal to
// the child, as it has already exited.
await handleSessionStop(/* signal */ null)
})
})
}
Expand Down
12 changes: 11 additions & 1 deletion packages/next/src/server/lib/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,20 @@ export async function startServer(

try {
const cleanupListeners = [() => new Promise((res) => server.close(res))]
let cleanupStarted = false
const cleanup = () => {
debug('start-server process cleanup')
if (cleanupStarted) {
// We can get duplicate signals, e.g. when `ctrl+c` is used in an
// interactive shell (i.e. bash, zsh), the shell will recursively
// send SIGINT to children. The parent `next-dev` process will also
// send us SIGINT.
return
}
cleanupStarted = true
;(async () => {
debug('start-server process cleanup')
await Promise.all(cleanupListeners.map((f) => f()))
debug('start-server process cleanup finished')
process.exit(0)
})()
}
Expand Down
6 changes: 5 additions & 1 deletion test/production/graceful-shutdown/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ describe('Graceful Shutdown', () => {
app = await initNextServerScript(
serverFile,
/- Local:/,
{ ...process.env, PORT: appPort.toString() },
{
...process.env,
NEXT_EXIT_TIMEOUT_MS: '10',
PORT: appPort.toString(),
},
undefined,
{ cwd: next.testDir }
)
Expand Down
Loading