From 55ef8e5f692a1f7188263e861bc21cda2e8631e1 Mon Sep 17 00:00:00 2001 From: Nakidai Date: Tue, 29 Oct 2024 23:40:19 +0300 Subject: Possibly fix bug in GC When I was checking log of this bot I've noticed that thing in gc loop Traceback (most recent call last): File "/var/lib/ptc/venv/lib/python3.10/site-packages/discord/client.py", line 449, in _run_event await coro(*args, **kwargs) File "/var/lib/ptc/venv/lib/python3.10/site-packages/petthecord/runner.py", line 57, in on_ready await petter.gc_loop() File "/var/lib/ptc/venv/lib/python3.10/site-packages/petthecord/cache.py", line 107, in gc_loop del self._cache[filename.split('_')[0]] KeyError: '...' I don't really want to know why this error has occured, but IMO it's something related to broken index.json. So to fix the bug I just wrapped deleting action to try-except block which will ignore the KeyError exception. TBH looks like a kludge, but as I said I don't want to get into it :> --- src/petthecord/cache.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/petthecord/cache.py b/src/petthecord/cache.py index 1b4cd3a..2642a44 100644 --- a/src/petthecord/cache.py +++ b/src/petthecord/cache.py @@ -104,7 +104,10 @@ class CachedPet: if path.is_file() and filename != "index.json": if (time() - getmtime(path) > self._lifetime): self._logger.debug(f"Removing {filename}") - del self._cache[filename.split('_')[0]] + try: + del self._cache[filename.split('_')[0]] + except KeyError: + pass remove(path) with open(self._path / "index.json", "w") as f: dump(self._cache, f) -- cgit 1.4.1