Repository navigation
Drop clients whose TLS handshake fails in accept() - #114
Merged
Merged
Conversation
On an HTTPS server, accept() also does the TLS handshake. When a client's handshake failed, for example because it sent plain HTTP, poll() raised the error and stopped the server. Now poll() drops that client and returns NO_REQUEST, so the next poll() serves the next client. Also rename _RETRYABLE_ERRNOS to _IGNORED_ERRNOS: poll() never retries the failed connection. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mikeysklar
approved these changes
Nov 19, 2026
mikeysklar
left a comment
Contributor
There was a problem hiding this comment.
Tested main vs this PR. With the PR, the server keeps serving after every bad client.
| Bad client | main | PR |
|---|---|---|
| Plain HTTP to :443 | raised -29184, server stopped |
dropped, next request ok |
| Cert rejected | ok (-30592 already ignored) |
ok |
| Garbage bytes | raised -29184, server stopped |
dropped, next request ok |
| WebSocket over plain HTTP | raised -29184, server stopped |
dropped, next request ok |
Metro ESP32-S2, CircuitPython 11.0.0-alpha.3 with adafruit/circuitpython#11878.
20 rounds of bad clients on the PR: no raise, no memory drift. Same result in CPython 3.14.
adafruit-adabot
pushed a commit
to adafruit/Adafruit_CircuitPython_Bundle
that referenced
this pull request
Nov 19, 2026
Updating https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground to 5.3.13 from 5.3.12: > Merge pull request adafruit/Adafruit_CircuitPython_CircuitPlayground#130 from dhalbert/cpb-detection Updating https://github.com/adafruit/Adafruit_CircuitPython_HTTPServer to 4.8.4 from 4.8.3: > Merge pull request adafruit/Adafruit_CircuitPython_HTTPServer#114 from dhalbert/ignore-tls-accept-errors Updating https://github.com/adafruit/Adafruit_CircuitPython_MatrixPortal to 3.2.13 from 3.2.12: > Merge pull request adafruit/Adafruit_CircuitPython_MatrixPortal#107 from dhalbert/uname-machine Updating https://github.com/adafruit/Adafruit_CircuitPython_PyBadger to 4.0.13 from 4.0.12: > Merge pull request adafruit/Adafruit_CircuitPython_PyBadger#76 from dhalbert/magtag-name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Diagnosis and code by Claude, reviewed by @dhalbert.
Problem
On an HTTPS server,
accept()also does the TLS handshake. When a client's handshake failed,Server.poll()raised the error, which stopped the user's program. But onlyMBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGEwas ignored.For example, a client that sent plain HTTP or
ws://to the HTTPS port caused the exceptionOSError: (-29184, 'MBEDTLS_ERR_SSL_INVALID_RECORD'). adafruit/circuitpython#10093 reported-30464(MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE) from the sameaccept()call.Fix
poll()now drops a client whose handshake fails inaccept()and returnsNO_REQUEST. So the nextpoll()serves the next client.Unfortunately, CircuitPython raises these errors differently than CPython: CircuitPython raises
OSErrorwith a negative mbedTLS error code; CPython raisesssl.SSLError.Renamed
_RETRYABLE_ERRNOSto_IGNORED_ERRNOS, because the name was misleading:poll()never retries the failed connection.Testing
ws://clients, and then still served HTTPS and WSS.This fix is related to adafruit/circuitpython#11878. Without that PR, CircuitPython leaves the failed client's socket open until it is garbage collected, so that client hangs instead of being reset. The server keeps running either way.