# HG changeset patch # User Mads Kiilerich # Date 2015-12-25 11:37:49 # Node ID efce61aac33d492863e3c956328ed0e25be2527a # Parent 2bf5e8731154a0bec3a0d6ad0ee0e84664daff37 db: handle race when multiple threads add the same cache invalidation entry simultaneously (Issue #177) 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