do not crash polling when just unsubscribed

This commit is contained in:
Mark Spanbroek 2024-11-12 08:44:51 +01:00 committed by markspanbroek
parent c6a59b5187
commit f15d55f513
2 changed files with 18 additions and 19 deletions

View File

@ -171,25 +171,23 @@ proc new*(_: type JsonRpcSubscriptions,
discard discard
proc getChanges(id: JsonNode): Future[JsonNode] {.async: (raises: [CancelledError]).} = proc getChanges(id: JsonNode): Future[JsonNode] {.async: (raises: [CancelledError]).} =
try: if mappedId =? subscriptions.subscriptionMapping.?[id]:
let mappedId = subscriptions.subscriptionMapping[id] try:
let changes = await subscriptions.client.eth_getFilterChanges(mappedId) let changes = await subscriptions.client.eth_getFilterChanges(mappedId)
if changes.kind == JArray: if changes.kind == JArray:
return changes return changes
except KeyError as error: except JsonRpcError:
raiseAssert "subscription mapping invalid: " & error.msg await resubscribe(id)
except JsonRpcError: # TODO: we could still miss some events between losing the subscription
await resubscribe(id) # and resubscribing. We should probably adopt a strategy like ethers.js,
# TODO: we could still miss some events between losing the subscription # whereby we keep track of the latest block number that we've seen
# and resubscribing. We should probably adopt a strategy like ethers.js, # filter changes for:
# whereby we keep track of the latest block number that we've seen # https://github.com/ethers-io/ethers.js/blob/f97b92bbb1bde22fcc44100af78d7f31602863ab/packages/providers/src.ts/base-provider.ts#L977
# filter changes for: except CancelledError as error:
# https://github.com/ethers-io/ethers.js/blob/f97b92bbb1bde22fcc44100af78d7f31602863ab/packages/providers/src.ts/base-provider.ts#L977 raise error
except CancelledError as error: except CatchableError:
raise error # there's nothing we can do here
except CatchableError: discard
# there's nothing we can do here
discard
return newJArray() return newJArray()
proc poll(id: JsonNode) {.async: (raises: [CancelledError]).} = proc poll(id: JsonNode) {.async: (raises: [CancelledError]).} =

View File

@ -1,3 +1,4 @@
-d:"chronicles_log_level=INFO" -d:"chronicles_log_level=INFO"
-d:"json_rpc_websocket_package=websock" -d:"json_rpc_websocket_package=websock"
--warning[LockLevel]:off --warning[LockLevel]:off
--warning[DotLikeOps]:off