SSL/TLS memory alloc error, but large free heap

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
enigmaniac
Posts: 1
Joined: Sat Jan 23, 2021 4:30 pm

SSL/TLS memory alloc error, but large free heap

Post by enigmaniac » Sat Jan 23, 2021 8:19 pm

Hi everyone,
New to micropython and hardly an expert in embedded systems, so please forgive my ignorance...

I'm hitting a strange recurring memory bug while running SSL on an ESP32 making regular client requests to a REST service (every 15 seconds)

At some point, I'll start getting alloc errors from the SSL module, usually
MBEDTLS_ERR_RSA_PUBLIC_FAILED+MBEDTLS_ERR_MPI_ALLOC_FAILED

But sometimes
MBEDTLS_ERR_X509_ALLOC_FAILED

This error might show up after 10 requests, or after 300. But once it starts to occur, it occurs on every single SSL request. If I pause requests for a while, things will (usually) start working again --- or a reset also fixes things.

Everything else in my program continues to work correctly: eg can still update my screenbuffer, read/write from the UART, use webrepl (though I do start getting more disconnects).

There's still plenty of heap memory available, and there doesn't seem to be any heap fragmentation issues -- I'm already running some proactive garbage collection after each request with gc.collect()

A quick search on the forum didn't turn up any similar issues, but has anyone else encountered this before?

My super ignorant guess:
It feels like the ssl module (or maybe socket more generally?) is building up a backlog of requests and/or has a memory leak. It has only allocated a specific amount of memory to itself, and so starts returning errors when that's full, even if there's more memory still available at the system level. Not sure why things start working again when I give it a rest, maybe some lower-level garbage collection / timeouts in the socket or ssl module start to free up memory?

Any suggestions for things in my own code which could be causing this, or ways to address (other than polling less frequently)? Or is this a lower-level problem with the ESP32 port?

Thanks for your help!

For reference, here's the code I'm using to make the request
https://github.com/enigmaniac/MicroWebCli

161616harvard
Posts: 5
Joined: Fri Feb 12, 2021 7:33 pm

Re: SSL/TLS memory alloc error, but large free heap

Post by 161616harvard » Mon Mar 01, 2021 2:05 pm

I have same MBEDTLS_ERR_RSA_PRIVATE_FAILED+MBEDTLS_ERR_MPI_ALLOC_FAILED' errors. Have you found an answer to this?

MatthiasP
Posts: 7
Joined: Mon Apr 19, 2021 7:31 pm

Re: SSL/TLS memory alloc error, but large free heap

Post by MatthiasP » Thu May 06, 2021 6:12 am

I am encountering the same problem. Which amount of memory is SSL/TLS expected to need?

PM-TPI
Posts: 75
Joined: Fri Jun 28, 2019 3:09 pm

Re: SSL/TLS memory alloc error, but large free heap

Post by PM-TPI » Thu Nov 17, 2022 12:59 pm

Same here

Post Reply