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) {