Skip to content

Commit ecd1ea0

Browse files
committed
Fix reseting the timer while it is being waited on
If the timer was reset while it was being waited on, the next tick time was not recalculated and the timer would wait for the original time instead of the new one. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 2b0ee1a commit ecd1ea0

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/frequenz/channels/timer.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,10 +696,15 @@ async def ready(self) -> bool: # noqa: DOC502
696696

697697
now = self._now()
698698
time_to_next_tick = self._next_tick_time - now
699+
699700
# If we didn't reach the tick yet, sleep until we do.
700-
if time_to_next_tick > 0:
701+
# We need to do this in a loop also reacting to the reset event, as the timer
702+
# could be reset while we are sleeping, in which case we need to recalculated
703+
# the time to the next tick and try again.
704+
while time_to_next_tick > 0:
701705
await asyncio.sleep(time_to_next_tick / 1_000_000)
702706
now = self._now()
707+
time_to_next_tick = self._next_tick_time - now
703708

704709
# If a stop was explicitly requested during the sleep, we bail out.
705710
if self._stopped:

0 commit comments

Comments
 (0)