Skip to content

Conversation

@alvrs
Copy link
Contributor

@alvrs alvrs commented Jul 16, 2025

fixes #3804

Observations before the PR

  • reconnectCount is incremented inside the setTimeout, so if onClose or onError is triggered before the timeout it will schedule another reconnect with the "old" reconnectCount value
  • onError and onClose may both call setup at the same time, leading to multiple sockets
  • onError closes the entire socketClient, which removes the socketClient from the socketClientCache, so the next time a socket client is requested, a fresh client is created (which also resets the reconnectCount). There is no limit to reconnection attempts or cooldown in this scenario.

Behavior before this change

CleanShot.2025-07-16.at.15.49.31.mp4

Changes implemented in this PR

  • Added a lock to avoid multiple simultaneous connection attempts (ie if onClose and onError are triggered shortly after each other)
  • Only close the socket, not the whole socketClient in reaction to an error, so we can still attempt a reconnect (without this the reconnection limit is not respected)
  • Consolidated the reconnection logic in a helper function
  • Always close the previous socket instance on reconnect to make sure there is only one open instance at a time. (This could also be moved back to onError since the socket should already be closed in onClose, but I find it easier to reason about if it's located with the reconnection logic. Not a strong opinion.)

Behavior with these changes

  • Only attempt to reconnect up to the limit (ie 5 in this recording), space out reconnection attempts by specified delay
  • Only one WebSocket connection to the target open at a time
CleanShot.2025-07-16.at.17.32.57.mp4

@changeset-bot
Copy link

changeset-bot bot commented Jul 16, 2025

🦋 Changeset detected

Latest commit: 72fad5b

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

This PR includes changesets to release 1 package
Name Type
viem 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

@vercel
Copy link

vercel bot commented Jul 16, 2025

@alvrs is attempting to deploy a commit to the Wevm Team on Vercel.

A member of the Team first needs to authorize it.

@alvrs alvrs changed the title [WIP] fix: websocket reconnection loop fix: websocket reconnection loop Jul 16, 2025
@alvrs alvrs marked this pull request as ready for review July 16, 2025 16:44
@jxom jxom merged commit 76140d5 into wevm:main Jul 18, 2025
2 of 3 checks passed
@github-actions github-actions bot mentioned this pull request Jul 18, 2025
@tmm tmm mentioned this pull request Oct 23, 2025
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.

WebSocket opens many connections in during and after reconnection

2 participants