`createServer()` keeps its file watcher open when `configureServer` or `buildStart` throws
Describe the bug
My CLI starts a Vite dev server through the JS API. When a user's plugin threw in configureServer, the CLI printed the error and then just sat there. createServer() had rejected, but its file watcher was still open (and with @cloudflare/vite-plugin, workerd too), so the process never exited.
The same happens when a plugin throws in buildStart. A throw from config or configResolved exits fine. Looks like those run before the watcher starts. With server.watch: null all four exit.
Since createServer() rejects, the caller doesn't get a server to call close() on. For now my CLI adds an order: 'pre' plugin whose configureServer only grabs the server so it can close it if a later hook throws.
I'd expect createServer() to close whatever it already started before it rejects.
I can work on a fix for this if you're fine with that 😄
Reproduction
https://github.com/materwelonDhruv/repros/tree/main/vite/failed-start-keeps-watcher
Steps to reproduce
pnpm installpnpm reprostarts a child process per hook. Each one callscreateServer()with a plugin that throws from that hook and catches the rejection. Any child still running after 5s gets killed.
System Info
System:
OS: macOS 27.2
CPU: (12) arm64 Apple M2 Max
Memory: 178.78 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 26.13.0 - /Users/dhruv/.local/share/vite-plus/js_runtime/node/26.13.0/bin/node
Yarn: 4.18.1 - /Users/dhruv/.local/share/vite-plus/fallback-bin/yarn
npm: 11.20.0 - /Users/dhruv/.local/share/vite-plus/js_runtime/node/26.13.0/bin/npm
pnpm: 12.14.0 - /Users/dhruv/Library/pnpm/bin/pnpm
bun: 1.4.2 - /opt/homebrew/bin/bun
Deno: 2.9.6 - /opt/homebrew/bin/deno
Browsers:
Brave Browser: 151.1.93.137
Chrome: 156.0.8078.76
Firefox: 157.0.1
Safari: 27.2
npmPackages:
vite: 8.3.5 => 8.3.5
Used Package Manager
pnpm
Logs
Click to expand!
pnpm repro:
default watcher
config: exited
configResolved: exited
configureServer: still running after 5s
buildStart: still running after 5s
server.watch: null
config: exited
configResolved: exited
configureServer: exited
buildStart: exited
Validations
- [x] Follow our Code of Conduct
- [x] Read the Contributing Guidelines.
- [x] Read the docs.
- [x] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [x] 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 vuejs/core instead.
- [x] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [x] The provided reproduction is a minimal reproducible example of the bug.
I think this is expected (as long as the error is output in console with a non-slient logLevel. The user is expected to restart the server via r key in CLI or edit the config to fix the problem.
Hmm, the r key isn't there for this case though. Vite's cli exits with process.exit(1) when the first start fails, and the shortcuts only get bound once a server exists.
Editing the config does recover it through the js api, I tried it. But createServer() already rejected, so the caller has no reference to the server that comes up afterwards. It ends up listening on a port with nothing able to close it.
So either closing what it started on rejection, or resolving with the server so the caller can keep using it would work for me. I'd lean toward closing, since a rejected promise makes it look like "nothing's running".
Ah, that makes sense. Then, I think we should close the watcher for errors in configureServer.
For buildStart, I think we should keep the behavior so it aligns with the non-client environments and bundled-dev mode.
https://github.com/vitejs/vite/blob/90bc034a97e7eccd0f3cbc69904fffa625a4c34d/packages/vite/src/node/server/index.ts#L1122-L1127
I did NOT know you could link code snippets. Wow.
Also yea nice ok. I can PR the configureServer change.
For buildStart, I should have mentioned that my repro only uses middlewareMode: true, and as it turns out that's important lol. W/o middleware mode buildStart runs in listen so createServer has alrdy returned the server and the caller can close it. That case is fine then I think.
In middleware mode it runs inside createServer so it rejects and the caller doesn't get a server, same as configureServer. I get wanting it to match non-client environments and bundled dev, but in those the caller already has the server and can close it. Here they can't. So there needs to be different a behavior for middleware mode. Could the PR also close the watcher just for that path then? It wouldn't change listen. My code always runs in middleware mode so we come across this.
Yeah, I think closing in middleware mode makes sense. In the future, I hope we can fix the inconsistency once we remove this special behavior of unbundled client environment.
Hi @materwelonDhruv, are you still planning to open the PR for this? You and @sapphi-red scoped it on Nov 2: close the watcher when configureServer throws, and also when buildStart throws in middleware mode. I don't see a PR yet, so I wanted to check before touching it. Happy to leave it with you if you're still on it; if you're not able to get to it, I'd like to pick it up.
Hey @0xABCD01 yep. I'm still working on this. It's my first time contributing to Vite, so I'm taking some time to understand stuff. I'll open the PR in a day or two.