Skip to content

Commit c93b343

Browse files
committed
fix: time out local connection attempts to unreachable devices
Connecting the local channel had no timeout, so when the device's LAN address was unreachable (packets dropped rather than refused) the TCP connect blocked until the operating system gave up. Device startup waits on this first attempt before subscribing over MQTT, so the device stayed disconnected and traits were not discovered even though MQTT worked. The connect now uses the existing local timeout, so startup falls back to MQTT and the background task keeps retrying local.
1 parent 321c6d5 commit c93b343

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

‎roborock/devices/transport/local_channel.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ async def connect(self) -> None:
187187
loop = get_running_loop()
188188
protocol = _LocalProtocol(self._data_received, self._connection_lost)
189189
try:
190-
self._transport, self._protocol = await loop.create_connection(lambda: protocol, self._host, _PORT)
190+
async with asyncio.timeout(_TIMEOUT):
191+
self._transport, self._protocol = await loop.create_connection(lambda: protocol, self._host, _PORT)
191192
self._is_connected = True
192193
except OSError as e:
193194
raise RoborockConnectionException(f"Failed to connect to {self._host}:{_PORT}") from e

‎tests/devices/transport/test_local_channel.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,23 @@ async def test_connection_failure(local_channel: LocalChannel, mock_loop: Mock)
107107
assert local_channel._is_connected is False
108108

109109

110+
async def test_connection_timeout(local_channel: LocalChannel, mock_loop: Mock) -> None:
111+
"""Test that a connection attempt to an unreachable host times out."""
112+
113+
async def never_connect(*args: object) -> None:
114+
await asyncio.Event().wait()
115+
116+
mock_loop.create_connection.side_effect = never_connect
117+
118+
with (
119+
patch("roborock.devices.transport.local_channel._TIMEOUT", 0.01),
120+
pytest.raises(RoborockConnectionException, match="Failed to connect to 192.168.1.100:58867"),
121+
):
122+
await local_channel.connect()
123+
124+
assert not local_channel.is_connected
125+
126+
110127
async def test_close_connection(local_channel: LocalChannel, mock_loop: Mock, mock_transport: Mock) -> None:
111128
"""Test closing the connection."""
112129
await local_channel.connect()

0 commit comments

Comments
 (0)