[vite-plugin] Fix flaky WebSocket upgrade test and harden upgrade handler - #14862
Merged
Conversation
🦋 Changeset detectedLatest commit: b7b386c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
workers-devprod
requested review from
a team and
dario-piotrowicz
and removed request for
a team
July 26, 2026 08:45
Contributor
|
Codeowners approval required for this PR:
Show detailed file reviewers |
Contributor
|
✅ All changesets look good |
Contributor
|
Review submitted successfully to PR #14862. Summary of my review:
|
@cloudflare/autoconfig
@cloudflare/config
create-cloudflare
@cloudflare/deploy-helpers
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-shared
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-pool-workers
@cloudflare/workers-auth
@cloudflare/workers-editor-shared
@cloudflare/workers-utils
wrangler
commit: |
…dler The `handleWebSocket` upgrade listener is `async` but did not guard `miniflare.dispatchFetch`. When Miniflare is disposed mid-upgrade (dev server shutdown/restart) the rejection escaped as an unhandled rejection, which can terminate the process and leaks the client socket. Wrap the handler body in try/catch and destroy the socket on failure. Also deflake `websockets.spec.ts` on Windows CI: `listen()` now awaits `miniflare.ready` so `workerd` cold start is charged to `testTimeout` rather than the 1s `vi.waitFor` budget; Miniflare is created per-test via a helper (no throwaway spawn); and `afterEach` tears down tracked sockets before closing the server so a failed assertion can't cascade into a hook timeout. Extend the shared vitest config to pick up `retry`/`hookTimeout`.
Co-authored-by: ask-bonk[bot] <249159057+ask-bonk[bot]@users.noreply.github.com>
petebacondarwin
force-pushed
the
fix/vite-plugin-websocket-upgrade-flake
branch
from
July 27, 2026 09:57
e14c534 to
b7b386c
Compare
emily-shen
approved these changes
Jul 27, 2026
workers-devprod
approved these changes
Jul 27, 2026
workers-devprod
left a comment
Contributor
There was a problem hiding this comment.
Codeowners reviews satisfied
Merged
This was referenced Aug 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Deflakes the
@cloudflare/vite-pluginWebSocket upgrade test on Windows CI, and fixes a latent unhandled-rejection bug it exposed.Observed flake: Tests (Windows, packages-and-tools) —
handleWebSocket > does not forward framing headers from the Worker response:AssertionError: expected '' to contain 'HTTP/1.1 101'Error: Hook timed out in 10000ms(afterEach)MiniflareCoreError [ERR_DISPOSED]unhandled rejectionRoot cause:
listen()never awaitedminiflare.ready, so a coldworkerdboot was charged againstvi.waitFor's 1s default timeout instead of the 50s test timeout. On the contended Windows runner the boot exceeded 1s, so no bytes arrived. The test then threw before itssocket.destroy(), sohttpServer.close()hung on the live socket — and because this package's vitest config didn't extend the shared one,hookTimeoutfell back to the 10s default (and there was noretrysafety net). Finally,afterEach'sdispose()rejected the still-pendingdispatchFetch, and theasyncupgrade handler had notry/catch, surfacing theERR_DISPOSEDunhandled rejection.Changes:
src/websockets.ts(real fix): wrap theupgradehandler intry/catchand destroy the socket on failure. Without this, a rejecteddispatchFetchduring dev-server shutdown/restart escapes theasynclistener as an unhandled rejection (can terminate the process) and leaks the client socket. This path is reachable in realvite dev/vite preview, not just tests.src/__tests__/websockets.spec.ts:listen()awaitsminiflare.ready(deterministic fix); Miniflare created per-test via a helper (no throwaway spawn);afterEachtears down tracked sockets and callscloseAllConnections()before closing the server so a failed assertion can't cascade into a hook timeout; explicit{ timeout: 10_000 }on the two real round-trip waits; added a regression test asserting no unhandled rejection whendispatchFetchfails.vitest.config.ts: extendvitest.shared.tsto inheritretry: 1,hookTimeout/teardownTimeout, etc.Verification:
websockets.spec.ts9/9 pass; full package suite 179/179 across 18 files (confirmsrestoreMocks: truefrom the shared config regresses nothing); type-check, lint, and format clean. Removing thetry/catchmakes the new regression test hang the process, confirming it catches the bug.Note
This is a contribution from an AI agent: OpenCode, anthropic/claude-opus-4-8.