about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNakidai <nakidai@disroot.org>2024-10-29 23:40:19 +0300
committerNakidai <nakidai@disroot.org>2024-10-29 23:40:19 +0300
commit55ef8e5f692a1f7188263e861bc21cda2e8631e1 (patch)
treec7504693eabcc5cda03573f15312f8a597e49608
parentdadf4304f5a796db79f62ebecf08397436b7058f (diff)
downloadpetthecord-55ef8e5f692a1f7188263e861bc21cda2e8631e1.tar.gz
petthecord-55ef8e5f692a1f7188263e861bc21cda2e8631e1.zip
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 :>
-rw-r--r--src/petthecord/cache.py5
1 files changed, 4 insertions, 1 deletions
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)