From 7aeac3cd1d05745137ba5ec36fb9e1157e6c8951 Mon Sep 17 00:00:00 2001 From: Eric <5089238+emizzle@users.noreply.github.com> Date: Fri, 13 Dec 2024 12:46:59 +1100 Subject: [PATCH] fix(timer): ensure timer loop is asyncSpawned --- codex/utils/timer.nim | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/codex/utils/timer.nim b/codex/utils/timer.nim index 9361d07b4..b01d95c66 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