Skip to content

Close accepted client sockets on poll() early returns - #113

Merged
dhalbert merged 2 commits into
adafruit:mainfrom
sboettch:fix-poll-socket-leak-41
Nov 10, 2026
Merged

dhalbert merged 2 commits into
adafruit:mainfrom
sboettch:fix-poll-socket-leak-41

Conversation

@sboettch

Copy link
Copy Markdown

Fixes #41

Hello Adafruit CircuitPython maintainers, and thank you for maintaining Adafruit_CircuitPython_HTTPServer. I am a citizen applied data scientist working on WebHealth (an open public-interest security observatory), and while investigating issue #41 (Connection refused after some time/multiple requests on Wiznet5k Pico W5100S), I traced an accepted-socket leak on early return NO_REQUEST paths in Server.poll() and put together this focused fix.

Fixes:

  • Close the accepted conn socket inside Server.poll() (adafruit_httpserver/server.py, lines 481-533) before early return NO_REQUEST paths on EAGAIN, ECONNRESET, MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE, and SSLV3_ALERT_CERTIFICATE_UNKNOWN (adafruit_httpserver 5.0.0 on CircuitPython 9.2.1) so hardware socket pools (Wiznet5k W5100S/W5500 4-8 socket pools and Pico W socketpool) do not leak descriptors across aborted connections.
  • Break immediately when sock.recv_into(...) == 0 in _receive_header_bytes() and _receive_body_bytes() (adafruit_httpserver/server.py, lines 323-359, RFC 9112 Section 9.6) so a client closing its write stream before \r\n\r\n or before Content-Length bytes does not spin server.poll() until timeout.

Testing:

  • Verified conn.close() on EAGAIN / ECONNRESET / TLS-alert early returns and single-read exit on recv_into(...) == 0 half-closed streams across 6 offline unit vectors.

Thank you for reviewing, and please let me know if you would like any adjustments to the exception handling or test structure.

Warm regards,
Sophia Boettcher

sboettcher-ub313 and others added 2 commits November 9, 2026 23:37
Close `conn` before returning `NO_REQUEST` on `EAGAIN`, `ECONNRESET`,
`MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE`, and `SSLV3_ALERT_CERTIFICATE_UNKNOWN`
in `Server.poll()`, and break when `sock.recv_into(...) == 0` in
`_receive_header_bytes()` and `_receive_body_bytes()`.

Fixes adafruit#41

Signed-off-by: Sophia Boettcher <sboettcher@ub313.net>
Move the retryable errnos into a module-level tuple so the check
doesn't allocate a new tuple on every error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dhalbert

Copy link
Copy Markdown
Contributor

I refactored the error handling to do the tests all at once to avoid code duplication.

I put the errno values in a module-level tuple that is constructed only once (instead of on each check).

@dhalbert dhalbert left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! I refactored the tests to reduce the code size, but did not change the semantics.

@dhalbert
dhalbert merged commit 3d6c593 into adafruit:main Nov 10, 2026
1 check passed
adafruit-adabot pushed a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Nov 12, 2026
Updating https://github.com/adafruit/Adafruit_CircuitPython_HTTPServer to 4.8.3 from 4.8.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_HTTPServer#113 from sboettch/fix-poll-socket-leak-41

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Added the following libraries: Adafruit_CircuitPython_LT8912B
@sboettch

Copy link
Copy Markdown
Author

Thank you so much for the review and the cleanup.. i learned from the refactor 🌸

warmly, s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Connection refused after some time/multiple requests on Wiznet5k Pico W5100S

3 participants