From f37445f8a7d14f7d75e1ec848398f591d793b6f6 Mon Sep 17 00:00:00 2001 From: agessaman Date: Sat, 26 Sep 2026 12:48:44 -0700 Subject: [PATCH] fix(mqtt): count only failed starts, not rejected reconnects, in cf reconnect() on a started client returns ESP_FAIL while an attempt is already in progress. On a Heltec V3, a live reconfigure hit exactly that and the attempt then connected, yet cf read 1. Count start_failures only when connect() on a stopped client fails, and restore the attempt flag to its prior value on any failed call so the in-progress attempt is still judged by its own outcome. --- src/helpers/bridges/MQTTBridge.cpp | 11 ++++++++--- src/helpers/bridges/MQTTBridge.h | 6 +++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/helpers/bridges/MQTTBridge.cpp b/src/helpers/bridges/MQTTBridge.cpp index 2bee5bb2..8e5eec70 100644 --- a/src/helpers/bridges/MQTTBridge.cpp +++ b/src/helpers/bridges/MQTTBridge.cpp @@ -2512,8 +2512,10 @@ esp_err_t MQTTBridge::reconnectSlotClient(int index) { } esp_err_t r; + const bool was_pending = _slot_attempt_pending[index]; + const bool starting = !slot.client->isStarted(); _slot_attempt_pending[index] = true; - if (!slot.client->isStarted()) { + if (starting) { MQTT_DEBUG_PRINTLN("MQTT%d start (client was stopped)", index + 1); r = slot.client->connect(); } else { @@ -2526,8 +2528,11 @@ esp_err_t MQTTBridge::reconnectSlotClient(int index) { // is the point at which a freshly minted token becomes the one in use. slot.applied_token_expires_at = slot.token_expires_at; } else { - _slot_attempt_pending[index] = false; - slot.start_failures++; + _slot_attempt_pending[index] = was_pending; + // A failed start (buffer/task allocation, config) means no attempt could begin. + // reconnect() on a started client returns ESP_FAIL while one is already in + // progress, which is not a failure (seen on hardware after a reconfigure). + if (starting) slot.start_failures++; } return r; } diff --git a/src/helpers/bridges/MQTTBridge.h b/src/helpers/bridges/MQTTBridge.h index 87e585b1..767a49e1 100644 --- a/src/helpers/bridges/MQTTBridge.h +++ b/src/helpers/bridges/MQTTBridge.h @@ -187,9 +187,9 @@ private: // disconnect_count (which also counts dropped live sessions) and from the // publish error counter, which cannot move at all while a slot is down. uint32_t connect_failures; - // connect()/reconnect() calls that returned an error, so no attempt started and - // no event will arrive. Written only by the bridge task; reported summed with - // connect_failures. + // connect() calls on a stopped client that returned an error, so no attempt + // started and no event will arrive. Written only by the bridge task; reported + // summed with connect_failures. uint32_t start_failures; unsigned long first_disconnect_time; // millis() of first disconnect after boot