From b0f189056a854f5f6ae1284c21c3f387a7fb50e3 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 14 Sep 2025 19:03:34 -0400 Subject: [PATCH] fix MQTT retry loop race condition with cleanup disconnect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, the reconnection loop was calling client.disconnect() during cleanup, which triggered the disconnect callback with code 0 ("Normal disconnection"). The callback would then exit early, preventing further reconnection attempts. This creates a cleanup flag that prevents the disconnect callback from stopping reconnection when we're intentionally cleaning up the old client during retry attempts. Fixes issue where MQTT would get stuck in retry loop without actually attempting fresh connections. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- frigate/comms/mqtt.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/frigate/comms/mqtt.py b/frigate/comms/mqtt.py index 0334b3eb9..718376aa5 100644 --- a/frigate/comms/mqtt.py +++ b/frigate/comms/mqtt.py @@ -24,6 +24,7 @@ class MqttClient(Communicator): self._reconnect_thread: Optional[threading.Thread] = None self._reconnect_delay = 10 # Retry every 10 seconds self._stop_reconnect: bool = False + self._cleanup_in_progress: bool = False def subscribe(self, receiver: Callable[[str, str], None]) -> None: """Wrapper for allowing dispatcher to subscribe.""" @@ -239,11 +240,15 @@ class MqttClient(Communicator): f"MQTT disconnected - reason: '{reason_name}', code: {reason_value}, type: {type(reason_code)}" ) - # Don't attempt reconnection if we're stopping or if it was a clean disconnect + # Don't attempt reconnection if we're stopping, cleaning up, or if it was a clean disconnect if self._stop_reconnect: logger.error("MQTT not reconnecting - stop flag set") return + if self._cleanup_in_progress: + logger.error("MQTT not reconnecting - cleanup in progress") + return + if reason_code == 0: logger.error("MQTT not reconnecting - clean disconnect (code 0)") return @@ -379,9 +384,12 @@ class MqttClient(Communicator): # Clean up old client if it exists if self.client is not None: try: + self._cleanup_in_progress = True self.client.disconnect() self.client.loop_stop() + self._cleanup_in_progress = False except Exception: + self._cleanup_in_progress = False pass # Ignore cleanup errors # Create completely fresh client and attempt connection