MQTT uploader: broker never retries if the first connect fails with a non-timeout error
Summary
If the initial client.connect() in MeshCoreMqttUploader.async_start() raises anything other than TimeoutError, client.loop_start() is never called. The broker then stays disconnected until Home Assistant is restarted (or the entry reloaded), with no further retries.
Where
custom_components/meshcore/mqtt_uploader.py, async_start() (v3.0.4, still the same on main):
try:
client.connect_timeout = CONNECT_TIMEOUT
await asyncio.wait_for(
self.hass.async_add_executor_job(
client.connect, broker.server, broker.port, broker.keepalive
),
CONNECT_TIMEOUT,
)
client.loop_start()
except TimeoutError:
self.logger.error(
"[%s] Connection timed out after %.0fs; paho keeps retrying",
broker.name,
CONNECT_TIMEOUT,
)
client.loop_start()
except Exception as ex:
self.logger.error("[%s] Connection error: %s", broker.name, ex)
# <-- no loop_start() here, so paho never retries
The TimeoutError branch starts the network loop so paho keeps retrying, but the generic except Exception branch does not.
What happened
I have two brokers configured (both WebSocket + TLS on port 443, auth token). Around midnight the uploader was started again. At that moment one broker rejected the WebSocket handshake:
2026-11-06 00:12:16.698 ERROR (MainThread) [custom_components.meshcore] [MQTT1] Connection error: WebSocket handshake error, connection not upgraded
After that there were no more log lines for [MQTT1], and binary_sensor.<node>_mqtt_broker_1_connection stayed off for about 10 hours, until I restarted Home Assistant. The same broker had dozens of short disconnects the day before and always came back within seconds, because those happened after the loop was already running.
Suggested fix
Start the loop in the generic exception branch as well (paho's loop_start() thread runs loop_forever(retry_first_connection=True) and reconnects with its backoff):
except Exception as ex:
self.logger.error(
"[%s] Connection error: %s; paho keeps retrying", broker.name, ex
)
client.loop_start()
Environment
- meshcore-ha 3.0.4
- Home Assistant 2026.11.0 (HAOS 18.4)
Confirmed, and your reading of paho is right: loop_start() runs loop_forever(retry_first_connection=True), so the generic branch just needed the same call the timeout branch had. Fix in #410, with a test that a refused first connect still starts the loop.