Skip to content

Fix MQTT connection loss and display freeze after extended uptime#1

Open
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-mqtt-connection-issues
Open

Fix MQTT connection loss and display freeze after extended uptime#1
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-mqtt-connection-issues

Conversation

Copy link
Copy Markdown

Copilot AI commented Apr 18, 2026

After weeks of uptime, the MQTT connection would permanently die and display refreshes would become extremely sluggish, requiring a reboot to recover. Root cause: multiple blocking operations in the reconnect path were starving loop().

Bugs fixed

networking.cpp

  • Double mqttClient.connect() call — the first call's result was silently discarded, causing two full blocking TCP/TLS handshakes on every reconnect attempt
  • delay(1000) in reconnectWiFi() — blocked the entire loop() for 1 second every 5 seconds whenever WiFi was lost; removed since WiFi.setAutoReconnect(true) was already set
  • No timeout on WiFiClientSecure — a failed MQTT connect could block for 10–30 s per attempt; capped at 5 s via espClient.setTimeout(5)
  • lastReconnectTry uninitialized — added explicit = 0 in constructor initializer list
// Before: two blocking connect attempts, result of first discarded
mqttClient.disconnect();
mqttClient.connect(clientId.c_str(), MQTT_USERNAME, MQTT_PASSWORD); // ignored
bool connected = mqttClient.connect(clientId.c_str(), MQTT_USERNAME, MQTT_PASSWORD);

// After: single attempt with bounded timeout
mqttClient.disconnect();
bool connected = mqttClient.connect(clientId.c_str(), MQTT_USERNAME, MQTT_PASSWORD);

display_handler.cpp

  • uint16_t underflow on alarm ring-buffer indexwriteIndex - scrollPosition wraps to ~65534 when scrollPosition > writeIndex, causing an out-of-bounds read on alarmHistory[]; fixed to (writeIndex - scrollPosition + MAX_ALARMS) % MAX_ALARMS

Copilot AI and others added 2 commits April 18, 2026 19:24
…t, and display index underflow

Agent-Logs-Url: https://github.com/calkoe/OpenFreqSensor/sessions/d13a5bb7-7836-4cde-a9c9-9ed47fd8bf0d

Co-authored-by: calkoe <26646066+calkoe@users.noreply.github.com>
Agent-Logs-Url: https://github.com/calkoe/OpenFreqSensor/sessions/d13a5bb7-7836-4cde-a9c9-9ed47fd8bf0d

Co-authored-by: calkoe <26646066+calkoe@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants