fix: defer subscriber teardown in emit to prevent WebSocket subscription loss#5132
Open
tenderdeve wants to merge 1 commit into
Open
fix: defer subscriber teardown in emit to prevent WebSocket subscription loss#5132tenderdeve wants to merge 1 commit into
tenderdeve wants to merge 1 commit into
Conversation
…ion loss
When a once("block") listener fires and no listeners remain,
emit() immediately calls subscriber.stop(), sending eth_unsubscribe.
But waitForTransaction re-registers asynchronously after an await,
so the subscription is torn down before re-registration can happen.
Defer the cleanup by pollingInterval ms and recheck listener count
before stopping, giving once-listener callbacks time to re-register.
Explicit teardown via off() and removeAllListeners() is unaffected.
Fixes ethers-io#5121
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
WebSocketProvidersilently losesnewHeadssubscription afteronce("block")listener firesemit()immediately callssubscriber.stop()(which sendseth_unsubscribe) when listener count hits 0. ButwaitForTransactionre-registers asynchronously after anawait getTransactionReceipt(), so the subscription is already gone.pollingIntervalms using_setTimeout(), then recheck if listener count is still 0 before stopping. This givesonce-listener callbacks time to re-register.off()andremoveAllListeners()still stops subscribers immediatelydestroy()File:
src.ts/providers/abstract-provider.tsFixes #5121
Test plan
waitForTransactionworks correctly across multiple sequential callsonce("block")listener lifecycleprovider.destroy()cleans up all timers and subscriptionsoff("block")still triggers immediate unsubscribe