Skip to content

Commit 5b135d6

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 edf2b37 commit 5b135d6

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/frequenz/channels/util/_timer.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,10 +652,15 @@ async def ready(self) -> bool:
652652

653653
now = self._now()
654654
time_to_next_tick = self._next_tick_time - now
655+
655656
# If we didn't reach the tick yet, sleep until we do.
656-
if time_to_next_tick > 0:
657+
# We need to do this in a loop also reacting to the reset event, as the timer
658+
# could be reset while we are sleeping, in which case we need to recalculated
659+
# the time to the next tick and try again.
660+
while time_to_next_tick > 0:
657661
await asyncio.sleep(time_to_next_tick / 1_000_000)
658662
now = self._now()
663+
time_to_next_tick = self._next_tick_time - now
659664

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

0 commit comments

Comments
 (0)