MQTT: Fix esp_mqtt_client_stop deadlock

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 https://github.com/espressif/esp-mqtt/issues/163
This commit is contained in:
Marius Vikhammer
2020-08-04 19:16:13 +08:00
parent 702da6d528
commit 5e17dcaeb2
2 changed files with 13 additions and 0 deletions

View File

@ -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
@ -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

View File

@ -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