Repository navigation
Conversation
|
Install the latest version of pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/67aab10f4f4c2d74e983c31ac5652a782d852ba1Open in |
|
The analysis Vite server called `listen()`, which occupied port 5173 mid-build. That made Playwright's `webServer` readiness check pass before `vite build` had finished, and would clash with a running dev server. The module runner doesn't need a listening server, so use `middlewareMode` instead. Co-authored-by: Copilot App <[email protected]>
|
I investigated this failed test run, but the investigation came up empty. What I tried
The new analysis dev server temporarily binds the application's default port 5173 during |
…d, keeping the forked analyse worker's event loop alive and leaking a worker + dev server on every `vite build --watch` rebuild.
This commit fixes the issue reported at packages/kit/src/core/postbuild/analyse.js:176
## Bug
Commit `807451b` ("Update analyse.js") re-indented `analyse()` and, in doing so, dropped the `try { ... } finally { await vite_dev_server.close(); }` wrapper that previously surrounded the analysis body. As a result the dev server created by:
```js
const vite_dev_server = await vite.createServer(vite_config);
```
is never closed — neither on the success path nor on the several `throw new Error(...)` error paths inside the function.
### Why this leaks
`analyse` runs inside a worker via `forked(import.meta.url, analyse)` (`packages/kit/src/utils/fork.js`). After the worker posts its result, the parent only calls `worker.unref()` — it never calls `worker.terminate()`. `unref()` merely tells the parent not to wait for the worker; it does **not** stop it. So the worker only goes away if its own event loop drains and it exits on its own.
An open (unclosed) Vite dev server keeps open handles alive even with `middlewareMode: true, hmr: false, watch: null` (e.g. the dep optimizer / esbuild service). That keeps the worker's event loop alive, so the worker never exits.
For a one-shot `vite build` this is harmless (the main process exits shortly after). But in `vite build --watch`, `before_ssr_build_rerun` (`packages/kit/src/exports/vite/build/index.js`) calls `analyse()` on every rebuild, spawning a fresh worker each time. Since none of them exit, workers **and their dev servers accumulate across rebuilds** — a handle/memory leak.
### Empirical verification
I reproduced the exact configuration used by `analyse()` (`server: { middlewareMode: true, hmr: false, watch: null }`, `logLevel: 'silent'`) with Vite 8 and loaded a module via `ssrLoadModule`, using an **unref'd** watchdog timer so only the server's own handles could keep the process alive:
* **Without `close()`**: the process was still alive after 8 s and only exited when the watchdog fired → the server keeps the event loop alive (leak confirmed).
* **With `await server.close()`**: the process exited cleanly in ~48 ms.
This also refutes the concern that re-adding `close()` might reintroduce a hang in `middlewareMode`: `close()` completed in ~48 ms with no hang.
## Fix
Restored the `try { ... } finally { await vite_dev_server.close(); }` wrapper around the analysis body in `packages/kit/src/core/postbuild/analyse.js`, so the dev server is closed on both success and error paths. This lets each forked worker's event loop drain and the worker exit cleanly, preventing worker/dev-server accumulation across `vite build --watch` rebuilds. Verified with `node --check` and the empirical test above.
Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: teemingc <[email protected]>
| set_env(env); | ||
| async function analyse({ manifest_data, env, vite_config_file }) { | ||
| const vite = /** @type {typeof import('vite')} */ (await import_peer('vite', process.cwd())); | ||
| const vite_config = await load_vite_config(vite_config_file, vite, 'serve', { |
There was a problem hiding this comment.
Is it a problem that we're using a different command here? Wouldn't that cause problems with something like this?:
export default defineConfig(({ command }) => ({
plugins: [
sveltekit(),
command === 'build' && production_platform_plugin()
]
}));There was a problem hiding this comment.
Oh.. yeah, gonna have to rethink this one.
| set_read_implementation((file) => createReadableStream(from_fs(file))); | ||
|
|
||
| const config = extract_svelte_config(vite_config); | ||
| const manifest = generate_manifest( |
There was a problem hiding this comment.
Is it a problem for loading remote function modules that we don't load hooks.server.ts anymore? Conceivably you could use a query there, I think, and if we don't load it we wouldn't find it, right? We also don't load instrumentation.server.js but it would be insane to use anything there
There was a problem hiding this comment.
Yeah, I think it is a problem. Probably need to import hooks.server.ts so Vite discovers its imports
| // Preserve the original vite command when postbuild forks resolve a `serve` config. | ||
| // But, if the user is running vitest, we don't bother with this | ||
| if (config_env.mode !== 'test') { | ||
| process.env.SVELTEKIT_BUILD ??= s(config_env.command === 'build'); |
There was a problem hiding this comment.
Is it a problem that we don't unset this anywhere? I'm slightly worried that if someone's doing something with Vite programmatically, this could get set and then stay set in a subsequent step. I'm not sure concretely how that could possibly be a problem but... one way to fix it might be to do something like this instead of writing to SVELTEKIT_BUILD permanently?:
fork_analysis({
env: {
...process.env,
SVELTEKIT_BUILD: 'true'
}
});|
Oh also, if someone does something like this: import * as vite from 'vite';
await vite.build({ root: 'apps/shop' });
await vite.build({ root: 'apps/admin' });I think the first build would pollute the VERSION for the second build, because |
|
Ah right. Let's see if I can solve that in a separate PR.. |
…ite build Rolldown never runs the adapter resolveId for the cloudflare: protocol, so analyse/preview fail. Rewrite to the adapter Node stub until sveltejs/kit#16966 lands. Co-authored-by: Cursor <[email protected]>
…index on D1, bump kit next.27 vite beta.1 adapter-cloudflare next.7 and deps, type Platform.ctx, pin overrides, exclude vite from bun minimumReleaseAge, sync README (#70) * chore: bump better-auth 1.7.3, drop account.issuer and issuer unique index on D1, bump kit next.27 vite beta.1 adapter-cloudflare next.7 and deps, type Platform.ctx, pin overrides, exclude vite from bun minimumReleaseAge, sync README Co-authored-by: Cursor <[email protected]> * fix: read D1 from cloudflare:workers env after adapter-cloudflare dropped event.platform, flush Sentry via waitUntil, add account (provider_id, account_id) index, ambient workers types, sync README Co-authored-by: Cursor <[email protected]> * fix: stub cloudflare:workers in SSR output so Node/Bun can load the Vite build Rolldown never runs the adapter resolveId for the cloudflare: protocol, so analyse/preview fail. Rewrite to the adapter Node stub until sveltejs/kit#16966 lands. Co-authored-by: Cursor <[email protected]> --------- Co-authored-by: Coinnich <> Co-authored-by: Cursor <[email protected]>
This PR uses a Vite dev server to analyse the nodes rather than relying on the SSR build. Doing so allows us to:
cloudflare:workers)