Skip to content

Commit 33441ca

Browse files
author
brentru
committed
add back retry loop
1 parent 42439eb commit 33441ca

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,20 +232,34 @@ def _get_socket(self, host, port, *, timeout=1):
232232
)[0]
233233

234234
sock = None
235-
sock = self._socket_pool.socket(addr_info[0], addr_info[1], addr_info[2])
235+
retry_count = 0
236+
while retry_count < 5 and sock is None:
237+
retry_count += 1
236238

237-
if port == 8883:
238-
sock = self._ssl_context.wrap_socket(sock, server_hostname=host)
239+
try:
240+
sock = self._socket_pool.socket(
241+
addr_info[0], addr_info[1], addr_info[2]
242+
)
243+
except OSError:
244+
continue
239245

240-
sock.settimeout(timeout)
241-
try:
242-
sock.connect((addr_info[-1][0], port))
243-
except MemoryError as err:
244-
sock.close()
245-
raise MemoryError from err
246-
except OSError as err:
247-
sock.close()
248-
raise OSError from err
246+
connect_host = addr_info[-1][0]
247+
if port == 8883:
248+
sock = self._ssl_context.wrap_socket(sock, server_hostname=host)
249+
connect_host = host
250+
sock.settimeout(timeout)
251+
252+
try:
253+
sock.connect((connect_host, port))
254+
except MemoryError:
255+
sock.close()
256+
sock = None
257+
except OSError:
258+
sock.close()
259+
sock = None
260+
261+
if sock is None:
262+
raise RuntimeError("Repeated socket failures")
249263

250264
self._backwards_compatible_sock = not hasattr(sock, "recv_into")
251265
return sock

0 commit comments

Comments
 (0)