-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Open
Labels
Description
I have modified tests/multi_net/asyncio_tls_server_client.py
to run multiple tests (i.e. echo's) and noticed that after ~8 iterations tcp_client
crashes. Explicitly running the gc every ~6 iterations avoids the crash. It's also possible to trap the error, in which case the gc is apparently run automatically and the program will continue to operate.
Run on an esp32s3 with 8MBytes of psram and a recent micropython build (december).
My testing code:
import os
import asyncio
import ssl
import gc
HOSTNAME = "micropython.local"
PORT = 8000
FIX = False
cert = cafile = "/certs/rsa_cert.der"
key = "/certs/rsa_key.der"
async def handle_connection(reader, writer):
data = await reader.read(100)
print("handle_connection echo:", data)
writer.write(data)
await writer.drain()
writer.close()
await writer.wait_closed()
async def tcp_server():
server_ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
server_ctx.load_cert_chain(cert, key)
server = await asyncio.start_server(handle_connection, "0.0.0.0", PORT, ssl=server_ctx)
async def tcp_client():
# let server start first
await asyncio.sleep_ms(100)
client_ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
client_ctx.verify_mode = ssl.CERT_REQUIRED
client_ctx.load_verify_locations(cafile=cafile)
for i in range(20):
reader, writer = await asyncio.open_connection(IP, PORT, ssl=client_ctx, server_hostname=HOSTNAME)
message = f"msg #{i:3}".encode()
# required (after every ~ 8th turn)
if FIX and i % 6 == 0:
gc.collect()
print(f"------------------ collect mem_alloc={gc.mem_alloc()}")
writer.write(message)
await writer.drain()
data = await reader.read(100)
assert data == message
print("tcp_client read:", data)
async def main():
await asyncio.gather(tcp_server(), tcp_client())
print("main DONE")
asyncio.run(main())