Skip to content

fix: restart the MQTT session when one device stops receiving messages - #1044

Draft
infy1995 wants to merge 3 commits into
Python-roborock:mainfrom
infy1995:fix/mqtt-device-health
Draft

infy1995 wants to merge 3 commits into
Python-roborock:mainfrom
infy1995:fix/mqtt-device-health

Conversation

@infy1995

@infy1995 infy1995 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Draft: reworked after the review in #1012: consecutive timeouts per device instead of a time window, and the mqtt_health diagnostics moved to a follow-up PR. I'm running this version on my Home Assistant and will mark it ready after a few days of testing.

Fixes #1012.

Problem

All devices on an account share one MQTT session. On my account, the broker sometimes stops delivering a Saros 10R's (V1, roborock.vacuum.a144) MQTT responses, while an F25 Ultra (A01, roborock.wdv.a194) on the same connection keeps working. The HealthManager counted timeouts for the whole session, and any successful response reset that count. The F25's responses hid the Saros's timeouts, so the session was never restarted. In Home Assistant the map is only available over MQTT, so it stopped updating for hours. Status, battery and commands kept working because they use the local connection.

The debug log in #1012 shows it with the official release: the connection stays up (PINGREQ/PINGRESP every 45 s), the F25 got 61 of 61 replies, while 91 GET_MULTI_MAPS_LIST requests to the Saros timed out in a row. Reloading the integration opened a new connection and the Saros answered right away.

Changes

  1. fix: restart the MQTT session when one device stops receiving messages.
    • Consecutive timeouts are also counted per device, reset only by that device's own response. 3 in a row restart the session, so other devices' responses can't hide them anymore.
    • 3 timeouts in a row across several devices still restart the session at once, as before.
    • Timeouts while the session is already reconnecting don't restart it.
    • Restarts are paced: at most one every 30 minutes, as before. After a device restarts the session, it may restart it again only after 1 hour, then 2 and 4 hours, then every 6 hours, until it responds again. A device that is really offline doesn't keep interrupting the others.
    • While a device waits out that interval, its timeouts don't count towards the session either, so one stray timeout from another device can't bypass it.
    • Restarts are logged at info level with the reason and the device's next allowed restart.
    • API change: HealthManager.on_success() / on_timeout() now take a device_id, and MqttChannel.health_manager returns a DeviceHealthManager, whose no-argument methods match the old usage.
  2. fix: reconnect right away after a forced MQTT session restart.
    • A forced restart used to wait out the 10 s backoff, which cut every device off for 10 s. It now reconnects immediately, but only when the session was connected. The flag is cleared before the next CONNECT, so if that attempt fails, including with RC_ERROR_UNAUTHORIZED, the normal backoff applies. A test covers that.
    • Every new connection clears the timeouts counted on the previous one, and the session tells the health manager whether it is connected.
  3. docs: the health management section of docs/DEVICES.md described local connection recovery as the HealthManager. It said A01/B01 devices don't need one. I split it into local connection recovery and MQTT health management.

Size: 9 files, +626/−73. About two thirds of those lines are tests.

Testing

  • pre-commit run --all-files: ruff, mypy, codespell and the other hooks pass. Unit tests: 3297 passed. MQTT broker tests (-m mqtt_broker) against EMQX from compose.yaml: 11 passed.
  • Home Assistant 2026.11, Roborock integration as a custom component pinned to this branch. Devices: Saros 10R (V1, local + MQTT) and F25 Ultra (A01, MQTT only) on one account.

What we observed

These are observations from one account, not an explanation.

  • Lifetime: the Saros's MQTT replies stop roughly 3–4 hours after a connection is opened, sometimes later on days with cleaning.
  • Scope: only the Saros's replies are lost. The client keepalive keeps passing, and the F25 keeps sending and receiving on the same connection. The Roborock app controls the Saros normally during the outage.
  • Topics: they are identical before and after a reconnect. Only the client id and the broker session change.
  • What fixes it: a new connection, every time. After each forced restart the Saros answered on the next request.

We can't tell whether the broker stops forwarding our requests to the device or the device's replies to us. Requests are QoS 0, so nothing confirms delivery.

Results with the earlier version of this branch

These came from the previous version, which counted timeouts within a 30-minute window. The restart itself and the immediate reconnect are unchanged.

  • Forced restart: each one reconnects in 0.16–0.20 s. The F25 had no timeouts at any of them.
  • Failure during cleaning: the session was restarted 82 s after the first map timeout, and the Saros answered 31 s later.
  • Saros powered off for about 2 h 20 min: the session was restarted only 3 times, with growing intervals. The F25 had 150 successful requests and 0 timeouts.
  • Internet outage: a broker disconnect followed by DNS failures was handled by the normal backoff.

What didn't work, or was dropped

  • Resubscribing the device's topic on the same connection, after it failed or preventively every 2.5 h. The Saros stayed silent. Only a new connection helped.
  • A periodic MQTT probe (GET_NETWORK_INFO every 10 minutes). It found broken topics while the vacuum was idle, but that meant a restart every 3–4 hours that nobody needed. Without it, a broken topic is found by the next map requests.
  • Counting timeouts within a 30-minute window to catch partial failures, where every other request is still answered. Replaced with consecutive timeouts after review, to avoid restarts on flaky Wi-Fi. A partial failure is then caught only once 3 requests in a row time out.

Considered and not done

  • One MQTT session per device. It goes against the shared-session design, adds broker connections and rate-limit risk, and the shared restart costs other devices only ~0.2 s.
  • Fetching the V1 map over the local connection. The docs say MQTT is the only way to get the map.
  • A proactive reconnect every ~2.5 h. The lifetime isn't fixed (3–6 h), and the reactive restart already handles it.

Follow-ups

  • mqtt_health diagnostics per device (failing since, last success and timeout, recent restarts), in a separate PR after this one.
  • B01 (Q7/Q10) channels don't report to the health manager. Q7 uses request/response and could be wired in the same way as A01. Q10 has no replies to time out. I have no B01 device to test with.

The broker can stop delivering one device's messages while the shared
session keeps working for the others. The health manager counted
timeouts for the whole session and any successful response reset the
count, so a working device hid a failing one and the session was never
restarted (Python-roborock#1012). Subscribing to the topic again on the same connection
does not help; only a new connection does.

Also count consecutive timeouts per device, reset only by that device's
own response, and restart the session after 3 of them. Timeouts from
several devices in a row still restart the session at once.

A restart can't fix a device that is offline, so after a device restarts
the session it may do so again only after 1 hour, then 2 and 4 hours,
and at most every 6 hours, until it responds again. While it waits, its
timeouts don't count towards the session either, so a stray timeout of
another device doesn't bypass that interval.
A restart requested by the health manager disconnects a session whose
broker was reachable, so waiting for the backoff interval only kept every
device on the session without MQTT for ten seconds. Reconnect at once
after a restart of a connected session; connections lost for any other
reason, a restart while still connecting, and a failed first reconnect
attempt still wait for the backoff.

Every new connection also clears the timeouts counted on the previous
one, and the session tells the health manager whether it is connected so
that timeouts during a reconnect don't trigger a restart.
The health management section described local connection recovery as the
HealthManager and stated that A01/B01 devices don't need one. Split it
into local connection recovery and MQTT health management, covering the
restart conditions and their pacing.
infy1995 added a commit to infy1995/python-roborock that referenced this pull request Nov 29, 2026
@infy1995
infy1995 force-pushed the fix/mqtt-device-health branch from 604ad59 to 2ae567c Compare November 29, 2026 09:34
@github-actions github-actions Bot added documentation Improvements or additions to documentation V1 V1 platform labels Nov 29, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation V1 V1 platform

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shared HealthManager never restarts the MQTT session when one device stops receiving MQTT messages and another device on the account keeps working

2 participants