Skip to content

fix(server): allow closing uninitialized DevEnvironment - #24184

Open
onmax wants to merge 1 commit into
vitejs:mainfrom
onmax:fix/devenvironment-close-before-init
Open

onmax wants to merge 1 commit into
vitejs:mainfrom
onmax:fix/devenvironment-close-before-init

Conversation

@onmax

@onmax onmax commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

DevEnvironment.close() rejects with <name> environment.pluginContainer called before initialized if init() failed or never ran. The pluginContainer getter throws while close() builds the array for Promise.allSettled, so depsOptimizer?.close() and hot.close() never run either.

Since #24020, createServer() hits this on startup failure. initEnvironments adds each environment to server.environments before awaiting environment.init(). When init() rejects, server.close() calls close() on that environment, Promise.allSettled swallows the rejection, and the transport stays open. I hit this directly in a CLI that creates an SSR environment with createEnvironment() and cleans up when init() fails. Because _initiated is already set, init() can't be retried and close() is the only cleanup path left.

This changes close() to call this._pluginContainer?.close(), matching BundledDev.close() which already guards this._devEngine?.close(). Initialized environments behave as before. #24167 proposed this guard along with waiting on in-flight init() calls before a bot closed it without review. I left out that wait and didn't reset _initiated on rejection so init() behavior stays unchanged, though I can add either if you prefer.

The new test in hooks.spec.ts fails on main and passes here. Standalone repro against 8.3.8 and main, run with pnpm install && pnpm verify: https://github.com/onmax/repros/tree/repro/vite-devenvironment-close-before-init/vite-devenvironment-close-before-init

Prepared with AI assistance for the repro and test. I checked the DevEnvironment.close() and initEnvironments paths myself and kept the fix to this guard.

@onmax onmax changed the title fix(server): close dev environments whose init did not complete fix(server): allow closing uninitialized DevEnvironment Nov 22, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant