From 90cd207e72aba5669a88a2b23b9e7f960032d48a Mon Sep 17 00:00:00 2001 From: Hideki Miyazaki Date: Fri, 25 Sep 2026 15:09:21 +0900 Subject: [PATCH] Retry FreeRTOS_connect() on ETIMEDOUT/ENOTCONN in the multithread TLS client The network stack (PHY link, ARP for the gateway) isn't necessarily ready the instant a connection task starts, so a connect attempt made too early fails with ETIMEDOUT(-116) or ENOTCONN(-128). The non-multithreaded caller in test_main.c already retries around its own wolfSSL_TLS_client_do() call for exactly this; TLS_MULTITHREAD_TEST's per-task connections had no equivalent, so they'd fail outright instead of retrying. Close and recreate the socket each attempt, since its state after a failed connect isn't reusable. --- .../e2studio/RA6M4/test/src/wolf_client.c | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/IDE/Renesas/e2studio/RA6M4/test/src/wolf_client.c b/IDE/Renesas/e2studio/RA6M4/test/src/wolf_client.c index 8717f46e38..7de78e7398 100644 --- a/IDE/Renesas/e2studio/RA6M4/test/src/wolf_client.c +++ b/IDE/Renesas/e2studio/RA6M4/test/src/wolf_client.c @@ -163,19 +163,36 @@ int wolfSSL_TLS_client_do(void *pvParam) xRemoteAddress.sin_port = FreeRTOS_htons(p->port); xRemoteAddress.sin_addr = FreeRTOS_inet_addr(SERVER_IP); - /* Create a FreeRTOS TCP Socket and connect */ - xClientSocket = FreeRTOS_socket(FREERTOS_AF_INET, + /* Create a FreeRTOS TCP Socket and connect. The network stack (PHY link, + * ARP for the gateway) isn't necessarily ready the instant this task + * starts, so a connect attempt made too early fails with ETIMEDOUT(-116) + * or ENOTCONN(-128); retry in that case, same as the non-multithreaded + * caller in test_main.c does around its own wolfSSL_TLS_client_do() call. + * A failed socket is closed and recreated each attempt since its state + * after a failed connect isn't reusable. */ + { + int TCP_connect_retry = 0; + do { + xClientSocket = FreeRTOS_socket(FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP); - configASSERT(xClientSocket != FREERTOS_INVALID_SOCKET); + configASSERT(xClientSocket != FREERTOS_INVALID_SOCKET); - FreeRTOS_bind(xClientSocket, NULL, sizeof(xSize)); + FreeRTOS_bind(xClientSocket, NULL, sizeof(xSize)); - /* Client Socket Connect */ - ret = FreeRTOS_connect(xClientSocket, - &xRemoteAddress, - sizeof(xRemoteAddress)); + ret = FreeRTOS_connect(xClientSocket, + &xRemoteAddress, + sizeof(xRemoteAddress)); + if (ret == FR_SOCKET_SUCCESS) { + break; + } + + FreeRTOS_closesocket(xClientSocket); + xClientSocket = NULL; + TCP_connect_retry++; + } while ((ret == -116 || ret == -128) && TCP_connect_retry < 100); + } if (ret != FR_SOCKET_SUCCESS) { msg(pcName, i, " Error [%d]: FreeRTOS_connect.\n", ret);