From 86d2145b97b6165a441eb7bc094748ce548e445e Mon Sep 17 00:00:00 2001 From: anoracleofra-code Date: Thu, 26 Mar 2026 16:48:06 -0600 Subject: [PATCH] fix: use paho-mqtt threaded loop for stable MQTT reconnection The Meshtastic MQTT bridge was using client.loop(timeout=1.0) in a blocking while loop. When the broker dropped the connection (common after ~30s of idle in Docker), the client silently stopped receiving messages with no auto-reconnect. Switch to client.loop_start() which runs the MQTT network loop in a background thread with built-in automatic reconnection. Also: - Add on_disconnect callback for visibility into disconnection events - Set reconnect_delay_set(1, 30) for fast exponential-backoff reconnect - Lower keepalive from 60s to 30s to stay within Docker network timeouts --- backend/services/sigint_bridge.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/backend/services/sigint_bridge.py b/backend/services/sigint_bridge.py index ca3ea03..75092de 100644 --- a/backend/services/sigint_bridge.py +++ b/backend/services/sigint_bridge.py @@ -536,15 +536,26 @@ class MeshtasticBridge: else: logger.error(f"Meshtastic MQTT connection refused: rc={rc}") + def _on_disconnect(client, userdata, rc): + if rc != 0: + logger.warning(f"Meshtastic MQTT disconnected unexpectedly (rc={rc}), will auto-reconnect") + else: + logger.info("Meshtastic MQTT disconnected cleanly") + client = mqtt.Client(client_id="shadowbroker-mesh", protocol=mqtt.MQTTv311) client.username_pw_set(self.USER, self.PASS) client.on_connect = _on_connect client.on_message = self._on_message + client.on_disconnect = _on_disconnect + client.reconnect_delay_set(min_delay=1, max_delay=30) - client.connect(self.HOST, self.PORT, keepalive=60) + client.connect(self.HOST, self.PORT, keepalive=30) + client.loop_start() while not self._stop.is_set(): - client.loop(timeout=1.0) + self._stop.wait(1.0) + + client.loop_stop() client.disconnect() def _on_message(self, client, userdata, msg):