The serial link check walks the ports off the event loop
Every two seconds the liveness watcher asks whether the companion's serial port is still there, and that question is a walk over /sys/class/tty: about 30 ms of small reads on an idle PicoCalc. It ran on the event loop. Each read hands the GIL back and has to wait its turn to take it again, and while a worker thread is busy (a map frame being drawn, a tile being decoded) that turn is up to 5 ms away, hundreds of times over. A freeze watchdog on the device caught the main thread inside this walk in 20 of the 21 stack samples taken while the map was stalled, for up to 1.2 s at a time. It now runs in asyncio.to_thread, as the device picker's and the reconnect dialog's walks already did; the worker pays the wait and the screen doesn't. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
61d7463ddf
commit
bdbf65312a
2 files changed
+28
-1
No files matched your search
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user