-
-
Notifications
You must be signed in to change notification settings - Fork 8k
When running in middleware mode, vite should not kill process on SIGTERM #7627
Description
Describe the bug
I am running vite in middleware mode, nested inside another server. When the process receives a SIGTERM signal, e.g. through a restart on a code-change by node-dev, vite kills the process immediately and does not give my server the time to take care of cleanups. I believe vite should not handle the SIGTERM at all when in middleware mode and leave it to the outside code to manually call await vite.close(). This may be a breaking change for many, so the least it should do is not call process.exit(0) in middleware mode. If this is the solution to this issue, then it would be nice if it was possible to still call vite.close() from the outside, and it would not throw but bail out silently on a second call, if it was already called before by the SIGTERM handler.
This behavior is caused by the following lines of code in vite:
vite/packages/vite/src/node/server/index.ts
Lines 438 to 446 in cf59005
| exitProcess = async () => { | |
| try { | |
| await server.close() | |
| } finally { | |
| process.exit(0) | |
| } | |
| } | |
| process.once('SIGTERM', exitProcess) |
Reproduction
System Info
System:
OS: macOS 12.3.1
CPU: (8) x64 Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
Memory: 1.40 GB / 32.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.14.2 - ~/.nvm/versions/node/v16.14.2/bin/node
Yarn: 1.22.15 - ~/.nvm/versions/node/v16.14.2/bin/yarn
npm: 8.5.0 - ~/.nvm/versions/node/v16.14.2/bin/npm
Browsers:
Chrome: 100.0.4896.75
Chrome Canary: 102.0.4986.0
Firefox: 98.0.2
Firefox Developer Edition: 93.0
Firefox Nightly: 101.0a1
Safari: 15.4
Safari Technology Preview: 15.4
npmPackages:
vite: ^2.9.1 => 2.9.1Used Package Manager
npm
Logs
> [email protected] start
> node-dev index.js
Server running at: http://localhost:3000. Use text-editor to change ./index.js, which will trigger node-dev to send SIGTERM and restart the server.
[INFO] 13:17:34 Restarting: /.../vite-testcase-middleware-mode-process-exit/index.js has been modified
#1: received SIGTERM, sleeping for 1s. After sleep, a 2nd console message should appear, starting with '#2:'
Server running at: http://localhost:3000. Use text-editor to change ./index.js, which will trigger node-dev to send SIGTERM and restart the server.Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.