From 7ef35925bb08c8a395044c6eb40b49d36e157094 Mon Sep 17 00:00:00 2001 From: jiachin1995 Date: Fri, 2 Feb 2024 12:32:17 +0800 Subject: [PATCH 1/3] Fix loop_stop() not clearing ._thread when called from same thread When loop_stop is called from within on_message(), ._thread will not be set to None. This will cause subsequent loop_start() to return an error. --- src/paho/mqtt/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/paho/mqtt/client.py b/src/paho/mqtt/client.py index 0f4d7ae2..c0269fd9 100644 --- a/src/paho/mqtt/client.py +++ b/src/paho/mqtt/client.py @@ -2274,7 +2274,7 @@ def loop_stop(self) -> MQTTErrorCode: self._thread_terminate = True if threading.current_thread() != self._thread: self._thread.join() - self._thread = None + self._thread = None return MQTTErrorCode.MQTT_ERR_SUCCESS From 0053dc1098b1cb313ec5475c2b5d66369e66e0b9 Mon Sep 17 00:00:00 2001 From: jiachin1995 Date: Mon, 25 Mar 2024 01:18:32 +0800 Subject: [PATCH 2/3] Update client.py --- src/paho/mqtt/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/paho/mqtt/client.py b/src/paho/mqtt/client.py index c0269fd9..b193ac40 100644 --- a/src/paho/mqtt/client.py +++ b/src/paho/mqtt/client.py @@ -2274,7 +2274,6 @@ def loop_stop(self) -> MQTTErrorCode: self._thread_terminate = True if threading.current_thread() != self._thread: self._thread.join() - self._thread = None return MQTTErrorCode.MQTT_ERR_SUCCESS @@ -4420,6 +4419,7 @@ def _handle_on_connect_fail(self) -> None: def _thread_main(self) -> None: self.loop_forever(retry_first_connection=True) + self._thread = None def _reconnect_wait(self) -> None: # See reconnect_delay_set for details From 7a0d1846b84e394dbb843076e69bee3d9b913103 Mon Sep 17 00:00:00 2001 From: jiachin1995 Date: Mon, 25 Mar 2024 09:12:16 +0800 Subject: [PATCH 3/3] Update client.py --- src/paho/mqtt/client.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/paho/mqtt/client.py b/src/paho/mqtt/client.py index b193ac40..37afde58 100644 --- a/src/paho/mqtt/client.py +++ b/src/paho/mqtt/client.py @@ -4418,8 +4418,10 @@ def _handle_on_connect_fail(self) -> None: MQTT_LOG_ERR, 'Caught exception in on_connect_fail: %s', err) def _thread_main(self) -> None: - self.loop_forever(retry_first_connection=True) - self._thread = None + try: + self.loop_forever(retry_first_connection=True) + finally: + self._thread = None def _reconnect_wait(self) -> None: # See reconnect_delay_set for details