From 4b3a7fbe165e9bd28af4677d143f27cf2f75179d Mon Sep 17 00:00:00 2001 From: Steve C Date: Sat, 9 May 2020 21:37:09 -0400 Subject: [PATCH] [tasks] Allow Loop.cancel in Loop.before_loop Task cancel raises on the next awaited coro, so I've added this 0-sleep "hack" I'm internally debating if leaving the comment there, but I'm sure it would confuse the uninformed of this trick. --- discord/ext/tasks/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/discord/ext/tasks/__init__.py b/discord/ext/tasks/__init__.py index 36cfe8104..833fc65a9 100644 --- a/discord/ext/tasks/__init__.py +++ b/discord/ext/tasks/__init__.py @@ -68,6 +68,7 @@ class Loop: sleep_until = discord.utils.sleep_until self._next_iteration = datetime.datetime.now(datetime.timezone.utc) try: + await asyncio.sleep(0) # allows canceling in before_loop while True: self._last_iteration = self._next_iteration self._next_iteration = self._get_next_sleep_time()