From 022b97b5ce72dd3197c8fcbd4cc26e6be10d413b Mon Sep 17 00:00:00 2001 From: Eric <5089238+emizzle@users.noreply.github.com> Date: Fri, 8 Nov 2024 13:18:21 +1100 Subject: [PATCH] gracefully unsubscribe without errors During shutdown sequence, KeyErrors were being reported for subscriptions. We can safely ignore these if the subscription already doesn't exist. --- ethers/providers/jsonrpc/subscriptions.nim | 26 +++++++++++++--------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/ethers/providers/jsonrpc/subscriptions.nim b/ethers/providers/jsonrpc/subscriptions.nim index 9243f38..eeb1931 100644 --- a/ethers/providers/jsonrpc/subscriptions.nim +++ b/ethers/providers/jsonrpc/subscriptions.nim @@ -69,7 +69,7 @@ method subscribeLogs*(subscriptions: JsonRpcSubscriptions, method unsubscribe*(subscriptions: JsonRpcSubscriptions, id: JsonNode) - {.async, base.} = + {.async: (raises: [CancelledError]), base.} = raiseAssert "not implemented" method close*(subscriptions: JsonRpcSubscriptions) {.async, base.} = @@ -128,9 +128,15 @@ method subscribeLogs(subscriptions: WebSocketSubscriptions, method unsubscribe*(subscriptions: WebSocketSubscriptions, id: JsonNode) - {.async.} = - subscriptions.callbacks.del(id) - discard await subscriptions.client.eth_unsubscribe(id) + {.async: (raises: [CancelledError]).} = + try: + subscriptions.callbacks.del(id) + discard await subscriptions.client.eth_unsubscribe(id) + except CancelledError as e: + raise e + except KeyError, CatchableError: + # Ignore if uninstallation of the filter fails. + discard # Polling @@ -249,16 +255,16 @@ method subscribeLogs(subscriptions: PollingSubscriptions, method unsubscribe*(subscriptions: PollingSubscriptions, id: JsonNode) - {.async.} = - subscriptions.logFilters.del(id) - subscriptions.callbacks.del(id) - let sub = subscriptions.subscriptionMapping[id] - subscriptions.subscriptionMapping.del(id) + {.async: (raises: [CancelledError]).} = try: + subscriptions.logFilters.del(id) + subscriptions.callbacks.del(id) + let sub = subscriptions.subscriptionMapping[id] + subscriptions.subscriptionMapping.del(id) discard await subscriptions.client.eth_uninstallFilter(sub) except CancelledError as e: raise e - except CatchableError: + except KeyError, CatchableError: # Ignore if uninstallation of the filter fails. If it's the last step in our # cleanup, then filter changes for this filter will no longer be polled so # if the filter continues to live on in geth for whatever reason then it