Skip to content

createServer() keeps its file watcher open when configureServer or buildStart throws #23965

Description

@materwelonDhruv

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

  1. pnpm install
  2. pnpm repro starts a child process per hook. Each one calls createServer() 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

Activity

  1. sapphi-red commented on Nov 2, 2026

    @sapphi-red
    Member

    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.

  2. materwelonDhruv commented on Nov 2, 2026

    @materwelonDhruv
    ContributorAuthor

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

  3. sapphi-red commented on Nov 2, 2026

    @sapphi-red
    Member

    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.

    if (!config.experimental.bundledDev) {
    // For backward compatibility, we call buildStart for the client
    // environment when initing the server. For other environments
    // buildStart will be called when the first request is transformed
    await environments.client.pluginContainer.buildStart()
    }

  4. materwelonDhruv commented on Nov 2, 2026

    @materwelonDhruv
    ContributorAuthor

    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.

  5. sapphi-red commented on Nov 2, 2026

    @sapphi-red
    Member

    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.

  6. 0xABCD01 commented on Nov 6, 2026

    @0xABCD01

    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.

  7. materwelonDhruv commented on Nov 6, 2026

    @materwelonDhruv
    ContributorAuthor

    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.

  8. added
    p3-minor-bug
    feat: dev
    and removed on Nov 9, 2026
  9. added a commit that references this issue on Nov 19, 2026
    c732677
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: devp3-minor-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions