From 13c570d639503218924e9547f3b0043af5787883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Thu, 23 Mar 2023 17:36:07 +0100 Subject: [PATCH] session_db: reconnect if needed If the connection to the database fails when retrying a session operation, we end up with no cursore, which makes subsequent session operations fail. We fix this by ensuring we have cursor before any operations. --- session_db/pg_session_store.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/session_db/pg_session_store.py b/session_db/pg_session_store.py index 849e431de..5dc02fe0d 100644 --- a/session_db/pg_session_store.py +++ b/session_db/pg_session_store.py @@ -45,6 +45,7 @@ def with_cursor(func): while True: tries += 1 try: + self._ensure_connection() return func(self, *args, **kwargs) except (psycopg2.InterfaceError, psycopg2.OperationalError) as e: _logger.info("Session in DB connection Retry %s/5" % tries) @@ -67,6 +68,11 @@ class PGSessionStore(sessions.SessionStore): if self._cr is not None: self._cr.close() + @with_lock + def _ensure_connection(self): + if self._cr is None: + self._open_connection() + @with_lock def _open_connection(self): # return cursor to the pool