fix: cleanup resources aggresively on vite-plugin#12583
fix: cleanup resources aggresively on vite-plugin#12583
Conversation
🦋 Changeset detectedLatest commit: 334aa84 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
2ef269c to
081c544
Compare
|
Claude finished @anonrig's task —— View job Changeset ReviewReviewing changeset files... Tasks:
✅ All changesets look good The changeset
|
create-cloudflare
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-shared
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-pool-workers
@cloudflare/workers-editor-shared
@cloudflare/workers-utils
wrangler
commit: |
081c544 to
d74ff9d
Compare
d74ff9d to
23fbf99
Compare
| const cleanupWebSocket = handleWebSocket( | ||
| viteDevServer.httpServer, | ||
| ctx.miniflare, | ||
| entryWorkerName | ||
| ); | ||
| viteDevServer.httpServer.once("close", () => { | ||
| cleanupWebSocket(); | ||
| hotListenerCleanup?.(); | ||
| hotListenerCleanup = undefined; | ||
| void disposeRemoteProxySessions(); | ||
| }); |
There was a problem hiding this comment.
🔴 WebSocket upgrade handler accumulates on each server restart without cleanup
On every Vite dev server restart, configureServer is called again, and handleWebSocket registers a new upgrade handler on the same httpServer (line 174). However, the returned cleanupWebSocket function is only invoked inside httpServer.once("close", ...) (line 179), which only fires when the HTTP server is fully shut down — not on restart. Vite reuses the same HTTP server across restarts.
Root Cause and Impact
The PR correctly introduces cleanup for hotListenerCleanup at the top of the configureServer block (lines 109-110) using a persisted closure variable:
hotListenerCleanup?.();
hotListenerCleanup = undefined;But no analogous pattern exists for the WebSocket handler. The cleanupWebSocket reference is a local variable inside configureServer and is lost on each subsequent call. After N restarts, there are N active upgrade handlers on the same httpServer, each calling miniflare.dispatchFetch for every incoming WebSocket upgrade request.
Impact: Each WebSocket upgrade request from a client will be processed by all accumulated handlers, potentially causing duplicate connections, wasted resources, and unexpected behavior. This directly undermines the PR's goal of aggressive resource cleanup.
Prompt for agents
In packages/vite-plugin-cloudflare/src/plugins/dev.ts, add a persisted cleanup variable for the WebSocket handler, similar to how hotListenerCleanup is managed. Specifically: 1. Add a `let webSocketCleanup: (() => void) | undefined;` declaration next to `hotListenerCleanup` around line 42. 2. At the beginning of the `if (viteDevServer.httpServer)` block (around line 173), call `webSocketCleanup?.()` to clean up the previous handler before registering a new one. 3. After calling `handleWebSocket(...)` at line 174, store the returned cleanup function: `webSocketCleanup = cleanupWebSocket;`. This mirrors the pattern used for hotListenerCleanup at lines 109-110.
Was this helpful? React with 👍 or 👎 to provide feedback.
An attempt to cleanup resources more aggresively on vite-plugin.
A picture of a cute animal (not mandatory, but encouraged)