# HG changeset patch # User Denis Laxalde # Date 1520242378 -3600 # Node ID 77a543e7878a64fd8369a2deb38cf277b1c08173 # Parent b0345b1e1e48e61436ca07013bd9b011597df7b0 [pyramid] Recreate CWSession entity when eid no longer exists As noted in comment, it might occur that we receive a "sessioneid" from a valid cookie while respective CWSession got dropped (typical case is db being recreated while users got preserved). In such case, we just recreate the CWSession entity as if it did not exist. diff -r b0345b1e1e48 -r 77a543e7878a cubicweb/pyramid/session.py --- a/cubicweb/pyramid/session.py Thu Feb 22 16:22:55 2018 +0100 +++ b/cubicweb/pyramid/session.py Mon Mar 05 10:32:58 2018 +0100 @@ -91,7 +91,10 @@ from pyramid.compat import pickle from pyramid.session import SignedCookieSessionFactory -from cubicweb import Binary +from cubicweb import ( + Binary, + UnknownEid, +) log = logging.getLogger(__name__) @@ -228,8 +231,16 @@ 'CWSession', cwsessiondata=data) sessioneid = session.eid else: - session = cnx.entity_from_eid(sessioneid) - session.cw_set(cwsessiondata=data) + try: + session = cnx.entity_from_eid(sessioneid) + except UnknownEid: + # Might occur if CWSession entity got dropped (e.g. + # the whole db got recreated) while user's cookie is + # still valid. We recreate the CWSession in this case. + sessioneid = cnx.create_entity( + 'CWSession', cwsessiondata=data).eid + else: + session.cw_set(cwsessiondata=data) cnx.commit() # Only if needed actually set the cookie