Skip to content

Commit

Permalink
fix(websocket): release client-lock during WEBSOCKET_EVENT_DATA
Browse files Browse the repository at this point in the history
This resolves:

 1) Deadlock when trying to reserve a lock in WEBSOCKET_EVENT_DATA,
    but lock is held by a thread trying to send a websocket message.
 2) High latency caused by writers serialized with WEBSOCKET_EVENT_DATA
    while calling esp_websocket_client_send(), even when TCP window
    has enough space for the entire message being queued to send.

Multiple writers are still serialized at fragment boundaries, but
only with other writers and websocket error updates.

Fixes #625
  • Loading branch information
bryghtlabs-richard committed Dec 2, 2024
1 parent c5b49de commit 24eabbf
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions components/esp_websocket_client/esp_websocket_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1044,12 +1044,6 @@ static void esp_websocket_client_task(void *pv)
break;
}
client->ping_tick_ms = _tick_get_ms();

if (esp_websocket_client_recv(client) == ESP_FAIL) {
ESP_LOGE(TAG, "Error receive data");
esp_websocket_client_abort_connection(client, WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT);
break;
}
break;
case WEBSOCKET_STATE_WAIT_TIMEOUT:

Expand Down Expand Up @@ -1086,6 +1080,14 @@ static void esp_websocket_client_task(void *pv)
xSemaphoreTakeRecursive(client->lock, lock_timeout);
esp_websocket_client_abort_connection(client, WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT);
xSemaphoreGiveRecursive(client->lock);
} else if (read_select > 0) {
if (esp_websocket_client_recv(client) == ESP_FAIL) {
ESP_LOGE(TAG, "Error receive data");
xSemaphoreTakeRecursive(client->lock, lock_timeout);
esp_websocket_client_abort_connection(client, WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT);
xSemaphoreGiveRecursive(client->lock);
break;
}
}
} else if (WEBSOCKET_STATE_WAIT_TIMEOUT == client->state) {
// waiting for reconnecting...
Expand Down

0 comments on commit 24eabbf

Please sign in to comment.