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