@@ -241,9 +241,29 @@ static esp_err_t esp_websocket_client_dispatch_event(esp_websocket_client_handle
241241 return esp_event_loop_run (client -> event_handle , 0 );
242242}
243243
244+ /**
245+ * @brief Abort the WebSocket connection and initiate reconnection or shutdown
246+ *
247+ * @param client WebSocket client handle
248+ * @param error_type Type of error that caused the abort
249+ *
250+ * @return ESP_OK on success, ESP_FAIL on failure
251+ *
252+ * @note PRECONDITION: client->lock MUST be held by the calling thread before calling this function.
253+ * This function does NOT acquire the lock itself. Calling without the lock will result in
254+ * race conditions and undefined behavior.
255+ */
244256static esp_err_t esp_websocket_client_abort_connection (esp_websocket_client_handle_t client , esp_websocket_error_type_t error_type )
245257{
246258 ESP_WS_CLIENT_STATE_CHECK (TAG , client , return ESP_FAIL );
259+
260+
261+ if (client -> state == WEBSOCKET_STATE_CLOSING || client -> state == WEBSOCKET_STATE_UNKNOW ||
262+ client -> state == WEBSOCKET_STATE_WAIT_TIMEOUT ) {
263+ ESP_LOGW (TAG , "Connection already closing/closed, skipping abort" );
264+ return ESP_OK ;
265+ }
266+
247267 esp_transport_close (client -> transport );
248268
249269 if (!client -> config -> auto_reconnect ) {
@@ -256,6 +276,17 @@ static esp_err_t esp_websocket_client_abort_connection(esp_websocket_client_hand
256276 }
257277 client -> error_handle .error_type = error_type ;
258278 esp_websocket_client_dispatch_event (client , WEBSOCKET_EVENT_DISCONNECTED , NULL , 0 );
279+
280+ if (client -> errormsg_buffer ) {
281+ ESP_LOGD (TAG , "Freeing error buffer (%d bytes) - Free heap: %" PRIu32 " bytes" ,
282+ client -> errormsg_size , esp_get_free_heap_size ());
283+ free (client -> errormsg_buffer );
284+ client -> errormsg_buffer = NULL ;
285+ client -> errormsg_size = 0 ;
286+ } else {
287+ ESP_LOGD (TAG , "Disconnect - Free heap: %" PRIu32 " bytes" , esp_get_free_heap_size ());
288+ }
289+
259290 return ESP_OK ;
260291}
261292
@@ -453,6 +484,8 @@ static void destroy_and_free_resources(esp_websocket_client_handle_t client)
453484 esp_websocket_client_destroy_config (client );
454485 if (client -> transport_list ) {
455486 esp_transport_list_destroy (client -> transport_list );
487+ client -> transport_list = NULL ;
488+ client -> transport = NULL ;
456489 }
457490 vSemaphoreDelete (client -> lock );
458491#ifdef CONFIG_ESP_WS_CLIENT_SEPARATE_TX_LOCK
@@ -679,8 +712,18 @@ static int esp_websocket_client_send_with_exact_opcode(esp_websocket_client_hand
679712 } else {
680713 esp_websocket_client_error (client , "esp_transport_write() returned %d, errno=%d" , ret , errno );
681714 }
715+ ESP_LOGD (TAG , "Calling abort_connection due to send error" );
716+ #ifdef CONFIG_ESP_WS_CLIENT_SEPARATE_TX_LOCK
717+ xSemaphoreGiveRecursive (client -> tx_lock );
718+ xSemaphoreTakeRecursive (client -> lock , portMAX_DELAY );
719+ esp_websocket_client_abort_connection (client , WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT );
720+ xSemaphoreGiveRecursive (client -> lock );
721+ return ret ;
722+ #else
723+ // Already holding client->lock, safe to call
682724 esp_websocket_client_abort_connection (client , WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT );
683725 goto unlock_and_return ;
726+ #endif
684727 }
685728 opcode = 0 ;
686729 widx += wlen ;
@@ -1019,7 +1062,6 @@ static esp_err_t esp_websocket_client_recv(esp_websocket_client_handle_t client)
10191062 esp_websocket_free_buf (client , false);
10201063 return ESP_OK ;
10211064 }
1022-
10231065 esp_websocket_client_dispatch_event (client , WEBSOCKET_EVENT_DATA , client -> rx_buffer , rlen );
10241066
10251067 client -> payload_offset += rlen ;
@@ -1030,15 +1072,35 @@ static esp_err_t esp_websocket_client_recv(esp_websocket_client_handle_t client)
10301072 const char * data = (client -> payload_len == 0 ) ? NULL : client -> rx_buffer ;
10311073 ESP_LOGD (TAG , "Sending PONG with payload len=%d" , client -> payload_len );
10321074#ifdef CONFIG_ESP_WS_CLIENT_SEPARATE_TX_LOCK
1075+ xSemaphoreGiveRecursive (client -> lock ); // Release client->lock
1076+
1077+ // Now acquire tx_lock with timeout (consistent with PING/CLOSE handling)
10331078 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 ;
1079+ ESP_LOGE (TAG , "Could not lock ws-client within %d timeout for PONG" , WEBSOCKET_TX_LOCK_TIMEOUT_MS );
1080+ xSemaphoreTakeRecursive (client -> lock , portMAX_DELAY ); // Re-acquire client->lock before returning
1081+ esp_websocket_free_buf (client , false); // Free rx_buffer to prevent memory leak
1082+ return ESP_OK ; // Return gracefully, caller expects client->lock to be held
10361083 }
1037- #endif
1084+
1085+ // Re-acquire client->lock to maintain consistency
1086+ xSemaphoreTakeRecursive (client -> lock , portMAX_DELAY );
1087+
1088+
1089+ // Another thread may have closed it while we didn't hold client->lock
1090+ if (client -> state == WEBSOCKET_STATE_CLOSING || client -> state == WEBSOCKET_STATE_UNKNOW ||
1091+ client -> state == WEBSOCKET_STATE_WAIT_TIMEOUT || client -> transport == NULL ) {
1092+ ESP_LOGW (TAG , "Transport closed while preparing PONG, skipping send" );
1093+ xSemaphoreGiveRecursive (client -> tx_lock );
1094+ esp_websocket_free_buf (client , false); // Free rx_buffer to prevent memory leak
1095+ return ESP_OK ; // Caller expects client->lock to be held, which it is
1096+ }
1097+
10381098 esp_transport_ws_send_raw (client -> transport , WS_TRANSPORT_OPCODES_PONG | WS_TRANSPORT_OPCODES_FIN , data , client -> payload_len ,
10391099 client -> config -> network_timeout_ms );
1040- #ifdef CONFIG_ESP_WS_CLIENT_SEPARATE_TX_LOCK
10411100 xSemaphoreGiveRecursive (client -> tx_lock );
1101+ #else
1102+ esp_transport_ws_send_raw (client -> transport , WS_TRANSPORT_OPCODES_PONG | WS_TRANSPORT_OPCODES_FIN , data , client -> payload_len ,
1103+ client -> config -> network_timeout_ms );
10421104#endif
10431105 } else if (client -> last_opcode == WS_TRANSPORT_OPCODES_PONG ) {
10441106 client -> wait_for_pong_resp = false;
@@ -1136,7 +1198,20 @@ static void esp_websocket_client_task(void *pv)
11361198 client -> state = WEBSOCKET_STATE_CONNECTED ;
11371199 client -> wait_for_pong_resp = false;
11381200 client -> error_handle .error_type = WEBSOCKET_ERROR_TYPE_NONE ;
1201+ client -> payload_len = 0 ;
1202+ client -> payload_offset = 0 ;
1203+ client -> last_fin = false;
1204+ client -> last_opcode = WS_TRANSPORT_OPCODES_NONE ;
1205+
11391206 esp_websocket_client_dispatch_event (client , WEBSOCKET_EVENT_CONNECTED , NULL , 0 );
1207+ // Check for any data that may have arrived during handshake
1208+ int immediate_poll = esp_transport_poll_read (client -> transport , 0 ); // Non-blocking
1209+ if (immediate_poll >= 0 ) {
1210+ esp_err_t recv_result = esp_websocket_client_recv (client );
1211+ if (recv_result == ESP_OK ) {
1212+ esp_event_loop_run (client -> event_handle , 0 );
1213+ }
1214+ }
11401215 break ;
11411216 case WEBSOCKET_STATE_CONNECTED :
11421217 if ((CLOSE_FRAME_SENT_BIT & xEventGroupGetBits (client -> status_bits )) == 0 ) { // only send and check for PING
@@ -1214,12 +1289,13 @@ static void esp_websocket_client_task(void *pv)
12141289 esp_websocket_client_abort_connection (client , WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT );
12151290 xSemaphoreGiveRecursive (client -> lock );
12161291 } else if (read_select > 0 ) {
1292+ xSemaphoreTakeRecursive (client -> lock , lock_timeout );
12171293 if (esp_websocket_client_recv (client ) == ESP_FAIL ) {
12181294 ESP_LOGE (TAG , "Error receive data" );
1219- xSemaphoreTakeRecursive ( client -> lock , lock_timeout );
1295+ // Note: Already holding client->lock from line above
12201296 esp_websocket_client_abort_connection (client , WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT );
1221- xSemaphoreGiveRecursive (client -> lock );
12221297 }
1298+ xSemaphoreGiveRecursive (client -> lock );
12231299 } else {
12241300 ESP_LOGV (TAG , "Read poll timeout: skipping esp_transport_poll_read()." );
12251301 }
0 commit comments