Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(codexnode): ensure timer loop is asyncSpawned #1038

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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()

Check warning on line 771 in codex/node.nim

View check run for this annotation

Codecov / codecov/patch

codex/node.nim#L770-L771

Added lines #L770 - L771 were not covered by tests
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
Loading