diff options
| author | 2020-06-25 11:51:19 -0700 | |
|---|---|---|
| committer | 2020-06-25 11:51:19 -0700 | |
| commit | e09307e0f8f570279271c99525e0cde6cfa84d5b (patch) | |
| tree | 4070ace775dd87ccbfdc8894d1db88616a4e3da5 | |
| parent | Scheduler: remove duplicate dict delete (diff) | |
Scheduler: only close unawaited coroutines
The coroutine may cancel the scheduled task, which would also trigger
the finally block. The coroutine isn't necessarily finished when it
cancels the task, so it shouldn't be closed in this case.
| -rw-r--r-- | bot/utils/scheduling.py | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index 4110598d5..cf2a1f110 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -1,5 +1,6 @@ import asyncio import contextlib +import inspect import logging import typing as t from datetime import datetime @@ -87,8 +88,11 @@ class Scheduler: finally: # Close it to prevent unawaited coroutine warnings, # which would happen if the task was cancelled during the sleep. - self._log.trace("Explicitly closing the coroutine.") - coroutine.close() + # Only close it if it's not been awaited yet. This check is important because the + # coroutine may cancel this task, which would also trigger the finally block. + if inspect.getcoroutinestate(coroutine) == "CORO_CREATED": + self._log.trace("Explicitly closing the coroutine.") + coroutine.close() def _task_done_callback(self, task_id: t.Hashable, done_task: asyncio.Task) -> None: """ |