From 54661fb5f91bfc780495cc516d94b35e0c5206fe Mon Sep 17 00:00:00 2001 From: M1nd3r Date: Wed, 10 Apr 2024 14:43:11 +0200 Subject: [PATCH] Adjust loop clear --- core/src/trezor/wire/thp/channel.py | 13 +++++++++++-- core/src/trezor/wire/thp/session_context.py | 4 +--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/core/src/trezor/wire/thp/channel.py b/core/src/trezor/wire/thp/channel.py index f67a88d80..018f994e6 100644 --- a/core/src/trezor/wire/thp/channel.py +++ b/core/src/trezor/wire/thp/channel.py @@ -5,7 +5,7 @@ from typing import TYPE_CHECKING # pyright:ignore[reportShadowedImports] import usb from storage import cache_thp from storage.cache_thp import KEY_LENGTH, SESSION_ID_LENGTH, TAG_LENGTH, ChannelCache -from trezor import log, loop, protobuf, utils +from trezor import log, loop, protobuf, utils, workflow from trezor.enums import FailureType, MessageType # , ThpPairingMethod from trezor.messages import Failure from trezor.wire import message_handler @@ -534,9 +534,18 @@ class Channel(Context): try: await self.waiting_for_ack_timeout except loop.TaskClosed: - THP.sync_set_send_bit_to_opposite(self.channel_cache) break + THP.sync_set_send_bit_to_opposite(self.channel_cache) + + # Let the main loop be restarted and clear loop, if there is no other + # workflow and the state is ENCRYPTED_TRANSPORT + if ( + not workflow.tasks + and self.get_channel_state() is ChannelState.ENCRYPTED_TRANSPORT + ): + loop.clear() + async def _wait_for_ack(self) -> None: await loop.sleep(1000) diff --git a/core/src/trezor/wire/thp/session_context.py b/core/src/trezor/wire/thp/session_context.py index 0b3664165..55ad639a4 100644 --- a/core/src/trezor/wire/thp/session_context.py +++ b/core/src/trezor/wire/thp/session_context.py @@ -47,7 +47,7 @@ class SessionContext(Context): async def handle(self, is_debug_session: bool = False) -> None: if __debug__: - log.debug(__name__, "handle - start") + log.debug(__name__, "handle - start (session_id: %d)", self.session_id) if is_debug_session: import apps.debug @@ -98,8 +98,6 @@ class SessionContext(Context): and message.type not in AVOID_RESTARTING_FOR ): # Shut down the loop if there is no next message waiting. - # Let the session be restarted from `main`. - loop.clear() return # pylint: disable=lost-exception except Exception as exc: