From e4583c6116879200df5403882189bbed3065b81b Mon Sep 17 00:00:00 2001 From: Dorin Date: Mon, 15 Jan 2024 18:25:42 +0200 Subject: [PATCH 1/2] Fix potential socket data read already into self._buffer --- adafruit_esp32spi/adafruit_esp32spi_socket.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/adafruit_esp32spi/adafruit_esp32spi_socket.py b/adafruit_esp32spi/adafruit_esp32spi_socket.py index fa3a082..6ca0202 100644 --- a/adafruit_esp32spi/adafruit_esp32spi_socket.py +++ b/adafruit_esp32spi/adafruit_esp32spi_socket.py @@ -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() From e1197192651018e6aca8d843ea0c8d6415973b74 Mon Sep 17 00:00:00 2001 From: Dorin Date: Mon, 15 Jan 2024 18:59:34 +0200 Subject: [PATCH 2/2] Fix formatting --- adafruit_esp32spi/adafruit_esp32spi_socket.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi_socket.py b/adafruit_esp32spi/adafruit_esp32spi_socket.py index 6ca0202..70acbed 100644 --- a/adafruit_esp32spi/adafruit_esp32spi_socket.py +++ b/adafruit_esp32spi/adafruit_esp32spi_socket.py @@ -117,7 +117,9 @@ def recv_into(self, buffer, nbytes: int = 0): # 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] + 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:]