esp-tls mbedtls - leakage of open sockets when connection to host failed (IDFGH-8378) #9847
Closed
3 tasks done
Labels
Resolution: Done
Issue is done internally
Status: Done
Issue is done internally
Type: Bug
bugs in IDF
Answers checklist.
IDF version.
v4.4.2
Operating System used.
Linux
How did you build your project?
VS Code IDE
If you are using Windows, please specify command line type.
No response
Development Kit.
ESP32-WROOM + ETH W5500 SPI
Power Supply used.
External 3.3V
What is the expected behavior?
correct closure of a socket after calling
esp_tls_conn_destroy
in case of a failed connection to the server usingesp_tls_conn_new_sync
esp-tls and mbedtls.What is the actual behavior?
despite calling
esp_tls_conn_destroy
socket is not closed so that the number of available sockets is exhausted and no other connection can be established
in log can be observed a message
E (72500) esp-tls: Failed to create socket (family 2 socktype 1 protocol 0)
Steps to reproduce.
my configuration
eth module to connect to AWS MQTT using esp-aws-iot, esp-tls, mbedtls
tls configuration set to non blocking mode
non_block = true
inesp_tls_cfg_t
the connection to the MQTT server must be blocked to simulate connection problems e.g. with a firewall.
other network traffic such as DNS should work
step 1 - attempt to connect to
esp_tls_conn_new_sync
server (failed -host unreachable)step 2 -
esp_tls_conn_destroy
repeat the above until all available sockets are taken and you see the message in the log
esp-tls : Failed to create socket (family 2 socktype 1 protocol 0)
at this point no network connection can be established anymore
Debug Logs.
More Information.
Proposed solution
This line
esp-idf/components/esp-tls/esp_tls_mbedtls.c
Line 256 in 1b16ef6
should be replaced with
if (tls->is_tls && tls->server_fd.fd != -1) {
This has been successfully tested by me on the above example
The problem was introduced during the resolution of this bug #6163
An incorrect assumption was made, that the socket would be closed by calling
mbedtls_net_free
Unfortunately, if the connection to the server fails (e.g. host is unreachable),
mbedtls_net_context
struct is not initialised - the previously opened socket is not assigned tofd
thus can't be closed by mbedtls.socket number is assigned to fd in this place
esp-idf/components/esp-tls/esp_tls_mbedtls.c
Lines 67 to 73 in 1b16ef6
but it's only invoked after connection is established
esp-idf/components/esp-tls/esp_tls.c
Lines 413 to 414 in 1b16ef6
Unfortunately the connection will not always be established
Someone is experiencing similar problems https://esp32.com/viewtopic.php?t=29821
The text was updated successfully, but these errors were encountered: