Skip to content

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

@oskarlh

Description

@oskarlh

Check existing issues

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

  1. createPublicClient using webSocket
  2. const unwatch = watchBlockNumber({ onBlockNumber: someCallback });
  3. Disconnect from the Internet
  4. Wait a couple of minutes
  5. Reconnect to the Internet and wait for a new block number to be received
  6. unwatch()
  7. watchBlockNumber({ onBlockNumber: someOtherCallback });
  8. someOtherCallback will 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions