Skip to content

Commit 647ea28

Browse files
art804claude
andcommitted
fix: don't resend non-read commands over MQTT after a local timeout
A V1 command that timed out on the local channel was retried over MQTT. By then the request had already been published, so the device may have received and run it, and the retry could run a write command (start, set, save map) twice. Only read-only commands (get_*, app_get_*) are now retried after a timeout; others raise RoborockTimeout. Failures before publish still fall back as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ed5f859 commit 647ea28

2 files changed

Lines changed: 57 additions & 4 deletions

File tree

‎roborock/devices/rpc/v1_channel.py‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from roborock.devices.transport.channel import Channel
1818
from roborock.devices.transport.local_channel import LocalChannel, LocalSession, create_local_session
1919
from roborock.devices.transport.mqtt_channel import MqttChannel
20-
from roborock.exceptions import RoborockException
20+
from roborock.exceptions import RoborockException, RoborockTimeout
2121
from roborock.mqtt.health_manager import HealthManager
2222
from roborock.mqtt.session import MqttParams, MqttSession
2323
from roborock.protocols.v1_protocol import (
@@ -47,6 +47,12 @@
4747

4848
_T = TypeVar("_T", bound=RoborockBase)
4949
_TIMEOUT = 10.0
50+
_READ_ONLY_PREFIXES = ("get_", "app_get_")
51+
52+
53+
def _is_read_only(method: CommandType) -> bool:
54+
"""Return True if the command only reads state, so it is safe to resend after a timeout."""
55+
return method.startswith(_READ_ONLY_PREFIXES)
5056

5157

5258
# Exponential backoff parameters for reconnecting to local
@@ -89,10 +95,18 @@ async def send_command(
8995
request = RequestMessage(method, params=params)
9096

9197
# Try each channel in order until one succeeds
92-
last_exception = None
98+
last_exception: RoborockException | None = None
9399
for strategy in self._rpc_strategies_cb():
94100
try:
95101
decoded_response = await self._send_rpc(strategy, request, self._logger)
102+
except RoborockTimeout as e:
103+
# The request was published, so the device may have received and run it. Only
104+
# read-only commands are safe to resend on another channel; resending anything
105+
# else could make the device act twice.
106+
if not _is_read_only(method):
107+
raise
108+
self._logger.debug("Command %s timed out on %s channel: %s", method, strategy.name, e)
109+
last_exception = e
96110
except RoborockException as e:
97111
self._logger.debug("Command %s failed on %s channel: %s", method, strategy.name, e)
98112
last_exception = e
@@ -154,7 +168,7 @@ def find_response(response_message: RoborockMessage) -> None:
154168
if strategy.health_manager:
155169
await strategy.health_manager.on_timeout()
156170
future.cancel()
157-
raise RoborockException(f"Command timed out after {_TIMEOUT}s") from ex
171+
raise RoborockTimeout(f"Command timed out after {_TIMEOUT}s") from ex
158172
finally:
159173
unsub()
160174
if strategy.health_manager:

‎tests/devices/rpc/test_v1_channel.py‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from roborock.devices.cache import DeviceCache, DeviceCacheData, InMemoryCache
1717
from roborock.devices.rpc.v1_channel import V1Channel
1818
from roborock.devices.transport.local_channel import LocalSession
19-
from roborock.exceptions import RoborockException
19+
from roborock.exceptions import RoborockException, RoborockTimeout
2020
from roborock.protocol import (
2121
create_local_decoder,
2222
create_local_encoder,
@@ -433,6 +433,45 @@ async def test_v1_channel_send_pick_first_available(
433433
assert result.state == RoborockStateCode.cleaning
434434

435435

436+
@pytest.mark.parametrize(
437+
("command", "expect_mqtt_resend"),
438+
[
439+
(RoborockCommand.GET_STATUS, True),
440+
(RoborockCommand.APP_START, False),
441+
],
442+
ids=["read-falls-back-to-mqtt", "write-not-resent-on-mqtt"],
443+
)
444+
async def test_v1_channel_send_command_local_timeout(
445+
v1_channel: V1Channel,
446+
rpc_channel: V1RpcChannel,
447+
mock_mqtt_channel: Mock,
448+
mock_local_channel: Mock,
449+
command: RoborockCommand,
450+
expect_mqtt_resend: bool,
451+
) -> None:
452+
"""A command that times out locally is only resent over MQTT if it is read-only.
453+
454+
After a timeout the request has already been published, so the device may have run it.
455+
"""
456+
mock_mqtt_channel.response_queue.append(TEST_NETWORK_INFO_RESPONSE)
457+
await v1_channel.subscribe(Mock())
458+
mqtt_published_before = len(mock_mqtt_channel.published_messages)
459+
460+
# Local publish succeeds but no response arrives; MQTT would answer.
461+
mock_mqtt_channel.response_queue.append(TEST_RESPONSE)
462+
with patch("roborock.devices.rpc.v1_channel._TIMEOUT", 0.01):
463+
if expect_mqtt_resend:
464+
result = await rpc_channel.send_command(command, response_type=StatusV2)
465+
assert result.state == RoborockStateCode.cleaning
466+
else:
467+
with pytest.raises(RoborockTimeout):
468+
await rpc_channel.send_command(command)
469+
470+
mock_local_channel.publish.assert_called_once()
471+
mqtt_resent = len(mock_mqtt_channel.published_messages) > mqtt_published_before
472+
assert mqtt_resent == expect_mqtt_resend
473+
474+
436475
async def test_v1_channel_send_decoded_command_with_params(
437476
v1_channel: V1Channel,
438477
rpc_channel: V1RpcChannel,

0 commit comments

Comments
 (0)