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

When running in middleware mode, vite should not kill process on SIGTERM #7627

Closed
7 tasks done
lehni opened this issue Apr 6, 2022 · 2 comments · Fixed by #8550
Closed
7 tasks done

When running in middleware mode, vite should not kill process on SIGTERM #7627

lehni opened this issue Apr 6, 2022 · 2 comments · Fixed by #8550
Labels
enhancement New feature or request

Comments

@lehni
Copy link

lehni commented Apr 6, 2022

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:

exitProcess = async () => {
try {
await server.close()
} finally {
process.exit(0)
}
}
process.once('SIGTERM', exitProcess)

Reproduction

https://github.com/lehni/vite-testcase-middleware-mode-process-exit

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.1

Used Package Manager

npm

Logs

> vite-testcase-middleware-mode-process-exit@1.0.0 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

lehni added a commit to ditojs/dito that referenced this issue Apr 6, 2022
lehni added a commit to ditojs/dito that referenced this issue Apr 6, 2022
@haoqunjiang haoqunjiang added enhancement New feature or request and removed pending triage labels Apr 25, 2022
@bluwy
Copy link
Member

bluwy commented May 25, 2022

This make sense to me, and maybe we can document this too. Feel free to send over a PR.

@lehni
Copy link
Author

lehni commented May 25, 2022

@bluwy thanks for the reply. I'm not familiar enough with the internals to know the right path forward, so not really comfortable with the idea of addressing this by myself. I just wanted to raise the issue so it's on the radar.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants