diff --git a/kallithea/model/db.py b/kallithea/model/db.py --- a/kallithea/model/db.py +++ b/kallithea/model/db.py @@ -2142,7 +2142,13 @@ class CacheInvalidation(Base, BaseModel) return True inv_obj.cache_active = True Session().add(inv_obj) - Session().commit() + try: + Session().commit() + except exc.IntegrityError: + inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar() + if not inv_obj: + raise + # TOCTOU - another thread added the key at the same time; no further action required return False @classmethod