From cb9b0f0c8bd220d29c3b2fb4f955fcd963ce8473 Mon Sep 17 00:00:00 2001 From: Markus Zolliker Date: Thu, 2 May 2024 09:02:04 +0200 Subject: [PATCH] frappy.client: avoid shutdown callback sent twice in case th rx thread crashes, the shutdown callback is called twice -> improve cleanup code in __rxthread Change-Id: I0d20aa4304d94b17565b018ad8528d61bbbcbc83 Reviewed-on: https://forge.frm2.tum.de/review/c/secop/frappy/+/33614 Tested-by: Jenkins Automated Tests Reviewed-by: Alexander Zaft Reviewed-by: Enrico Faulhaber Reviewed-by: Markus Zolliker --- frappy/client/__init__.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/frappy/client/__init__.py b/frappy/client/__init__.py index 4c076c24..c8c7ad7d 100644 --- a/frappy/client/__init__.py +++ b/frappy/client/__init__.py @@ -317,8 +317,11 @@ class SecopClient(ProxyClient): self.register_callback(None, self.handleError) def __del__(self): + # make sure threads are stopping. this is needed in case + # a frappy client object is lost without calling .disconnect() try: - self.callbacks.clear() # avoid callbacks when deleting. may cause deadlocks in NICOS + # avoid callbacks when deleting. may cause deadlocks in NICOS + self.callbacks.clear() self.disconnect(True) except Exception: pass @@ -405,6 +408,7 @@ class SecopClient(ProxyClient): def __rxthread(self): noactivity = 0 + shutdown = False try: while self._running: while self.cleanup: @@ -480,14 +484,13 @@ class SecopClient(ProxyClient): except ConnectionClosed: pass except Exception as e: - self._shutdown.set() + shutdown = True self.callback(None, 'handleError', e) finally: self._rxthread = None + self.disconnect(shutdown) if self._shutdown.is_set(): - self.disconnect(True) return - self.disconnect(False) if self.activate: self.log.info('try to reconnect to %s', self.uri) self._connthread = mkthread(self._reconnect)