diff options
author | 2022-06-21 19:20:03 +0200 | |
---|---|---|
committer | 2022-06-21 19:28:18 +0200 | |
commit | 805c60437e50e2153d61e484872b207affd8db1f (patch) | |
tree | 9eef1367dc70336516dd093c26c1697557d65911 /botcore/utils/cooldown.py | |
parent | generalize handling of fully hashable args, and args with non-hashable parts (diff) |
stop cleanup task when manager is destroyed
Diffstat (limited to 'botcore/utils/cooldown.py')
-rw-r--r-- | botcore/utils/cooldown.py | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/botcore/utils/cooldown.py b/botcore/utils/cooldown.py index 59b0722e..099edba0 100644 --- a/botcore/utils/cooldown.py +++ b/botcore/utils/cooldown.py @@ -6,6 +6,7 @@ import asyncio import random import time import typing +import weakref from collections.abc import Awaitable, Hashable, Iterable from contextlib import suppress from dataclasses import dataclass @@ -106,6 +107,7 @@ class _CommandCooldownManager: self._periodical_cleanup(random.uniform(0, 10)), name="CooldownManager cleanup", ) + weakref.finalize(self, self.cleanup_task.cancel) def set_cooldown(self, channel: Hashable, call_arguments: Iterable[object]) -> None: """Set `call_arguments` arguments on cooldown in `channel`.""" @@ -145,11 +147,15 @@ class _CommandCooldownManager: Delete stale items every hour after waiting for `initial_delay`. The `initial_delay` ensures cleanups are not running for every command at the same time. + A strong reference to self is only kept while cleanup is running. """ + weak_self = weakref.ref(self) + del self + await asyncio.sleep(initial_delay) while True: await asyncio.sleep(60 * 60) - self._delete_stale_items() + weak_self()._delete_stale_items() def _delete_stale_items(self) -> None: """Remove expired items from internal collections.""" |