makes sure that a key on subscriptionMapping exists before trying to access it

This commit is contained in:
Marcin Czenko 2024-11-27 22:57:51 +01:00
parent d88e4614b1
commit 4642545309
2 changed files with 22 additions and 12 deletions

View File

@ -285,15 +285,16 @@ method unsubscribe*(subscriptions: PollingSubscriptions,
{.async.} = {.async.} =
subscriptions.logFilters.del(id) subscriptions.logFilters.del(id)
subscriptions.callbacks.del(id) subscriptions.callbacks.del(id)
let sub = subscriptions.subscriptionMapping[id] if subscriptions.subscriptionMapping.hasKey(id):
subscriptions.subscriptionMapping.del(id) let sub = subscriptions.subscriptionMapping[id]
try: subscriptions.subscriptionMapping.del(id)
discard await subscriptions.client.eth_uninstallFilter(sub) try:
except CancelledError as e: discard await subscriptions.client.eth_uninstallFilter(sub)
raise e except CancelledError as e:
except CatchableError: raise e
# Ignore if uninstallation of the filter fails. If it's the last step in our except CatchableError:
# cleanup, then filter changes for this filter will no longer be polled so # Ignore if uninstallation of the filter fails. If it's the last step in our
# if the filter continues to live on in geth for whatever reason then it # cleanup, then filter changes for this filter will no longer be polled so
# doesn't matter. # if the filter continues to live on in geth for whatever reason then it
discard # doesn't matter.
discard

View File

@ -50,6 +50,15 @@ template subscriptionTests(subscriptions, client) =
await sleepAsync(100.millis) await sleepAsync(100.millis)
check count == 0 check count == 0
test "unsubscribing from a non-existent subscription does not do any harm":
await subscriptions.unsubscribe(newJInt(0))
test "duplicate unsubscribe is harmless":
proc callback(blck: Block) = discard
let subscription = await subscriptions.subscribeBlocks(callback)
await subscriptions.unsubscribe(subscription)
await subscriptions.unsubscribe(subscription)
test "stops listening to new blocks when provider is closed": test "stops listening to new blocks when provider is closed":
var count = 0 var count = 0
proc callback(blck: ?!Block) = proc callback(blck: ?!Block) =