Repository navigation
Open
adafruit/Adafruit_CircuitPython_HTTPServer
#114Labels
Milestone
Description
CircuitPython version and board name
Adafruit CircuitPython 9.2.4 on 2025-01-29; Wemos Lolin C3 Mini with ESP32-C3FH4Code/REPL
import wifi
import socketpool
import asyncio
from adafruit_httpserver import Server, Request, Response, Websocket, GET
pool = socketpool.SocketPool(wifi.radio)
server = Server(
pool,
# root_path="/static",
https=True,
certfile="cert.pem",
keyfile="key.pem",
debug=True)
websocket: Websocket = None
@server.route("/ws", GET)
def connect_client(request: Request):
global websocket # pylint: disable=global-statement
if websocket is not None:
websocket.close() # Close any existing connection
websocket = Websocket(request)
return websocket
async def handle_http_requests():
while True:
server.poll()
await asyncio.sleep_ms(0)
async def handle_websocket_requests():
while True:
if websocket is not None:
if (data := websocket.receive(fail_silently=True)) is not None:
print(data)
await asyncio.sleep_ms(0)
async def send_websocket_messages():
while True:
if websocket is not None:
websocket.send_message("aaaa", fail_silently=True)
await asyncio.sleep_ms(1000)
server.start(str(wifi.radio.ipv4_address))
async def main():
await asyncio.gather(
asyncio.create_task(handle_websocket_requests()),
asyncio.create_task(send_websocket_messages()),
asyncio.create_task(handle_http_requests())
)
asyncio.run(main())Behavior
When HTTPS is enabled (by providing certificate and key files), static content is served correctly; however, WebSocket connections fail. Testing with a C# Watson WebSocket client produces the error:
Traceback (most recent call last):
File "adafruit_httpserver/server.py", line 476, in poll
OSError: -30464
Traceback (most recent call last):
File "code.py", line 101, in <module>
File "asyncio/core.py", line 317, in run
File "asyncio/core.py", line 276, in run_until_complete
File "asyncio/core.py", line 261, in run_until_complete
File "code.py", line 98, in main
File "asyncio/funcs.py", line 162, in gather
File "asyncio/core.py", line 261, in run_until_complete
File "code.py", line 39, in handle_http_requests
File "adafruit_httpserver/server.py", line 528, in poll
File "adafruit_httpserver/server.py", line 476, in poll
OSError: -30464
This issue disappears when HTTPS is disabled (i.e., when the certificate-related parameters are commented out).
server = Server(
pool,
# https=True,
# certfile="cert.pem",
# keyfile="key.pem",
debug=True)Expected Behavior:
WebSocket connections should be established successfully regardless of whether HTTPS is enabled, while still serving static content securely.
Actual Behavior:
With HTTPS enabled, WebSocket connections fail with the error OSError: -30464 during the polling process.
Additional Context:
- The issue appears to be isolated to WebSocket functionality when using HTTPS.
- The rest of the HTTP functionality (e.g., serving static content) is unaffected.
- This behavior was reproduced on a CircuitPython environment using the adafruit_httpserver library.
Activity
dhalbert commentedon Jun 17, 2026
This is an old bug. I'd like to re-test it with recent builds. Do you have a simple client, or shell commands, that could test it? Thanks.
berkut0 commentedon Jun 30, 2026
Re-tested on Adafruit QT Py ESP32-C3, CircuitPython 10.2.1 (2026-05-13).
Observation
On the original report (ESP32-C3, HTTPS enabled), the server appeared to start and serve static content, but a WebSocket client caused
OSError: -30464insideserver.poll(), which took down the wholeasyncioloop.On my board with CP 10.2.1, enabling HTTPS no longer gets that far:
server.start()raisesMemoryErrorin_create_server_socketbefore the server is running. So on this hardware the failure happens early, as a clear out-of-memory error, instead of a later TLS/WebSocket crash that stops the entire program. That seems like an improvement in how the failure is surfaced, even though HTTPS + WebSocket is still not usable on ESP32-C3.I could not reproduce
-30464here because HTTPS does not start on this board. That error may still apply on boards with enough RAM to bring up the TLS server (e.g. ESP32-S3).What works: HTTP +
ws://(no TLS).Server (
code.pyon device):Client (PC —
pip install websockets):Run:
Expected:
recv: aaaa, thensent: ping.berkut0 commentedon Jun 30, 2026
@dhalbert — follow-up with a minimal HTTPS / WSS repro setup for boards that have enough RAM (e.g. ESP32-S3). On ESP32-C3,
server.start()still raisesMemoryErrorbefore TLS comes up.Board
ESP32-S3 (or similar). ESP32-C3 cannot start the HTTPS server.
Device files
Generate certs (host PC):
Copy
cert.pemandkey.pemto the device root.Server (
code.py)Same as original issue report, plus a
/route for a simple HTTPS GET check. Listens on port 5000 (library default forserver.start(host)— not 443).PC test script
pip install websockets, then save astest_https_wss.py:Run (port 5000 is the default if omitted):
Expected results
https://<ip>:5000/status: 200,body: HTTPS OKwss://<ip>:5000/wsrecv: aaaaOSError: -30464inserver.poll()On ESP32-C3:
MemoryErroratserver.start()(HTTPS never starts).dhalbert commentedon Jun 30, 2026
@berkut0 Thanks! Could you test with 10.3.0-alpha.3? There are some changes since 10.2.1 that might relate to this.
berkut0 commentedon Jun 30, 2026
Tested 10.3.0-alpha.3 on my custom ESP32-C3 board (Adafruit QT Py ESP32-C3 firmware). No change vs 10.2.1: same
MemoryErrorinserver.start()→_create_server_socket.ws://still works.The original failure mode from this issue (HTTPS up, then
OSError: -30464inserver.poll()on WSS, taking down the whole program) no longer happens here — it's replaced by a clearMemoryErrorbefore the server starts. That's a better outcome on ESP32-C3.It seems to me it might make sense to close #10093 for the C3 case with that note, or perhaps consider a separate issue documenting that ESP32-C3 doesn't have enough RAM for HTTPS/WSS — though you'd probably want to verify on ESP32-S3 first before treating the issue as resolved, which I can't do right now (I'm focused on the C line in general).
Happy to follow whatever you think fits best.
dhalbert commentedon Jun 30, 2026
It might be possible to adjust the network parameters for the C3 to prevent the memory error; then it would fail in the same way that the S3 is failing.
Just to confirm, that is this issue, is that what you meant? I would leave it open for the non
MemoryErrorcase. A separate issue would be to see if the params can be adjusted.berkut0 commentedon Jul 2, 2026
Yes, I meant this issue, but I agree it probably should stay open for the non-
MemoryErrorcase.My current impression from testing on ESP32-C3 is that the original behavior described here is no longer reproduced on recent builds: instead of HTTPS starting and then WSS failing later with
OSError: -30464inserver.poll(), the failure now happens earlier and more clearly as aMemoryErrorduringserver.start().So for the C3 case, this feels like a much better failure mode. My guess is that the original report may have been mostly an ESP32-C3-specific low-memory issue, but I may be wrong — I haven't had time to check this on higher-memory boards yet. My expectation is that on those boards it probably just works.
A separate issue for trying to tune the network/SSL parameters on ESP32-C3 sounds reasonable to me.
Thanks a lot for looking into this.