Skip to content

Commit f717b44

Browse files
committed
tests if key exists in subscriptionMapping when unsubscribing
1 parent 1ae2cd4 commit f717b44

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

ethers/providers/jsonrpc/subscriptions.nim

+13-12
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,16 @@ method unsubscribe*(subscriptions: PollingSubscriptions,
252252
{.async.} =
253253
subscriptions.logFilters.del(id)
254254
subscriptions.callbacks.del(id)
255-
let sub = subscriptions.subscriptionMapping[id]
256-
subscriptions.subscriptionMapping.del(id)
257-
try:
258-
discard await subscriptions.client.eth_uninstallFilter(sub)
259-
except CancelledError as e:
260-
raise e
261-
except CatchableError:
262-
# Ignore if uninstallation of the filter fails. If it's the last step in our
263-
# cleanup, then filter changes for this filter will no longer be polled so
264-
# if the filter continues to live on in geth for whatever reason then it
265-
# doesn't matter.
266-
discard
255+
if subscriptions.subscriptionMapping.hasKey(id):
256+
let sub = subscriptions.subscriptionMapping[id]
257+
subscriptions.subscriptionMapping.del(id)
258+
try:
259+
discard await subscriptions.client.eth_uninstallFilter(sub)
260+
except CancelledError as e:
261+
raise e
262+
except CatchableError:
263+
# Ignore if uninstallation of the filter fails. If it's the last step in our
264+
# cleanup, then filter changes for this filter will no longer be polled so
265+
# if the filter continues to live on in geth for whatever reason then it
266+
# doesn't matter.
267+
discard

testmodule/providers/jsonrpc/testJsonRpcSubscriptions.nim

+9
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ template subscriptionTests(subscriptions, client) =
4848
discard await client.call("evm_mine", newJArray())
4949
await sleepAsync(100.millis)
5050
check count == 0
51+
52+
test "unsubscribing from a non-existent subscription does not do any harm":
53+
await subscriptions.unsubscribe(newJInt(0))
54+
55+
test "duplicate unsubscribe is harmless":
56+
proc callback(blck: Block) = discard
57+
let subscription = await subscriptions.subscribeBlocks(callback)
58+
await subscriptions.unsubscribe(subscription)
59+
await subscriptions.unsubscribe(subscription)
5160

5261
test "stops listening to new blocks when provider is closed":
5362
var count = 0

0 commit comments

Comments
 (0)