Skip to content

Commit 284ea42

Browse files
committed
pass timeout through to _sock_exact_recv when _socket_pool does not have timeout attribute
1 parent 926846c commit 284ea42

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ def loop(self, timeout: float = 0) -> Optional[list[int]]:
10041004
rcs = []
10051005

10061006
while True:
1007-
rc = self._wait_for_msg()
1007+
rc = self._wait_for_msg(timeout=timeout)
10081008
if rc is not None:
10091009
rcs.append(rc)
10101010
if time.monotonic() - stamp > timeout:
@@ -1013,11 +1013,13 @@ def loop(self, timeout: float = 0) -> Optional[list[int]]:
10131013

10141014
return rcs if rcs else None
10151015

1016-
def _wait_for_msg(self) -> Optional[int]:
1016+
def _wait_for_msg(self, timeout: Optional[float] = None) -> Optional[int]:
10171017
# pylint: disable = too-many-return-statements
10181018

10191019
"""Reads and processes network events.
10201020
Return the packet type or None if there is nothing to be received.
1021+
1022+
:param float timeout: return after this timeout, in seconds.
10211023
"""
10221024
# CPython socket module contains a timeout attribute
10231025
if hasattr(self._socket_pool, "timeout"):
@@ -1027,7 +1029,7 @@ def _wait_for_msg(self) -> Optional[int]:
10271029
return None
10281030
else: # socketpool, esp32spi
10291031
try:
1030-
res = self._sock_exact_recv(1)
1032+
res = self._sock_exact_recv(1, timeout=timeout)
10311033
except OSError as error:
10321034
if error.errno in (errno.ETIMEDOUT, errno.EAGAIN):
10331035
# raised by a socket timeout if 0 bytes were present
@@ -1093,7 +1095,9 @@ def _recv_len(self) -> int:
10931095
return n
10941096
sh += 7
10951097

1096-
def _sock_exact_recv(self, bufsize: int) -> bytearray:
1098+
def _sock_exact_recv(
1099+
self, bufsize: int, timeout: Optional[float] = None
1100+
) -> bytearray:
10971101
"""Reads _exact_ number of bytes from the connected socket. Will only return
10981102
bytearray with the exact number of bytes requested.
10991103
@@ -1104,6 +1108,7 @@ def _sock_exact_recv(self, bufsize: int) -> bytearray:
11041108
bytes is returned or trigger a timeout exception.
11051109
11061110
:param int bufsize: number of bytes to receive
1111+
:param float timeout: timeout, in seconds. Defaults to keep_alive
11071112
:return: byte array
11081113
"""
11091114
stamp = time.monotonic()
@@ -1115,7 +1120,7 @@ def _sock_exact_recv(self, bufsize: int) -> bytearray:
11151120
to_read = bufsize - recv_len
11161121
if to_read < 0:
11171122
raise MMQTTException(f"negative number of bytes to read: {to_read}")
1118-
read_timeout = self.keep_alive
1123+
read_timeout = timeout if timeout is not None else self.keep_alive
11191124
mv = mv[recv_len:]
11201125
while to_read > 0:
11211126
recv_len = self._sock.recv_into(mv, to_read)

tests/test_loop.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Loop(TestCase):
2121
INITIAL_RCS_VAL = 42
2222
rcs_val = INITIAL_RCS_VAL
2323

24-
def fake_wait_for_msg(self):
24+
def fake_wait_for_msg(self, timeout=None):
2525
"""_wait_for_msg() replacement. Sleeps for 1 second and returns an integer."""
2626
time.sleep(1)
2727
retval = self.rcs_val

0 commit comments

Comments
 (0)