changeset a87d1206c5cc in trytond:6.4
details: https://hg.tryton.org/trytond?cmd=changeset&node=a87d1206c5cc
description:
        Catch get_connection errors in Cache listener

        issue11563
        review411271003
        (grafted from 7123014d260c278bd36a25d9c4a7543db553733c)
diffstat:

 trytond/cache.py |  26 ++++++++++++++++----------
 1 files changed, 16 insertions(+), 10 deletions(-)

diffs (45 lines):

diff -r a1b42a36e1f7 -r a87d1206c5cc trytond/cache.py
--- a/trytond/cache.py  Thu Sep 15 21:49:12 2022 +0200
+++ b/trytond/cache.py  Sat Sep 17 19:00:48 2022 +0200
@@ -348,16 +348,20 @@
 
     @classmethod
     def _listen(cls, dbname):
-        database = backend.Database(dbname)
-        if not database.has_channel():
-            raise NotImplementedError
+        current_thread = threading.current_thread()
+        pid = os.getpid()
 
-        logger.info("listening on channel '%s' of '%s'", cls._channel, dbname)
-        conn = database.get_connection(autocommit=True)
-        pid = os.getpid()
-        selector = selectors.DefaultSelector()
-        current_thread = threading.current_thread()
+        conn, selector = None, None
         try:
+            database = backend.Database(dbname)
+            if not database.has_channel():
+                raise NotImplementedError
+
+            logger.info(
+                "listening on channel '%s' of '%s'", cls._channel, dbname)
+            conn = database.get_connection(autocommit=True)
+            selector = selectors.DefaultSelector()
+
             cursor = conn.cursor()
             cursor.execute('LISTEN "%s"' % cls._channel)
             current_thread.listening = True
@@ -380,8 +384,10 @@
                 "cache listener on '%s' crashed", dbname, exc_info=True)
             raise
         finally:
-            selector.close()
-            database.put_connection(conn)
+            if selector:
+                selector.close()
+            if conn:
+                database.put_connection(conn)
             with cls._listener_lock[pid]:
                 if cls._listener.get((pid, dbname)) == current_thread:
                     del cls._listener[pid, dbname]

Reply via email to