Vite can hang the node.js process if server is closed during file transform #18224
Open
7 tasks done
Labels
p2-edge-case
Bug, but has workaround or limited in scope (priority)
Describe the bug
If
addWatchFile()
is called by a Vite plugin after the server is closed, the watcher will be set, preventing Node.js process from exiting.For example, if vite:css was in the middle of transforming a file when the server is closed, vite:css may call
addWatchFile()
, which will hang the process.Reproduction
https://github.com/maxpatiiuk/vite-watcher-hangs
Steps to reproduce
Clone this repository
Install dependencies
Run the Vite server
See that the Node.js process does not exit after the server is closed
vite.config.ts
and see that the server exists correctly as this way the watcher is set before the server is closed.System Info
Used Package Manager
npm
Logs
Logs without `--debug`
Logs with `--debug`
Explanation of the issue
Multiple built-in Vite plugins are setting watchers for files. For example,
vite:css
plugin:vite/packages/vite/src/node/plugins/css.ts
Line 384 in 0474550
These watchers are set even if
server.watcher.close()
was called.These watchers in turn prevent the Node.js process from exiting, hanging it.
Solutions:
server.close()
, if there are pending requests, after the requests are processed, close all watchers again if any were open.PluginContext.addWatchFile()
a noop after the watcher is closedReal-world example
I am starting Vite dev server inside Vitest global setup file. (Vite dev server is used for Puppeteer)
The Vite dev server is closed in the global teardown file.
If some test fails, the teardown is called early, while Vite dev server might still be in the process of transforming a CSS file.
The vite:css plugin may set a file watcher, prevent Vitest from exiting.
Vitest prints this message:
The
hanging-process
reporter points to file system watchers set by vite:css plugin.Validations
The text was updated successfully, but these errors were encountered: