fix(uploader): shorter wait and nicer port paths (#28514)

* fix(uploader): shorten the wait for a bootloader after reboot

Reboot-to-bootloader is immediate, so this wait only has to cover USB
re-enumeration, not the reboot. Five seconds of it meant a port that was
never going to answer held up every other port on the list, and it could
outlast the window it was trying to catch: a board sits in its bootloader
for BOOTLOADER_DELAY, 3s on some boards and 5s on most, before it jumps to
the application.

With an FMU-v6C and a Black Magic probe attached, a pass over the probe's
two CDC nodes drops from 14.75s to 5.45s, and a running application is in
its bootloader 3.52s after the uploader starts.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Julian Oes <julian@oes.ch>

* fix(uploader): report ports by their /dev/serial/by-id name

A ttyACM number is handed out in plug order and says nothing about which
device it belongs to, which is awkward as soon as there is more than one
CDC device on the bus -- "Attempting reboot on /dev/ttyACM0" gives no hint
that it is a debug probe rather than the board.

Resolve each detected port back to its by-id symlink when one exists, so
every message names the device. This also collapses a duplicate: a board
is matched both by a by-id pattern and by the /dev/ttyACM* catch-all, so
it was being probed twice per pass. With an FMU-v6C and a Black Magic
probe attached, detection goes from four entries with two naming the same
board to three, each identifying itself.

Linux only; elsewhere, and for ports with no symlink, paths are unchanged.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Julian Oes <julian@oes.ch>

---------

Signed-off-by: Julian Oes <julian@oes.ch>
This commit is contained in:
Julian Oes authored and GitHub committed 2026-09-02 16:48:32 -06:00
1 parent 19ce32980e
commit c3328cfe04
1 file changed
+49 -6
+49 -6
View File
@@ -143,6 +143,32 @@ def _present_ports() -> Optional[set]:
return None
_BY_ID_DIR = "/dev/serial/by-id"
def _by_id_name(port: str) -> str:
"""Prefer the stable /dev/serial/by-id name for a port, when one exists.
A by-id name says which device it is; ttyACM numbers are handed out in plug
order and mean nothing to a reader with more than one board on the bus.
Linux only -- elsewhere, and for ports with no symlink, the path is
returned unchanged.
"""
if port.startswith(_BY_ID_DIR):
return port
try:
real = os.path.realpath(port)
for name in sorted(os.listdir(_BY_ID_DIR)):
link = os.path.join(_BY_ID_DIR, name)
if os.path.realpath(link) == real:
return link
except OSError as e:
logger.debug(f"Cannot resolve by-id name for {port}: {e}")
return port
# =============================================================================
# Exception Hierarchy
# =============================================================================
@@ -1443,8 +1469,19 @@ class PortDetector:
ports.discard(port)
result.extend(sorted(ports))
logger.info(f"Detected {len(result)} potential ports: {result}")
return result
# Report by-id names, and collapse the duplicates that leaves: one
# device is routinely matched both by a by-id pattern and by the
# /dev/ttyACM* catch-all.
named = []
seen = set()
for port in result:
port = _by_id_name(port)
if port not in seen:
seen.add(port)
named.append(port)
logger.info(f"Detected {len(named)} potential ports: {named}")
return named
def _detect_by_vid_pid(self) -> list[str]:
"""Detect ports by USB Vendor/Product ID.
@@ -1527,7 +1564,7 @@ class PortDetector:
else:
logger.debug(f"Exact port not present: {pattern}")
return list(dict.fromkeys(ports))
return list(dict.fromkeys(_by_id_name(port) for port in ports))
# =============================================================================
@@ -1896,7 +1933,12 @@ class Uploader:
# After reboot-to-bootloader the USB CDC device disappears and comes back
# under a different product string (app vs bootloader). Give it this long
# to re-enumerate before giving up on this attempt.
REBOOT_REDISCOVER_TIMEOUT = 5.0
#
# The reboot itself is immediate, so this only has to cover re-enumeration.
# Waiting longer is actively harmful: a board sits in its bootloader for
# BOOTLOADER_DELAY (3s on some boards, 5s on most) before it jumps to the
# application, and time spent here is time not spent trying other ports.
REBOOT_REDISCOVER_TIMEOUT = 1.0
def _try_identify(
self, transport: SerialTransport, protocol: BootloaderProtocol
@@ -2020,9 +2062,10 @@ class Uploader:
now = _present_ports()
appeared = (now - ports_before) if now is not None else set()
known = {os.path.realpath(port) for port in ports}
for port in sorted(appeared):
if port not in ports:
ports.append(port)
if os.path.realpath(port) not in known:
ports.append(_by_id_name(port))
def rank(port: str) -> int:
if port in appeared or os.path.realpath(port) in appeared: