Allow dev server to exit cleanly (SIGINT/SIGTERM) #67165
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Next's dev server uses a parent/child process model. The parent process acts as a monitor, and it able to restart the server upon configuration changes or upload telemetry even in the case of a crash. The child listens for http requests and does all the real work.
Previously we were sending
SIGKILL
immediately to the child when the parent process gotSIGINT
orSIGTERM
. This ensures that the child exits as quickly as possible, but it also means that the child probably won't have time to finish flushing files to disk (trace files, cache hit statistics, etc.), and can lead to incomplete writes.We should give the child a small amount of time to exit cleanly with
SIGINT
/SIGTERM
before sendingSIGKILL
. The timeout is needed in case the child process is unresponsive/hanging.Tradeoffs
This does cause a short hang for 1 second when the server is heavily loaded (e.g. trying to compile a page, both with webpack and with turbo), and often the process is not able to exit within the 1 second threshold in this case.
At least on the turbopack side, we should be able to better handle this by shutting down the tokio runtime (but this would require creating a second runtime for cleanup) or by preventing turbo-tasks from spawning any more work. That would more quickly free up the CPU to be able to handle the high-priority exit work.
Testing
Run the dev server with
DEBUG="next:*"
and see the log messages about server shutdown when pressing ctrl+c.