diff --git a/ChangeLog.txt b/ChangeLog.txt index e077f23b..e0b00e65 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -15,6 +15,8 @@ v2.0.0 - 2023-xx-xx - MQTTMessage field "dup" and "retain" used to be integer with value 0 and 1. They are now boolean. - Add on_pre_connect() callback, which is called immediately before a connection attempt is made. +- Correctly mark connection as broken on SSL error and don't crash loop_forever. + Closes #750. v1.6.1 - 2021-10-21 diff --git a/src/paho/mqtt/client.py b/src/paho/mqtt/client.py index e5a0811d..b22c173b 100644 --- a/src/paho/mqtt/client.py +++ b/src/paho/mqtt/client.py @@ -239,7 +239,7 @@ class _OutPacket(TypedDict): _socket = socket -class WebsocketConnectionError(ValueError): +class WebsocketConnectionError(ConnectionError): pass @@ -1896,7 +1896,7 @@ def loop_forever( if self._state == mqtt_cs_connect_async: try: self.reconnect() - except (OSError, WebsocketConnectionError): + except OSError: self._handle_on_connect_fail() if not retry_first_connection: raise @@ -1937,7 +1937,7 @@ def should_exit() -> bool: else: try: self.reconnect() - except (OSError, WebsocketConnectionError): + except OSError: self._handle_on_connect_fail() self._easy_log( MQTT_LOG_DEBUG, "Connection failed, retrying") @@ -2601,14 +2601,14 @@ def _packet_read(self) -> MQTTErrorCode: command = self._sock_recv(1) except BlockingIOError: return MQTTErrorCode.MQTT_ERR_AGAIN - except ConnectionError as err: - self._easy_log( - MQTT_LOG_ERR, 'failed to receive on socket: %s', err) - return MQTTErrorCode.MQTT_ERR_CONN_LOST except TimeoutError as err: self._easy_log( MQTT_LOG_ERR, 'timeout on socket: %s', err) return MQTTErrorCode.MQTT_ERR_CONN_LOST + except OSError as err: + self._easy_log( + MQTT_LOG_ERR, 'failed to receive on socket: %s', err) + return MQTTErrorCode.MQTT_ERR_CONN_LOST else: if len(command) == 0: return MQTTErrorCode.MQTT_ERR_CONN_LOST @@ -2623,7 +2623,7 @@ def _packet_read(self) -> MQTTErrorCode: byte = self._sock_recv(1) except BlockingIOError: return MQTTErrorCode.MQTT_ERR_AGAIN - except ConnectionError as err: + except OSError as err: self._easy_log( MQTT_LOG_ERR, 'failed to receive on socket: %s', err) return MQTTErrorCode.MQTT_ERR_CONN_LOST @@ -2653,7 +2653,7 @@ def _packet_read(self) -> MQTTErrorCode: data = self._sock_recv(self._in_packet['to_process']) except BlockingIOError: return MQTTErrorCode.MQTT_ERR_AGAIN - except ConnectionError as err: + except OSError as err: self._easy_log( MQTT_LOG_ERR, 'failed to receive on socket: %s', err) return MQTTErrorCode.MQTT_ERR_CONN_LOST @@ -2704,7 +2704,7 @@ def _packet_write(self) -> MQTTErrorCode: except BlockingIOError: self._out_packet.appendleft(packet) return MQTTErrorCode.MQTT_ERR_AGAIN - except ConnectionError as err: + except OSError as err: self._out_packet.appendleft(packet) self._easy_log( MQTT_LOG_ERR, 'failed to receive on socket: %s', err) @@ -4296,7 +4296,7 @@ def _recv_impl(self, length: int) -> bytes: else: raise BlockingIOError - except ConnectionError: + except OSError: self.connected = False return b''