core: MQTT Client Loses Connection and Does Not Attempt Reconnect Without Restarting HA
The problem
Setting up a connection to a local Mosquitto MQTT broker via wss works just fine. The Home Assistant MQTT client connects, receives, and publishes messages as expected.
If I restart my Mosquitto service for any reason, Home Assistant logs that it lost the connection. It does not attempt to reconnect to the broker until I restart Home Assistant. Other clients using the same endpoint reconnect without issue immediately once the broker is back online.
What version of Home Assistant Core has the issue?
core-2024.12.0
What was the last working version of Home Assistant Core?
No response
What type of installation are you running?
Home Assistant Container
Integration causing the issue
MQTT
Link to integration documentation on our website
https://www.home-assistant.io/integrations/mqtt/
Diagnostics information
No response
Example YAML snippet
No response
Anything in the logs that might be useful for us?
2024-12-11T21:02:31.528977372-06:00 stderr F 2024-12-11 21:02:31.520 DEBUG (MainThread) [homeassistant.components.mqtt.models] Rendering incoming payload '[{"amp":null,"att":{"ref":0}},{"amp":null,"att":{"ref":0}},{"amp":null,"att":{"ref":0}},{"amp":null,"att":{"ref":0}},{"amp":null,"att":{"ref":0}},{"amp":null,"att":{"ref":0}},{"amp":null,"att":{"ref":0}},{"amp":null,"att":{"ref":0}}]' with variables {'entity_id': 'select.energy_monitor_mqtt_select_4', 'name': 'MQTT Select', 'this': <template TemplateStateFromEntityId(select.energy_monitor_mqtt_select_4)>} and Template<template=({{ (value_json[3].att.ref | string) + ' Amp' }}) renders=60>
2024-12-11T21:02:34.555509706-06:00 stderr F 2024-12-11 21:02:34.553 DEBUG (MainThread) [homeassistant.components.mqtt.client] mosquitto.mydomain.com: connection closed 34
2024-12-11T21:02:34.563350289-06:00 stderr F 2024-12-11 21:02:34.558 DEBUG (MainThread) [homeassistant.components.mqtt.client] Disconnected from MQTT server mosquitto.mydomain.com:443 (7)
Additional information
The included log output shows 3 consecutive logs from the mqtt client in debug logging mode. The first line just represents a healthy event being received. The next 2 represent the moment when the broker was restarted. Over the course of minutes or hours, nothing else from the mqtt client is logged.
A snippet from the HQ diagnostics concerning MQTT:
{
"mqtt_config": {
"birth_message": {
"payload": "online",
"qos": 0,
"retain": false,
"topic": "homeassistant/status"
},
"broker": "mosquitto.mydomain.com",
"certificate": "auto",
"discovery": true,
"discovery_prefix": "homeassistant",
"keepalive": 30,
"password": "**REDACTED**",
"port": 443,
"protocol": "3.1.1",
"tls_insecure": false,
"transport": "websockets",
"username": "**REDACTED**",
"will_message": {
"payload": "offline",
"qos": 0,
"retain": false,
"topic": "homeassistant/status"
},
"ws_headers": {},
"ws_path": "/"
}
}
The Mosquitto broker sits behind an Nginx reverse proxy that manages a Let’s Encrypt wildcard domain cert for my personal domain. I have PiHole routing local DNS requests for the mosquitto.mydomain.com domain to the Nginx reverse proxies listening on two different static IP addresses. Connecting to either IP on port 443 will get through to the single backend Mosquitto service.
MQTT explorer and various devices connected to the broker through the exact same endpoint work fine and reconnect immediately once the broker is back online. Home Assistant seems to be the only client that does not recover.
Restarting via the UI or restarting the HA Docker container both work to get the mqtt client working again. Re-saving the MQTT configuration via the UI does not. Using the MQTT configure UI to publish a test message fails and logs an error about how the client isn’t connected.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 25 (13 by maintainers)
Commits related to this issue
- Handle mqtt.WebsocketConnectionError when connecting to the MQTT broker fixes #132985 — committed to home-assistant/core by bdraco 2 years ago
- Handle mqtt.WebsocketConnectionError when connecting to the MQTT broker (#133610) fixes #132985 — committed to kkpan11/home-assistant-core by bdraco 2 years ago
- Handle mqtt.WebsocketConnectionError when connecting to the MQTT broker (#133610) fixes #132985 — committed to home-assistant/core by bdraco 2 years ago
- Handle WebsocketConnectionError during mqtt auto reconnect followup to #133610 to handle the exception in the auto reconnect path as well fixes #132985 — committed to home-assistant/core by bdraco 2 years ago
- Handle WebsocketConnectionError during mqtt auto reconnect (#133697) followup to #133610 to handle the exception in the auto reconnect path as well fixes #132985 — committed to zweckj/home-assistant-core by bdraco 2 years ago
Thanks, but I’ve mentioned a few times already about testing against my broker without Nginx, which is a helpful comparison.
I ended up grabbing a simplified example of using paho’s client directly in a test python script to recreate what was going on. The
reconnect()is raising the exception:paho.mqtt.client.WebsocketConnectionError: WebSocket handshake errorwhile the broker is offline.If I change the HA integration client to catch any exception type in addition to the OSError it is already catching, it all starts working again. HA reconnects just fine. So it seems like the exception that is being raised just isn’t being caught and is causing the reconnect loop to die.
For example, my working
_reconnect_loopmethod looks like this now:I see the debug log:
Error re-connecting to MQTT server due to exception: WebSocket handshake errorand eventually a bunch of connected messages once the broker is back.For some reason I can’t get it to work right if I catch
mqtt.WebsocketConnectionErrorinstead ofException.