diff --git a/include/mqtt_client.h b/include/mqtt_client.h index b20f63f..020d026 100644 --- a/include/mqtt_client.h +++ b/include/mqtt_client.h @@ -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 diff --git a/mqtt_client.c b/mqtt_client.c index edfec7a..d576833 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -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