Skip to content

Conversation

@jxom
Copy link
Member

@jxom jxom commented Dec 15, 2025

Summary

Fixes #3957

When using watchBlockNumber (or other WebSocket subscriptions) and the connection fails and reconnects, calling unwatch() and then watch() again would cause duplicate callbacks for the same block numbers.

When eth_unsubscribe was called while the socket was disconnected, the
subscription was never removed from the subscriptions map because the
delete only happened in the response callback. On reconnect, the socket
would re-subscribe to these 'zombie' subscriptions, causing duplicate
callbacks.

The fix moves subscriptions.delete() to happen immediately when
eth_unsubscribe is called, before attempting to send the request.

Closes #3957
@changeset-bot
Copy link

changeset-bot bot commented Dec 15, 2025

🦋 Changeset detected

Latest commit: 1616a29

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

@jxom jxom merged commit 8f665cd into main Dec 15, 2025
19 of 32 checks passed
@jxom jxom deleted the fix/websocket-duplicate-subscriptions-on-reconnect branch December 15, 2025 03:27
@github-actions github-actions bot mentioned this pull request Dec 15, 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.

Unwatching and then rewatching block numbers causes duplication of numbers if the connection has failed before

2 participants