-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Possible memory leak in WiFiClientSecure on failed client.connect() #3808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
An update, the fix causes a panic if Moving calls to log_v("Performing the SSL/TLS handshake...");
unsigned long handshake_start_time=millis();
while ((ret = mbedtls_ssl_handshake(&ssl_client->ssl_ctx)) != 0) {
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
// ++++++++++ ADDED TO FIX MEMORY LEAK ON FAILED CONNECTION ++++++++++
if (cli_key != NULL) mbedtls_pk_free(&ssl_client->client_key);
if (rootCABuff != NULL) mbedtls_x509_crt_free(&ssl_client->ca_cert);
if (cli_cert != NULL) mbedtls_x509_crt_free(&ssl_client->client_cert);
// ++++++++++ END ++++++++++
return handle_error(ret);
}
if((millis()-handshake_start_time)>ssl_client->handshake_timeout) {
// ++++++++++ ADDED TO FIX MEMORY LEAK ON FAILED CONNECTION ++++++++++
if (cli_key != NULL) mbedtls_pk_free(&ssl_client->client_key);
if (rootCABuff != NULL) mbedtls_x509_crt_free(&ssl_client->ca_cert);
if (cli_cert != NULL) mbedtls_x509_crt_free(&ssl_client->client_cert);
// ++++++++++ END ++++++++++
return -1;
}
vTaskDelay(10 / portTICK_PERIOD_MS);
} |
[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions. |
[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions. |
The issue open on 12 Mar |
This is still present in the current release. I am seeing it in normal operation on ESP32s with constrained throughput where the SSL handshake is taking longer than expected. |
based on code at espressif#3808
@brokentoaster did the fix above solve your leak? |
Hardware:
Board: ESP32 Dev Module (DevKitC / WROOM-32)
Core Installation version: 1.0.4
IDE name: Arduino IDE
Flash Frequency: 80Mhz
PSRAM enabled: no
Upload Speed: 921600
Computer OS: Mac OSX
Description:
I believe I've found a source of a memory leak in WiFiClientSecure, that occurs only when a
client.connect()
fails, with root_ca, key, and certificates having been set.Test Setup:
We're chasing a memory leak that occurs only when a call to
client.connect()
fails. To simulate this, we'll force the handshake to fail by setting the timeout to 0. Then, we'll constantly free, re-allocate, and call connect on aWiFiClientSecure
object viadelete
andnew
. We'll watch for heap usage as we do this.Observations:
Theory:
Deleting the object exercises the destructor
WiFiClientSecure::~WiFiClientSecure()
, which callsWiFiClientSecure::stop()
, which callsstop_ssl_socket()
inssl_client.cpp
.I believe
stop_ssl_socket()
fails to free the certificates, and these calls are missing:mbedtls_x509_crt_free(&ssl_client->ca_cert);
mbedtls_x509_crt_free(&ssl_client->client_cert);
mbedtls_pk_free(&ssl_client->client_key);
My Fix:
Testing with this modified function body for
stop_ssl_socket()
inssl_client.cpp
, solves the memory leak problem, at least in my testing, heap usage stabilizes at <2k.Test Sketch:
The text was updated successfully, but these errors were encountered: