# HG changeset patch # User Mads Kiilerich # Date 2016-01-05 16:23:22 # Node ID d5707598fd643601b8cc4a82e00f61aba0c674ab # Parent 2189802db18a01d15cff9650ab5dc8c31263f585 db: fix unknown exception type in commit error handling efce61aac33d was a blind fix. It failed because `from sqlalchemy import *` doesn't import exc and the new except clause would thus fail. It also failed because the session has to be rolled back after a commit failure. Now, rework it to fix these issues. Note that we are able to detect whether the commit failed for valid reasons ... but we can't use that information to much ... diff --git a/kallithea/model/db.py b/kallithea/model/db.py --- a/kallithea/model/db.py +++ b/kallithea/model/db.py @@ -34,6 +34,7 @@ import hashlib import collections import functools +import sqlalchemy from sqlalchemy import * from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.orm import relationship, joinedload, class_mapper, validates @@ -2136,19 +2137,22 @@ class CacheInvalidation(Base, BaseModel) return True inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar() - if not inv_obj: + if inv_obj is None: inv_obj = CacheInvalidation(cache_key, repo_name) - if inv_obj.cache_active: + elif inv_obj.cache_active: return True inv_obj.cache_active = True Session().add(inv_obj) try: Session().commit() - except exc.IntegrityError: + except sqlalchemy.exc.IntegrityError: + log.error('commit of CacheInvalidation failed - retrying') + Session().rollback() 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 + if inv_obj is None: + log.error('failed to create CacheInvalidation entry') + # TODO: fail badly? + # else: TOCTOU - another thread added the key at the same time; no further action required return False @classmethod