cubicweb/pyramid/session.py
branch3.26
changeset 12278 77a543e7878a
parent 12243 a46fb3f58ea2
child 12588 fa292e905edc
--- 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