changeset 7123014d260c in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset&node=7123014d260c
description:
Catch get_connection errors in Cache listener
issue11563
review411271003
diffstat:
trytond/cache.py | 26 ++++++++++++++++----------
1 files changed, 16 insertions(+), 10 deletions(-)
diffs (45 lines):
diff -r 65b62a8b02c3 -r 7123014d260c trytond/cache.py
--- a/trytond/cache.py Fri Sep 16 23:36:30 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]