From 5f2ba1428195fd4bb67cfb20b7b886fec9bab758 Mon Sep 17 00:00:00 2001 From: Eric <5089238+emizzle@users.noreply.github.com> Date: Mon, 16 Dec 2024 11:24:57 +0700 Subject: [PATCH] fix(codexnode): ensure timer loop is asyncSpawned (#1038) * fix(codexnode): stop clock after validator stops * fix(timer): ensure timer loop is asyncSpawned --- codex/node.nim | 6 +++--- codex/utils/timer.nim | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/codex/node.nim b/codex/node.nim index f180fd62..a43c9270 100644 --- a/codex/node.nim +++ b/codex/node.nim @@ -763,12 +763,12 @@ proc stop*(self: CodexNodeRef) {.async.} = if hostContracts =? self.contracts.host: await hostContracts.stop() - if not self.clock.isNil: - await self.clock.stop() - if validatorContracts =? self.contracts.validator: await validatorContracts.stop() + if not self.clock.isNil: + await self.clock.stop() + if not self.networkStore.isNil: await self.networkStore.close diff --git a/codex/utils/timer.nim b/codex/utils/timer.nim index 9361d07b..b01d95c6 100644 --- a/codex/utils/timer.nim +++ b/codex/utils/timer.nim @@ -30,13 +30,13 @@ proc new*(T: type Timer, timerName = "Unnamed Timer"): Timer = ## Create a new Timer intance with the given name Timer(name: timerName) -proc timerLoop(timer: Timer) {.async.} = +proc timerLoop(timer: Timer) {.async: (raises: []).} = try: while true: await timer.callback() await sleepAsync(timer.interval) except CancelledError: - raise + discard # do not propagate as timerLoop is asyncSpawned except CatchableError as exc: error "Timer caught unhandled exception: ", name=timer.name, msg=exc.msg @@ -47,9 +47,10 @@ method start*(timer: Timer, callback: TimerCallback, interval: Duration) {.base. timer.callback = callback timer.interval = interval timer.loopFuture = timerLoop(timer) + asyncSpawn timer.loopFuture method stop*(timer: Timer) {.async, base.} = - if timer.loopFuture != nil: + if timer.loopFuture != nil and not timer.loopFuture.finished: trace "Timer stopping: ", name=timer.name await timer.loopFuture.cancelAndWait() timer.loopFuture = nil