Skip to content

Commit 67c688d

Browse files
committed
fix(python): skip stale protocol v1 responses
1 parent 5063a4c commit 67c688d

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

python/.changelog.d/6859.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Skip stale protocol v1 responses on probing.

python/src/trezorlib/protocol_v1.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from .log import DUMP_BYTES
3030
from .thp import pairing
3131
from .tools import enter_context
32+
from .transport import Timeout
3233

3334
if t.TYPE_CHECKING:
3435
from .mapping import ProtobufMapping
@@ -350,6 +351,12 @@ def probe(
350351
transport: Transport, *, mapping: ProtobufMapping = mapping.DEFAULT_MAPPING
351352
) -> bool:
352353
"""Probe the transport to see if it supports protocol v1."""
354+
while True:
355+
try:
356+
resp = read(transport, _ignore_bad_magic=True, timeout=0.1)
357+
LOG.debug("stale response: %s", resp)
358+
except Timeout:
359+
break
353360
cancel_msg = messages.Cancel()
354361
cancel_msg_type, cancel_msg_bytes = mapping.encode(cancel_msg)
355362
write(transport, cancel_msg_type, cancel_msg_bytes)

tests/device_tests/test_basic.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import pytest
1818

1919
from trezorlib import messages, models
20+
from trezorlib.client import get_client
2021
from trezorlib.debuglink import DebugSession as Session
2122
from trezorlib.debuglink import TrezorTestContext as Client
2223

@@ -72,3 +73,15 @@ def test_not_initialized(session: Session):
7273
messages.FailureType.InvalidSession,
7374
)
7475
assert resp.code == expected[session.test_ctx.is_thp()]
76+
77+
78+
def test_desync(client: Client):
79+
with client.get_session(passphrase=None) as session:
80+
resp = session.call_raw(messages.Ping(message="test", button_protection=True))
81+
messages.ButtonRequest.ensure_isinstance(resp)
82+
session.write(messages.ButtonAck())
83+
session.debug.press_no()
84+
# don't read the response - simulating host disconnection
85+
86+
# Creating a new client not fail (https://github.com/trezor/trezor-firmware/issues/6859)
87+
get_client(client.app, client.transport).ping("reconnect")

0 commit comments

Comments
 (0)