diff --git a/src/paho/mqtt/client.py b/src/paho/mqtt/client.py index 0f4d7ae2..37afde58 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 @@ -4419,7 +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) + try: + self.loop_forever(retry_first_connection=True) + finally: + self._thread = None def _reconnect_wait(self) -> None: # See reconnect_delay_set for details