@@ -244,6 +244,16 @@ static esp_err_t esp_websocket_client_dispatch_event(esp_websocket_client_handle
244244static esp_err_t esp_websocket_client_abort_connection (esp_websocket_client_handle_t client , esp_websocket_error_type_t error_type )
245245{
246246 ESP_WS_CLIENT_STATE_CHECK (TAG , client , return ESP_FAIL );
247+
248+ // Note: This function must be called with client->lock already held
249+ // The caller is responsible for acquiring the lock before calling
250+
251+ // CRITICAL: Check if already closing/closed to prevent double-close
252+ if (client -> state == WEBSOCKET_STATE_CLOSING || client -> state == WEBSOCKET_STATE_UNKNOW ) {
253+ ESP_LOGW (TAG , "Connection already closing/closed, skipping abort" );
254+ return ESP_OK ;
255+ }
256+
247257 esp_transport_close (client -> transport );
248258
249259 if (!client -> config -> auto_reconnect ) {
@@ -256,6 +266,17 @@ static esp_err_t esp_websocket_client_abort_connection(esp_websocket_client_hand
256266 }
257267 client -> error_handle .error_type = error_type ;
258268 esp_websocket_client_dispatch_event (client , WEBSOCKET_EVENT_DISCONNECTED , NULL , 0 );
269+
270+ if (client -> errormsg_buffer ) {
271+ ESP_LOGD (TAG , "Freeing error buffer (%d bytes) - Free heap: %" PRIu32 " bytes" ,
272+ client -> errormsg_size , esp_get_free_heap_size ());
273+ free (client -> errormsg_buffer );
274+ client -> errormsg_buffer = NULL ;
275+ client -> errormsg_size = 0 ;
276+ } else {
277+ ESP_LOGD (TAG , "Disconnect - Free heap: %" PRIu32 " bytes" , esp_get_free_heap_size ());
278+ }
279+
259280 return ESP_OK ;
260281}
261282
@@ -453,6 +474,8 @@ static void destroy_and_free_resources(esp_websocket_client_handle_t client)
453474 esp_websocket_client_destroy_config (client );
454475 if (client -> transport_list ) {
455476 esp_transport_list_destroy (client -> transport_list );
477+ client -> transport_list = NULL ;
478+ client -> transport = NULL ;
456479 }
457480 vSemaphoreDelete (client -> lock );
458481#ifdef CONFIG_ESP_WS_CLIENT_SEPARATE_TX_LOCK
@@ -679,8 +702,18 @@ static int esp_websocket_client_send_with_exact_opcode(esp_websocket_client_hand
679702 } else {
680703 esp_websocket_client_error (client , "esp_transport_write() returned %d, errno=%d" , ret , errno );
681704 }
705+ ESP_LOGD (TAG , "Calling abort_connection due to send error" );
706+ #ifdef CONFIG_ESP_WS_CLIENT_SEPARATE_TX_LOCK
707+ xSemaphoreGiveRecursive (client -> tx_lock );
708+ xSemaphoreTakeRecursive (client -> lock , portMAX_DELAY );
709+ esp_websocket_client_abort_connection (client , WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT );
710+ xSemaphoreGiveRecursive (client -> lock );
711+ return ret ;
712+ #else
713+ // Already holding client->lock, safe to call
682714 esp_websocket_client_abort_connection (client , WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT );
683715 goto unlock_and_return ;
716+ #endif
684717 }
685718 opcode = 0 ;
686719 widx += wlen ;
@@ -1019,7 +1052,6 @@ static esp_err_t esp_websocket_client_recv(esp_websocket_client_handle_t client)
10191052 esp_websocket_free_buf (client , false);
10201053 return ESP_OK ;
10211054 }
1022-
10231055 esp_websocket_client_dispatch_event (client , WEBSOCKET_EVENT_DATA , client -> rx_buffer , rlen );
10241056
10251057 client -> payload_offset += rlen ;
@@ -1030,15 +1062,35 @@ static esp_err_t esp_websocket_client_recv(esp_websocket_client_handle_t client)
10301062 const char * data = (client -> payload_len == 0 ) ? NULL : client -> rx_buffer ;
10311063 ESP_LOGD (TAG , "Sending PONG with payload len=%d" , client -> payload_len );
10321064#ifdef CONFIG_ESP_WS_CLIENT_SEPARATE_TX_LOCK
1065+ xSemaphoreGiveRecursive (client -> lock ); // Release client->lock
1066+
1067+ // Now acquire tx_lock with timeout (consistent with PING/CLOSE handling)
10331068 if (xSemaphoreTakeRecursive (client -> tx_lock , WEBSOCKET_TX_LOCK_TIMEOUT_MS ) != pdPASS ) {
1034- ESP_LOGE (TAG , "Could not lock ws-client within %d timeout" , WEBSOCKET_TX_LOCK_TIMEOUT_MS );
1035- return ESP_FAIL ;
1069+ ESP_LOGE (TAG , "Could not lock ws-client within %d timeout for PONG" , WEBSOCKET_TX_LOCK_TIMEOUT_MS );
1070+ xSemaphoreTakeRecursive (client -> lock , portMAX_DELAY ); // Re-acquire client->lock before returning
1071+ return ESP_OK ; // Return gracefully, caller expects client->lock to be held
10361072 }
1037- #endif
1073+
1074+ // Re-acquire client->lock to maintain consistency
1075+ xSemaphoreTakeRecursive (client -> lock , portMAX_DELAY );
1076+
1077+ // CRITICAL: Check if transport is still valid after re-acquiring lock
1078+ // Another thread may have closed it while we didn't hold client->lock
1079+ if (client -> state == WEBSOCKET_STATE_CLOSING || client -> state == WEBSOCKET_STATE_UNKNOW ||
1080+ client -> state == WEBSOCKET_STATE_WAIT_TIMEOUT || client -> transport == NULL ) {
1081+ ESP_LOGW (TAG , "Transport closed while preparing PONG, skipping send" );
1082+ xSemaphoreGiveRecursive (client -> tx_lock );
1083+ return ESP_OK ; // Caller expects client->lock to be held, which it is
1084+ }
1085+
10381086 esp_transport_ws_send_raw (client -> transport , WS_TRANSPORT_OPCODES_PONG | WS_TRANSPORT_OPCODES_FIN , data , client -> payload_len ,
10391087 client -> config -> network_timeout_ms );
1040- #ifdef CONFIG_ESP_WS_CLIENT_SEPARATE_TX_LOCK
10411088 xSemaphoreGiveRecursive (client -> tx_lock );
1089+ #else
1090+ // When separate TX lock is not configured, we already hold client->lock
1091+ // which protects the transport, so we can send PONG directly
1092+ esp_transport_ws_send_raw (client -> transport , WS_TRANSPORT_OPCODES_PONG | WS_TRANSPORT_OPCODES_FIN , data , client -> payload_len ,
1093+ client -> config -> network_timeout_ms );
10421094#endif
10431095 } else if (client -> last_opcode == WS_TRANSPORT_OPCODES_PONG ) {
10441096 client -> wait_for_pong_resp = false;
@@ -1136,6 +1188,11 @@ static void esp_websocket_client_task(void *pv)
11361188 client -> state = WEBSOCKET_STATE_CONNECTED ;
11371189 client -> wait_for_pong_resp = false;
11381190 client -> error_handle .error_type = WEBSOCKET_ERROR_TYPE_NONE ;
1191+ client -> payload_len = 0 ;
1192+ client -> payload_offset = 0 ;
1193+ client -> last_fin = false;
1194+ client -> last_opcode = WS_TRANSPORT_OPCODES_NONE ;
1195+
11391196 esp_websocket_client_dispatch_event (client , WEBSOCKET_EVENT_CONNECTED , NULL , 0 );
11401197 break ;
11411198 case WEBSOCKET_STATE_CONNECTED :
@@ -1221,12 +1278,15 @@ static void esp_websocket_client_task(void *pv)
12211278 esp_websocket_client_abort_connection (client , WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT );
12221279 xSemaphoreGiveRecursive (client -> lock );
12231280 } else if (read_select > 0 ) {
1281+ // CRITICAL: Protect entire recv operation with client->lock
1282+ // This prevents transport from being closed while recv is in progress
1283+ xSemaphoreTakeRecursive (client -> lock , lock_timeout );
12241284 if (esp_websocket_client_recv (client ) == ESP_FAIL ) {
12251285 ESP_LOGE (TAG , "Error receive data" );
1226- xSemaphoreTakeRecursive ( client -> lock , lock_timeout );
1286+ // Note: Already holding client->lock from line above
12271287 esp_websocket_client_abort_connection (client , WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT );
1228- xSemaphoreGiveRecursive (client -> lock );
12291288 }
1289+ xSemaphoreGiveRecursive (client -> lock );
12301290 }
12311291 } else if (WEBSOCKET_STATE_WAIT_TIMEOUT == client -> state ) {
12321292 // waiting for reconnection or a request to stop the client...
0 commit comments