Skip to content

Commit

Permalink
fix(codexnode): ensure timer loop is asyncSpawned (#1038)
Browse files Browse the repository at this point in the history
* fix(codexnode): stop clock after validator stops

* fix(timer): ensure timer loop is asyncSpawned
  • Loading branch information
emizzle authored Dec 16, 2024
1 parent 6d415b0 commit 5f2ba14
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
6 changes: 3 additions & 3 deletions codex/node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions codex/utils/timer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

0 comments on commit 5f2ba14

Please sign in to comment.