Skip to content

Fix useStableSocket replacing socket on HMR effect re-runs#335

Merged
threepointone merged 3 commits intomainfrom
fix/hmr-socket-reconnect
Feb 20, 2026
Merged

Fix useStableSocket replacing socket on HMR effect re-runs#335
threepointone merged 3 commits intomainfrom
fix/hmr-socket-reconnect

Conversation

@deathbyknowledge
Copy link
Member

When Vite HMR fires, React Fast Refresh re-runs all effects. The old code unconditionally created a new socket instance, permanently killing the previous one (ReconnectingWebSocket sets _shouldReconnect=false on close). This broke downstream code holding references to the old socket.

Track the previous socketOptions reference to distinguish HMR re-runs (same reference) from real option changes (new reference). On HMR, call socket.reconnect() to preserve identity; on option changes, create a new socket as before.

Includes StrictMode-based tests that exercise the HMR code path.

When Vite HMR fires, React Fast Refresh re-runs all effects. The old
code unconditionally created a new socket instance, permanently killing
the previous one (ReconnectingWebSocket sets _shouldReconnect=false on
close). This broke downstream code holding references to the old socket.

Track the previous socketOptions reference to distinguish HMR re-runs
(same reference) from real option changes (new reference). On HMR,
call socket.reconnect() to preserve identity; on option changes,
create a new socket as before.

Includes StrictMode-based tests that exercise the HMR code path.
@changeset-bot
Copy link

changeset-bot bot commented Feb 19, 2026

🦋 Changeset detected

Latest commit: 6c47451

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
partysocket Patch

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

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 19, 2026

Open in StackBlitz

hono-party

npm i https://pkg.pr.new/cloudflare/partykit/hono-party@335

partyfn

npm i https://pkg.pr.new/cloudflare/partykit/partyfn@335

partyserver

npm i https://pkg.pr.new/cloudflare/partykit/partyserver@335

partysocket

npm i https://pkg.pr.new/cloudflare/partykit/partysocket@335

partysub

npm i https://pkg.pr.new/cloudflare/partykit/partysub@335

partysync

npm i https://pkg.pr.new/cloudflare/partykit/partysync@335

partytracks

npm i https://pkg.pr.new/cloudflare/partykit/partytracks@335

partywhen

npm i https://pkg.pr.new/cloudflare/partykit/partywhen@335

y-partyserver

npm i https://pkg.pr.new/cloudflare/partykit/y-partyserver@335

commit: 6c47451

pkg-pr-new publish with no arguments skips the private root package
and fails to auto-discover workspace packages, causing a 'No packages'
error on every PR. Pass the built package directories explicitly.
@deathbyknowledge deathbyknowledge marked this pull request as ready for review February 19, 2026 13:26
@alexander-zuev
Copy link

Validated on our side: this fixes the React StrictMode dev breakage for + (repro route: ). Preserving socket identity on effect re-run resolves the stale/replaced socket behavior we were seeing. We’re currently unblocked with a local patch and can drop it once this lands in a release.

Prevent automatic reconnect during HMR/StrictMode when the socket was created with startClosed: true by guarding the reconnect call. use-socket.ts now checks socketOptions.startClosed before calling socket.reconnect(), ensuring the socket stays CLOSED across React double-invokes. Tests in react-hooks.test.tsx were updated to assert the closed readyState for startClosed and to adjust assertions around socket identity and behavior under StrictMode.
@threepointone threepointone merged commit f288dd6 into main Feb 20, 2026
6 checks passed
@threepointone threepointone deleted the fix/hmr-socket-reconnect branch February 20, 2026 09:46
@github-actions github-actions bot mentioned this pull request Feb 9, 2026
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.

3 participants