Skip to content

Fix potential socket data read already into self._buffer #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions adafruit_esp32spi/adafruit_esp32spi_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discarded esp32spi_wsigiserver. Are there other circumstances where is could happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't checked any other libraries where _buffer is used, only https://github.com/adafruit/Adafruit_CircuitPython_WSGI/blob/main/adafruit_wsgi/esp32spi_wsgiserver.py

But i think is kinda of expected behavior and reasonable to use the _buffer, read as much as possible from the socket, because is not performant to read one bit at a time to find EOL for example.

This implementation also made into Adafruit_CircuitPython_Wiznet5k, adafruit_wiznet5k_socket _readline and adafruit_wiznet5k_wsgiserver so maybe is spread.

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