Skip to content

Drop clients whose TLS handshake fails in accept() - #114

Merged
dhalbert merged 1 commit into
adafruit:mainfrom
dhalbert:ignore-tls-accept-errors
Nov 19, 2026
Merged

dhalbert merged 1 commit into
adafruit:mainfrom
dhalbert:ignore-tls-accept-errors

Conversation

@dhalbert

Copy link
Copy Markdown
Contributor

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 only MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE was ignored.

For example, a client that sent plain HTTP or ws:// to the HTTPS port caused the exception OSError: (-29184, 'MBEDTLS_ERR_SSL_INVALID_RECORD'). adafruit/circuitpython#10093 reported -30464 (MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE) from the same accept() call.

Fix

  • poll() now drops a client whose handshake fails in accept() and returns NO_REQUEST. So the next poll() serves the next client.

    Unfortunately, CircuitPython raises these errors differently than CPython: CircuitPython raises OSError with a negative mbedTLS error code; CPython raises ssl.SSLError.

  • Renamed _RETRYABLE_ERRNOS to _IGNORED_ERRNOS, because the name was misleading: poll() never retries the failed connection.

Testing

  • Metro ESP32-S3, CircuitPython 11.0.0-alpha.3 plus adafruit/circuitpython#11878: the HTTPS/WSS example from adafruit/circuitpython#10093 kept running after plain HTTP and ws:// clients, and then still served HTTPS and WSS.
  • CPython 3.14: after a plain HTTP client was dropped, the server still answered an HTTPS request.

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.

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>
@dhalbert
dhalbert requested a review from a team November 14, 2026 19:36

@mikeysklar mikeysklar 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.

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.

@dhalbert
dhalbert merged commit 032b068 into adafruit:main Nov 19, 2026
1 check passed
@dhalbert
dhalbert deleted the ignore-tls-accept-errors branch November 19, 2026 03:16
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
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.

WebSocket Fails with HTTPS Enabled: OSError -30464 in adafruit_httpserver

2 participants