From f1ac25809c480b8eaa0b99a0abd242abc00dfff6 Mon Sep 17 00:00:00 2001 From: Sebastian Law Date: Fri, 25 Feb 2022 22:48:27 -0800 Subject: [PATCH] [tasks] Fix behavior when task overruns interval In a scenario with `tasks.loop(seconds=5)`: The task takes 30 seconds to run on the first two iterations, and then is nearly instant for iterations afterward. The behavior should be that the task runs at: t = 0 (on time) t = 30 (late, should've been at t = 5) t = 60 (late, should've been at t = 10) t = 60 (late, should've been at t = 15) t = 60 (late, should've been at t = 20) t = 60 (late, should've been at t = 25) ... 6 more iterations t = 60 (on time) t = 65 (on time) In a scenario with a loop with explicit times set at UTC 1pm, 2pm, 3pm, 4pm, and 5pm: - The task takes 6 hour to run on the first iteration, and then is nearly instant for iterations afterward. Assuming the task is started at noon, the behavior should be that the task runs at `t = 0` and then at `t = 3600` 4 times ("catching up" on the missed iterations at 2pm, 3pm, 4pm, and 5pm). - The task takes 30 days to run on the first iteration, and then is nearly instant for iterations afterward. Assuming the task is started at noon, the behavior should be that the task runs at `t = 0` and then at `t = 43200` 149 times ("catching up" on the missed iterations for the past month). This behavior should be documented in the ext.tasks docs --- discord/ext/tasks/__init__.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/discord/ext/tasks/__init__.py b/discord/ext/tasks/__init__.py index 3f00cc548..84cc1eaeb 100644 --- a/discord/ext/tasks/__init__.py +++ b/discord/ext/tasks/__init__.py @@ -175,16 +175,10 @@ class Loop(Generic[LF]): raise await asyncio.sleep(backoff.delay()) else: - await self._try_sleep_until(self._next_iteration) - if self._stop_next_iteration: return - now = datetime.datetime.now(datetime.timezone.utc) - if now > self._next_iteration: - self._next_iteration = now - if self._time is not MISSING: - self._prepare_time_index(now) + await self._try_sleep_until(self._next_iteration) self._current_loop += 1 if self._current_loop == self.count: