From c93b343ae516d25f5d527e33f5445fab9d72d84a Mon Sep 17 00:00:00 2001 From: Harry Coureau Date: Sun, 15 Nov 2026 13:57:43 +0000 Subject: [PATCH] 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. --- roborock/devices/transport/local_channel.py | 3 ++- tests/devices/transport/test_local_channel.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/roborock/devices/transport/local_channel.py b/roborock/devices/transport/local_channel.py index 413a90240..4f4f4356e 100644 --- a/roborock/devices/transport/local_channel.py +++ b/roborock/devices/transport/local_channel.py @@ -187,7 +187,8 @@ async def connect(self) -> None: loop = get_running_loop() protocol = _LocalProtocol(self._data_received, self._connection_lost) try: - self._transport, self._protocol = await loop.create_connection(lambda: protocol, self._host, _PORT) + async with asyncio.timeout(_TIMEOUT): + self._transport, self._protocol = await loop.create_connection(lambda: protocol, self._host, _PORT) self._is_connected = True except OSError as e: raise RoborockConnectionException(f"Failed to connect to {self._host}:{_PORT}") from e diff --git a/tests/devices/transport/test_local_channel.py b/tests/devices/transport/test_local_channel.py index 959a021d9..efe2fd50e 100644 --- a/tests/devices/transport/test_local_channel.py +++ b/tests/devices/transport/test_local_channel.py @@ -107,6 +107,23 @@ async def test_connection_failure(local_channel: LocalChannel, mock_loop: Mock) assert local_channel._is_connected is False +async def test_connection_timeout(local_channel: LocalChannel, mock_loop: Mock) -> None: + """Test that a connection attempt to an unreachable host times out.""" + + async def never_connect(*args: object) -> None: + await asyncio.Event().wait() + + mock_loop.create_connection.side_effect = never_connect + + with ( + patch("roborock.devices.transport.local_channel._TIMEOUT", 0.01), + pytest.raises(RoborockConnectionException, match="Failed to connect to 192.168.1.100:58867"), + ): + await local_channel.connect() + + assert not local_channel.is_connected + + async def test_close_connection(local_channel: LocalChannel, mock_loop: Mock, mock_transport: Mock) -> None: """Test closing the connection.""" await local_channel.connect()