Skip to content

Commit 79425a6

Browse files
committed
Fixed infinite loop when socket readline fails
In certain cases, the socket may not receive a full response (or any response), causing the while loop in readline to go on forever. After failing to receive any data, this fix will raise an exception and fail out.
1 parent 1fe7e85 commit 79425a6

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
@@ -85,11 +85,15 @@ def write(self, data): # pylint: disable=no-self-use
8585
def readline(self):
8686
"""Attempt to return as many bytes as we can up to but not including '\r\n'"""
8787
#print("Socket readline")
88+
stamp = time.monotonic()
8889
while b'\r\n' not in self._buffer:
8990
# there's no line already in there, read some more
9091
avail = min(_the_interface.socket_available(self._socknum), MAX_PACKET)
9192
if avail:
9293
self._buffer += _the_interface.socket_read(self._socknum, avail)
94+
elif time.monotonic() - stamp > self._timeout:
95+
self.close() # Make sure to close socket so that we don't exhaust sockets.
96+
raise RuntimeError("Didn't receive full response, failing out")
9397
firstline, self._buffer = self._buffer.split(b'\r\n', 1)
9498
gc.collect()
9599
return firstline

0 commit comments

Comments
 (0)