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