Skip to content

Commit

Permalink
fix(timer): ensure timer loop is asyncSpawned
Browse files Browse the repository at this point in the history
  • Loading branch information
emizzle committed Dec 13, 2024
1 parent 63b36e4 commit fd5d158
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 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,6 +47,7 @@ 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:
Expand Down

0 comments on commit fd5d158

Please sign in to comment.