Skip to content

Commit ac4aee5

Browse files
authored
Merge pull request #42 from ZachNo/socket_hang_fix
Fixed infinite loop when socket readline fails
2 parents fce3410 + bf54a08 commit ac4aee5

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

adafruit_esp32spi/adafruit_esp32spi_socket.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,15 @@ def write(self, data): # pylint: disable=no-self-use
8787
def readline(self):
8888
"""Attempt to return as many bytes as we can up to but not including '\r\n'"""
8989
#print("Socket readline")
90+
stamp = time.monotonic()
9091
while b'\r\n' not in self._buffer:
9192
# there's no line already in there, read some more
9293
avail = min(_the_interface.socket_available(self._socknum), MAX_PACKET)
9394
if avail:
9495
self._buffer += _the_interface.socket_read(self._socknum, avail)
96+
elif self._timeout > 0 and time.monotonic() - stamp > self._timeout:
97+
self.close() # Make sure to close socket so that we don't exhaust sockets.
98+
raise RuntimeError("Didn't receive full response, failing out")
9599
firstline, self._buffer = self._buffer.split(b'\r\n', 1)
96100
gc.collect()
97101
return firstline

0 commit comments

Comments
 (0)