From 28fe0bf975a47e009232b56dd10b6944cde70064 Mon Sep 17 00:00:00 2001
From: Matthias Nott <mnott@mnsoft.org>
Date: Sun, 22 Mar 2026 21:11:39 +0100
Subject: [PATCH] fix: MQTT connect timeout wrapper, host list logging

---
 lib/services/mqtt_service.dart |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/lib/services/mqtt_service.dart b/lib/services/mqtt_service.dart
index 41dc594..412291f 100644
--- a/lib/services/mqtt_service.dart
+++ b/lib/services/mqtt_service.dart
@@ -95,6 +95,7 @@
 
     final clientId = await _getClientId();
     final hosts = _getHosts();
+    _mqttLog('MQTT: hosts=${hosts.join(", ")} port=${config.port}');
 
     for (final host in hosts) {
       if (_intentionalClose) return;
@@ -162,8 +163,14 @@
       // Set _client BEFORE connect() so _onConnected can subscribe
       _client = client;
 
-      _mqttLog('MQTT: connecting to $host:${config.port} as $clientId');
-      final result = await client.connect();
+      _mqttLog('MQTT: connecting to $host:${config.port} as $clientId (timeout=${timeout}ms)');
+      final result = await client.connect().timeout(
+        Duration(milliseconds: timeout + 1000),
+        onTimeout: () {
+          _mqttLog('MQTT: connect timed out for $host');
+          return null;
+        },
+      );
       _mqttLog('MQTT: connect result=${result?.state}');
       if (result?.state == MqttConnectionState.connected) {
         client.autoReconnect = true; // Now enable auto-reconnect for the live connection

--
Gitblit v1.3.1