Skip to content

Commit

Permalink
MQTT: Fix esp_mqtt_client_stop deadlock
Browse files Browse the repository at this point in the history
esp_mqtt_client_stop would lead to a deadlock (with itself) when called
from the event handler.

Updated comments to reflect that some functions should not be called from
the event handler.

Closes #163
  • Loading branch information
ESP-Marius committed Aug 4, 2020
1 parent 702da6d commit 5e17dca
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/mqtt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ esp_err_t esp_mqtt_client_disconnect(esp_mqtt_client_handle_t client);
/**
* @brief Stops mqtt client tasks
*
* * Notes:
* - Cannot be called from the mqtt event handler
*
* @param client mqtt client handle
*
* @return ESP_OK on success
Expand Down Expand Up @@ -306,6 +309,9 @@ int esp_mqtt_client_publish(esp_mqtt_client_handle_t client, const char *topic,
/**
* @brief Destroys the client handle
*
* Notes:
* - Cannot be called from the mqtt event handler
*
* @param client mqtt client handle
*
* @return ESP_OK
Expand Down
7 changes: 7 additions & 0 deletions mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,13 @@ esp_err_t esp_mqtt_client_stop(esp_mqtt_client_handle_t client)
{
MQTT_API_LOCK(client);
if (client->run) {
/* A running client cannot be stopped from the MQTT task/event handler */
TaskHandle_t running_task = xTaskGetCurrentTaskHandle();
if (running_task == client->task_handle) {
ESP_LOGE(TAG, "Client cannot be stopped from MQTT task");
return ESP_FAIL;
}

// Only send the disconnect message if the client is connected
if(client->state == MQTT_STATE_CONNECTED) {
// Notify the broker we are disconnecting
Expand Down

0 comments on commit 5e17dca

Please sign in to comment.