mtorpey / pypersist

Persistent memoisation framework for Python
https://pypersist.readthedocs.io
Other
6 stars 3 forks source link

it is a nice tool, but there is a bug when app stop /crashed when saving disk cash before removing the lock file #17

Open dwniu2000 opened 3 months ago

dwniu2000 commented 3 months ago

the lock file will be left there forever and cause dead loop, there is a simple fix import time import os def _check_stale_lock(lockfname, timeout=10): while os.path.exists(lockfname): lock_time = os.path.getmtime(lockfname) current_time = time.time() if current_time - lock_time > timeout: # 10 seconds logger.warning(f"Lock file {lockfname} is stale, removing it") os.remove(lockfname) return True time.sleep(0.1) return False

fix bug when dead lock file exists

class Cache_Ext(diskcache.Cache): def getitem(self, key): lockfname = self._key_to_fname(key, diskcache.LOCK) _check_stale_lock(lockfname) super().getitem(key)

diskcache.Cache=Cache_Ext

dwniu2000 commented 3 months ago

forgot put return return super().getitem(key)