From e2c1ecf742c6065b863234568c284bfea7137212 Mon Sep 17 00:00:00 2001 From: Rafael Silva Date: Wed, 15 Dec 2021 12:45:11 +0000 Subject: [PATCH] fix #150 Signed-off-by: Rafael Silva --- include/mqtt.h | 1 + src/mqtt.c | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/mqtt.h b/include/mqtt.h index 95a9497..9f90a68 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -206,6 +206,7 @@ struct mqtt_fixed_header { MQTT_ERROR(MQTT_ERROR_INITIAL_RECONNECT) \ MQTT_ERROR(MQTT_ERROR_INVALID_REMAINING_LENGTH) \ MQTT_ERROR(MQTT_ERROR_CLEAN_SESSION_IS_REQUIRED) \ + MQTT_ERROR(MQTT_ERROR_RECONNECT_FAILED) \ MQTT_ERROR(MQTT_ERROR_RECONNECTING) /* todo: add more connection refused errors */ diff --git a/src/mqtt.c b/src/mqtt.c index de04057..fb66c97 100644 --- a/src/mqtt.c +++ b/src/mqtt.c @@ -39,7 +39,15 @@ enum MQTTErrors mqtt_sync(struct mqtt_client *client) { MQTT_PAL_MUTEX_LOCK(&client->mutex); if (client->error != MQTT_ERROR_RECONNECTING && client->error != MQTT_OK && client->user_callback != NULL) { client->user_callback(client, MQTT_EVENT_RECONNECT, NULL, &client->user_callback_state); - /* unlocked during CONNECT */ + if (client->error != MQTT_OK) { + client->error = MQTT_ERROR_RECONNECT_FAILED; + + /* normally unlocked during CONNECT */ + MQTT_PAL_MUTEX_UNLOCK(&client->mutex); + } + + err = client->error; + if (err != MQTT_OK) goto ERR; } else { /* mqtt_reconnect will have queued the disconnect packet - that needs to be sent and then call reconnect */ if (client->error == MQTT_ERROR_RECONNECTING) {