Skip to content

Commit

Permalink
Correctly mark connection as broken on SSL error and don't crash loop…
Browse files Browse the repository at this point in the history
…_forever
  • Loading branch information
PierreF committed Jan 7, 2024
1 parent b1a7347 commit 492d799
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ v2.0.0 - 2023-xx-xx
Minimum tested version is Python 3.7
- 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
Expand Down
22 changes: 11 additions & 11 deletions src/paho/mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
sockpair_data = b"0"


class WebsocketConnectionError(ValueError):
class WebsocketConnectionError(ConnectionError):
pass


Expand Down Expand Up @@ -1747,7 +1747,7 @@ def loop_forever(self, timeout=1.0, max_packets=1, retry_first_connection=False)
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
Expand Down Expand Up @@ -1788,7 +1788,7 @@ def should_exit():
else:
try:
self.reconnect()
except (OSError, WebsocketConnectionError):
except OSError:
self._handle_on_connect_fail()
self._easy_log(
MQTT_LOG_DEBUG, "Connection failed, retrying")
Expand Down Expand Up @@ -2410,14 +2410,14 @@ def _packet_read(self):
command = self._sock_recv(1)
except BlockingIOError:
return MQTT_ERR_AGAIN
except ConnectionError as err:
self._easy_log(
MQTT_LOG_ERR, 'failed to receive on socket: %s', err)
return MQTT_ERR_CONN_LOST
except TimeoutError as err:
self._easy_log(
MQTT_LOG_ERR, 'timeout on socket: %s', err)
return MQTT_ERR_CONN_LOST
except OSError as err:
self._easy_log(
MQTT_LOG_ERR, 'failed to receive on socket: %s', err)
return MQTT_ERR_CONN_LOST
else:
if len(command) == 0:
return MQTT_ERR_CONN_LOST
Expand All @@ -2433,7 +2433,7 @@ def _packet_read(self):
byte = self._sock_recv(1)
except BlockingIOError:
return 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 MQTT_ERR_CONN_LOST
Expand Down Expand Up @@ -2463,7 +2463,7 @@ def _packet_read(self):
data = self._sock_recv(self._in_packet['to_process'])
except BlockingIOError:
return 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 MQTT_ERR_CONN_LOST
Expand Down Expand Up @@ -2513,7 +2513,7 @@ def _packet_write(self):
except BlockingIOError:
self._out_packet.appendleft(packet)
return 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)
Expand Down Expand Up @@ -4000,7 +4000,7 @@ def _recv_impl(self, length):
else:
raise BlockingIOError

except ConnectionError:
except OSError:
self.connected = False
return b''

Expand Down

0 comments on commit 492d799

Please sign in to comment.