How to handle exception in loop thread [message #1860015] |
Tue, 11 July 2023 02:09 |
Mike Riccio Messages: 1 Registered: July 2023 |
Junior Member |
|
|
My client's network connection was interrupted due to a switch rebooting, and my paho mqtt client stopped working as a result. I start the loop with loop_start(), and I can see that the loop thread got an exception:
Jul 10 03:42:58 Exception in thread Thread-1 (_thread_main):
Jul 10 03:42:58 Traceback (most recent call last):
Jul 10 03:42:58 File "/opt/homebrew/Cellar/python@3.11/3.11.4/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
Jul 10 03:42:58 self.run()
Jul 10 03:42:58 File "/opt/homebrew/Cellar/python@3.11/3.11.4/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 975, in run
Jul 10 03:42:58 self._target(*self._args, **self._kwargs)
Jul 10 03:42:58 File "/opt/homebrew/lib/python3.11/site-packages/paho/mqtt/client.py", line 3591, in _thread_main
Jul 10 03:42:58 self.loop_forever(retry_first_connection=True)
Jul 10 03:42:58 File "/opt/homebrew/lib/python3.11/site-packages/paho/mqtt/client.py", line 1756, in loop_forever
Jul 10 03:42:58 rc = self._loop(timeout)
Jul 10 03:42:58 ^^^^^^^^^^^^^^^^^^^
Jul 10 03:42:58 File "/opt/homebrew/lib/python3.11/site-packages/paho/mqtt/client.py", line 1164, in _loop
Jul 10 03:42:58 rc = self.loop_read()
Jul 10 03:42:58 ^^^^^^^^^^^^^^^^
Jul 10 03:42:58 File "/opt/homebrew/lib/python3.11/site-packages/paho/mqtt/client.py", line 1556, in loop_read
Jul 10 03:42:58 rc = self._packet_read()
Jul 10 03:42:58 ^^^^^^^^^^^^^^^^^^^
Jul 10 03:42:58 File "/opt/homebrew/lib/python3.11/site-packages/paho/mqtt/client.py", line 2370, in _packet_read
Jul 10 03:42:58 command = self._sock_recv(1)
Jul 10 03:42:58 ^^^^^^^^^^^^^^^^^^
Jul 10 03:42:58 File "/opt/homebrew/lib/python3.11/site-packages/paho/mqtt/client.py", line 640, in _sock_recv
Jul 10 03:42:58 return self._sock.recv(bufsize)
Jul 10 03:42:58 ^^^^^^^^^^^^^^^^^^^^^^^^
My code kept running after this, but it was no longer receiving any messages, so it was effectively broken at this point until I caught it.
How can I detect such an exception and force a reconnect attempt (or even abort the program which would trigger a restart)?
Thanks!
|
|
|
Powered by
FUDForum. Page generated in 0.03514 seconds