viem
viem copied to clipboard
Unwatching and then rewatching block numbers causes duplication of numbers if the connection has failed before
Check existing issues
- [x] I checked there isn't already an issue for the bug I encountered.
Viem Version
2.37.6
Current Behavior
If you start watching for new block numbers, using a WebSocket transport, a PublicClient, and watchBlockNumber, and there is a connection failure and then the connection is restored, and after that you stop watching and then start watching again, you will receive duplicate numbers
Expected Behavior
The onBlockNumber callback receives the same number only once
Steps To Reproduce
-
createPublicClientusingwebSocket -
const unwatch = watchBlockNumber({ onBlockNumber: someCallback }); - Disconnect from the Internet
- Wait a couple of minutes
- Reconnect to the Internet and wait for a new block number to be received
-
unwatch() -
watchBlockNumber({ onBlockNumber: someOtherCallback }); -
someOtherCallbackwill now (incorrectly) be called twice for the same block numbers
Link to Minimal Reproducible Example
https://github.com/oskarlh/viemwatchbug
Anything else?
Node v22.17 on MacOS 15.6.1.
In an application I have observed more than just duplication, with the same number being received three or even four times. It seemed to happen more frequently when I connected through a VPN.