feat(trezorlib): accept a bootloader run that ends before the debug link answers [no changelog]
What changed, and why it matters
This commit changes an internal test helper in Trezor's Python library so that when a simulated bootloader finishes very quickly (because it immediately boots into trusted firmware), the test code treats that as a normal result instead of throwing an error. It is a testing/QA reliability fix, not a security patch for user-facing code.
No security action required. Treat as a normal test-infrastructure improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change is in python/src/trezorlib/_internal/bootloader_emulator.py. _wait_until_ready() now returns False when the bootloader emulator process exits before the debug port responds, instead of raising RuntimeError. start() then returns early without creating a client context. A running property was added. This only affects internal emulator-based tests and does not alter firmware, device, or production library behavior.
Changed components
python/src/trezorlib/_internal/bootloader_emulator.pyInspect captured patch +23 / −6
### python/src/trezorlib/_internal/bootloader_emulator.py
@@ -348,6 +348,10 @@ def ctx(self) -> BootloaderTestContext:
raise RuntimeError("Emulator not started, or its run already ended")
return self._ctx
+ @property
+ def running(self) -> bool:
+ return self.process is not None and self.process.poll() is None
+
def make_args(self) -> list[str]:
return self.startup.to_args()
@@ -367,9 +371,14 @@ def make_env(self) -> dict[str, str]:
def _get_debug_transport(self) -> UdpTransport:
return UdpTransport(f"127.0.0.1:{self.debug_port}")
- def _wait_until_ready(self, timeout: float = emulator.EMULATOR_WAIT_TIME) -> None:
- # Probe the debug port: the wire port is polled only on host-facing
- # screens, the debug port on every screen.
+ def _wait_until_ready(self, timeout: float = emulator.EMULATOR_WAIT_TIME) -> bool:
+ """Wait for the debug port to answer.
+
+ Returns False when the run ended first. That is a legitimate
+ outcome: a boot into trusted firmware takes the bootloader no time
+ at all. The wire port is not probed because it is polled only on
+ host-facing screens.
+ """
assert self.process is not None, "Emulator not started"
probe = self._get_debug_transport()
probe.open()
@@ -380,15 +389,18 @@ def _wait_until_ready(self, timeout: float = emulator.EMULATOR_WAIT_TIME) -> Non
if probe.is_ready():
break
if self.process.poll() is not None:
- raise RuntimeError(
- f"Emulator exited prematurely (code {self.process.returncode})"
+ LOG.info(
+ "Bootloader emulator ended before answering (code %d)",
+ self.process.returncode,
)
+ return False
if time.monotonic() - start >= timeout:
raise TimeoutError("Can't connect to bootloader emulator")
time.sleep(0.1)
finally:
probe.close()
LOG.info("Bootloader emulator ready after %.3fs", time.monotonic() - start)
+ return True
def start(self, startup: BootloaderStartup | None = None) -> None:
if startup is not None:
@@ -421,11 +433,16 @@ def start(self, startup: BootloaderStartup | None = None) -> None:
# a failed start must not leave the process holding its ports
try:
try:
- self._wait_until_ready()
+ ready = self._wait_until_ready()
except TimeoutError:
LOG.warning("Bootloader emulator did not come up in time")
raise
+ if not ready:
+ # the run is over; wait_exit() reports how it ended and there
+ # is no client to talk to
+ return
+
(self.profile_dir / "trezor.pid").write_text(str(self.process.pid) + "\n")
(self.profile_dir / "trezor.port").write_text(str(self.port) + "\n")
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.