diff --git a/core/src/session.py b/core/src/session.py index 4f161c36ce..1ecbc467be 100644 --- a/core/src/session.py +++ b/core/src/session.py @@ -22,9 +22,8 @@ workflow.start_default() # initialize the wire codec wire.setup(usb.iface_wire) -if __debug__: - wire.setup(usb.iface_debug, is_debug_session=True) +# start the event loop loop.run() if __debug__: diff --git a/core/src/trezor/wire/__init__.py b/core/src/trezor/wire/__init__.py index 4c67a2ae8d..a2c5127567 100644 --- a/core/src/trezor/wire/__init__.py +++ b/core/src/trezor/wire/__init__.py @@ -53,9 +53,9 @@ if TYPE_CHECKING: EXPERIMENTAL_ENABLED = False -def setup(iface: WireInterface, is_debug_session: bool = False) -> None: +def setup(iface: WireInterface) -> None: """Initialize the wire stack on passed USB interface.""" - loop.schedule(handle_session(iface, codec_v1.SESSION_ID, is_debug_session)) + loop.schedule(handle_session(iface, codec_v1.SESSION_ID)) def wrap_protobuf_load( @@ -87,9 +87,7 @@ if __debug__: WIRE_BUFFER_DEBUG = bytearray(PROTOBUF_BUFFER_SIZE_DEBUG) -async def _handle_single_message( - ctx: context.Context, msg: codec_v1.Message, use_workflow: bool -) -> bool: +async def _handle_single_message(ctx: context.Context, msg: codec_v1.Message) -> bool: """Handle a message that was loaded from USB by the caller. Find the appropriate handler, run it and write its result on the wire. In case @@ -148,14 +146,7 @@ async def _handle_single_message( # communication inside, but it should eventually return a # response message, or raise an exception (a rather common # thing to do). Exceptions are handled in the code below. - if use_workflow: - # Spawn a workflow around the task. This ensures that concurrent - # workflows are shut down. - res_msg = await workflow.spawn(context.with_context(ctx, task)) - else: - # For debug messages, ignore workflow processing and just await - # results of the handler. - res_msg = await task + res_msg = await workflow.spawn(context.with_context(ctx, task)) except context.UnexpectedMessage: # Workflow was trying to read a message from the wire, and @@ -198,22 +189,10 @@ async def _handle_single_message( return msg.type in AVOID_RESTARTING_FOR -async def handle_session( - iface: WireInterface, session_id: int, is_debug_session: bool = False -) -> None: - if __debug__ and is_debug_session: - ctx_buffer = WIRE_BUFFER_DEBUG - else: - ctx_buffer = WIRE_BUFFER - - ctx = context.Context(iface, session_id, ctx_buffer) +async def handle_session(iface: WireInterface, session_id: int) -> None: + ctx = context.Context(iface, session_id, WIRE_BUFFER) next_msg: codec_v1.Message | None = None - if __debug__ and is_debug_session: - import apps.debug - - apps.debug.DEBUG_CONTEXT = ctx - # Take a mark of modules that are imported at this point, so we can # roll back and un-import any others. modules = utils.unimport_begin() @@ -236,9 +215,7 @@ async def handle_session( next_msg = None try: - do_not_restart = await _handle_single_message( - ctx, msg, use_workflow=not is_debug_session - ) + do_not_restart = await _handle_single_message(ctx, msg) except context.UnexpectedMessage as unexpected: # The workflow was interrupted by an unexpected message. We need to # process it as if it was a new message... @@ -252,17 +229,13 @@ async def handle_session( if __debug__: log.exception(__name__, exc) finally: - if not __debug__ or not is_debug_session: - # Unload modules imported by the workflow. Should not raise. - # This is not done for the debug session because the snapshot taken - # in a debug session would clear modules which are in use by the - # workflow running on wire. - utils.unimport_end(modules) + # Unload modules imported by the workflow. Should not raise. + utils.unimport_end(modules) - if not do_not_restart: - # Let the session be restarted from `main`. - loop.clear() - return # pylint: disable=lost-exception + if not do_not_restart: + # Let the session be restarted from `main`. + loop.clear() + return # pylint: disable=lost-exception except Exception as exc: # Log and try again. The session handler can only exit explicitly via