diff --git a/meshterm/core/connection.py b/meshterm/core/connection.py index 27caa39..8e51289 100644 --- a/meshterm/core/connection.py +++ b/meshterm/core/connection.py @@ -2723,7 +2723,12 @@ class MeshCoreDevice(Device): return True if not self._port: return True # no port recorded (shouldn't happen once connected) — can't tell - return serial_port_present(self._port) + # Off the event loop, like every other caller of the port walk. It is ~30 ms of + # sysfs reads on the PicoCalc when nothing else is running, but each of its hundreds + # of small reads hands the GIL back and has to wait its turn to take it again — and + # with a map frame being drawn on a worker thread that turn is up to 5 ms away. On + # the loop, this check every two seconds froze the screen for up to 1.2 s a time. + return await asyncio.to_thread(serial_port_present, self._port) async def disconnect(self) -> None: """Close the connection and release resources. Idempotent, and bounded in time. diff --git a/tests/test_disconnect.py b/tests/test_disconnect.py index d9dbc3a..844e924 100644 --- a/tests/test_disconnect.py +++ b/tests/test_disconnect.py @@ -1139,6 +1139,28 @@ def test_serial_port_present_platform_uart_by_path(monkeypatch) -> None: assert not connection.serial_port_present("/dev/ttyUSB9") # node gone -> absent (unplug) +async def test_serial_link_check_walks_the_ports_off_the_event_loop(monkeypatch) -> None: + """The liveness poll never runs the port walk on the loop the screen paints from. + + The walk is hundreds of small sysfs reads, each one waiting its turn for the GIL; with + a map frame being drawn on a worker thread, running it on the loop froze the PicoCalc's + screen for up to 1.2 s every two seconds. + """ + import threading + + device = connection.MeshCoreDevice(port="/dev/ttyS1") + device._mc = SimpleNamespace(is_connected=True) + walked_on: list[threading.Thread] = [] + + def walk(port: str) -> bool: + walked_on.append(threading.current_thread()) + return True + + monkeypatch.setattr(connection, "serial_port_present", walk) + assert await device.link_present() + assert walked_on and walked_on[0] is not threading.main_thread() + + async def test_wait_for_disconnect_fires_when_port_vanishes(tmp_path: Path, monkeypatch) -> None: """The liveness watcher resolves once the connected device's port leaves enumeration.""" ctx = _make_ctx(tmp_path)