Repository navigation
Description
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.5Used 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: exitedValidations
- 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 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.
Activity
sapphi-red commentedon Nov 2, 2026
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 viarkey in CLI or edit the config to fix the problem.materwelonDhruv commentedon Nov 2, 2026
Hmm, the
rkey isn't there for this case though. Vite's cli exits withprocess.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".
sapphi-red commentedon Nov 2, 2026
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.vite/packages/vite/src/node/server/index.ts
Lines 1122 to 1127 in 90bc034
materwelonDhruv commentedon Nov 2, 2026
I did NOT know you could link code snippets. Wow.
Also yea nice ok. I can PR the
configureServerchange.For
buildStart, I should have mentioned that my repro only usesmiddlewareMode: true, and as it turns out that's important lol. W/o middleware modebuildStartruns inlistensocreateServerhas alrdy returned the server and the caller can close it. That case is fine then I think.In middleware mode it runs inside
createServerso it rejects and the caller doesn't get a server, same asconfigureServer. 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 changelisten. My code always runs in middleware mode so we come across this.sapphi-red commentedon Nov 2, 2026
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.
0xABCD01 commentedon Nov 6, 2026
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
configureServerthrows, and also whenbuildStartthrows 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.materwelonDhruv commentedon Nov 6, 2026
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.
fix: close server on failed start (fix vitejs#23965) (vitejs#24020)