Skip to content

Commit

Permalink
Fix potential socket data read already into self._buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
us3r64 committed Jan 15, 2024
1 parent c69b6f2 commit e4583c6
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions adafruit_esp32spi/adafruit_esp32spi_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ def recv_into(self, buffer, nbytes: int = 0):
num_to_read = len(buffer) if nbytes == 0 else nbytes
num_read = 0
while num_to_read > 0:
# we might have read socket data into the self._buffer with:
# esp32spi_wsgiserver: socket_readline
if len(self._buffer) > 0:
bytes_to_read = min(num_to_read, len(self._buffer))
buffer[num_read : num_read + bytes_to_read] = self._buffer[:bytes_to_read]
num_read += bytes_to_read
num_to_read -= bytes_to_read
self._buffer = self._buffer[bytes_to_read:]
# explicitly recheck num_to_read to avoid extra checks
continue

num_avail = self._available()
if num_avail > 0:
last_read_time = time.monotonic()
Expand Down

0 comments on commit e4583c6

Please sign in to comment.