-
Notifications
You must be signed in to change notification settings - Fork 30.4k
fix: gracefully shutdown server #59551
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,17 +34,20 @@ import { | |
| } from '../lib/helpers/get-reserved-port' | ||
| import os from 'os' | ||
|
|
||
| type Child = ReturnType<typeof fork> | ||
| type ExitCode = Parameters<Child['kill']>[0] | ||
|
|
||
| let dir: string | ||
| let child: undefined | ReturnType<typeof fork> | ||
| let child: undefined | Child | ||
| let config: NextConfigComplete | ||
| let isTurboSession = false | ||
| let traceUploadUrl: string | ||
| let sessionStopHandled = false | ||
| let sessionStarted = Date.now() | ||
|
|
||
| const handleSessionStop = async (signal: string | null) => { | ||
| const handleSessionStop = async (signal: ExitCode | null) => { | ||
| if (child) { | ||
| child.kill((signal as any) || 0) | ||
| child.kill(signal ?? 0) | ||
| } | ||
| if (sessionStopHandled) return | ||
| sessionStopHandled = true | ||
|
|
@@ -108,8 +111,11 @@ const handleSessionStop = async (signal: string | null) => { | |
| process.exit(0) | ||
| } | ||
|
|
||
| process.on('SIGINT', () => handleSessionStop('SIGINT')) | ||
| process.on('SIGTERM', () => handleSessionStop('SIGTERM')) | ||
| process.on('SIGINT', () => handleSessionStop('SIGKILL')) | ||
| process.on('SIGTERM', () => handleSessionStop('SIGKILL')) | ||
|
|
||
| // exit event must be synchronous | ||
| process.on('exit', () => child?.kill('SIGKILL')) | ||
|
|
||
| const nextDev: CliCommand = async (args) => { | ||
| if (args['--help']) { | ||
|
|
@@ -333,16 +339,4 @@ const nextDev: CliCommand = async (args) => { | |
| await runDevServer(false) | ||
| } | ||
|
|
||
| function cleanup() { | ||
| if (!child) { | ||
| return | ||
| } | ||
|
|
||
| child.kill('SIGTERM') | ||
| } | ||
|
|
||
| process.on('exit', cleanup) | ||
| process.on('SIGINT', cleanup) | ||
| process.on('SIGTERM', cleanup) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the other handlers were already killing the child - this one always sends SIGTERM. However, we still need to add a special handler for the exit event since it needs to be synchronous |
||
|
|
||
| export { nextDev } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -264,10 +264,9 @@ export async function startServer( | |
| }) | ||
|
|
||
| try { | ||
| const cleanup = (code: number | null) => { | ||
| const cleanup = () => { | ||
| debug('start-server process cleanup') | ||
| server.close() | ||
| process.exit(code ?? 0) | ||
| server.close(() => process.exit(0)) | ||
| } | ||
| const exception = (err: Error) => { | ||
| if (isPostpone(err)) { | ||
|
|
@@ -279,11 +278,11 @@ export async function startServer( | |
| // This is the render worker, we keep the process alive | ||
| console.error(err) | ||
| } | ||
| process.on('exit', (code) => cleanup(code)) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://nodejs.org/api/process.html#event-exit
Once Also calling the same cleanup code, now that there's a log, was resulting in the log printing twice after a signal was received. |
||
| // Make sure commands gracefully respect termination signals (e.g. from Docker) | ||
| // Allow the graceful termination to be manually configurable | ||
| if (!process.env.NEXT_MANUAL_SIG_HANDLE) { | ||
| // callback value is signal string, exit with 0 | ||
| process.on('SIGINT', () => cleanup(0)) | ||
| process.on('SIGTERM', () => cleanup(0)) | ||
| process.on('SIGINT', cleanup) | ||
| process.on('SIGTERM', cleanup) | ||
| } | ||
| process.on('rejectionHandled', () => { | ||
| // It is ok to await a Promise late in Next.js as it allows for better | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed these to SIGKILL to make sure the child process stops immediately. In a production server we would want to pass along the SIGINT or SIGTERM but if you're killing the dev server you probably wouldn't expect it to do any cleanup. This should also be consistent with the current behavior that exits right away with
process.exit