Skip to content

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

@lehni

Description

@lehni

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

> [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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions