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

Draft
wants to merge 2 commits into
base: bgw/cache-hit-stats
Choose a base branch
from

Conversation

bgw
Copy link
Member

@bgw bgw commented Jun 24, 2024

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 got SIGINT or SIGTERM. 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 sending SIGKILL. 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.

bgw added 2 commits June 24, 2024 14:50
Previously we were sending SIGKILL immediately to the child. 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 sending SIGKILL. The timeout is needed in case the
child process is unresponsive/hanging.
@ijjk ijjk added created-by: Turbopack team PRs by the turbopack team type: next labels Jun 24, 2024
Copy link
Member Author

bgw commented Jun 24, 2024

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @bgw and the rest of your teammates on Graphite Graphite

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants