refactor(core): move UI ButtonRequest-related code into ButtonRequestHandler#6741
refactor(core): move UI ButtonRequest-related code into ButtonRequestHandler#6741
ButtonRequest-related code into ButtonRequestHandler#6741Conversation
WalkthroughButton-request handling was reworked: ButtonRequestHandler now owns an internal mailbox and lifecycle methods Sequence Diagram(s)sequenceDiagram
participant Layout
participant BRHandler as ButtonRequestHandler
participant Firmware as Firmware/Transport
participant AckCB as AckCallback
Layout->>BRHandler: br_task(ack_callback)
Note right of BRHandler: starts internal _handle loop (task)
Layout->>BRHandler: put(ButtonRequest)
BRHandler->>BRHandler: enqueue request in internal box
BRHandler->>Firmware: send ButtonRequest / await ButtonAck
Firmware-->>BRHandler: ButtonAck received
BRHandler->>AckCB: invoke ack_callback()
AckCB-->>Layout: notify _button_request_acked
Layout->>BRHandler: join(_waiting_screen_task)
BRHandler->>BRHandler: enqueue sentinel (None) and finish loop
BRHandler-->>Layout: join returns (handler completed)
Estimated code review effort🎯 4 (Complex) | ⏱️ ~35 minutes 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
core/src/trezor/ui/__init__.py (1)
342-350: Minor: Redundantmsg andcheck.On line 346, the
msg and ButtonRequest(...)pattern is redundant sincemsgis already checked forNoneon line 343. However, this is a minor style issue and doesn't affect correctness.♻️ Optional cleanup
def put_button_request(self, msg: ButtonRequestMsg | None) -> bool: if self.button_request_handler is None or msg is None: return False - br = msg and ButtonRequest( + br = ButtonRequest( code=msg[0], name=msg[1], pages=self.layout.page_count() ) self.button_request_handler.put(br) return True🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@core/src/trezor/ui/__init__.py` around lines 342 - 350, In put_button_request, remove the redundant "msg and" when building the ButtonRequest since msg is already checked for None; directly construct br = ButtonRequest(code=msg[0], name=msg[1], pages=self.layout.page_count()) and then call self.button_request_handler.put(br) (symbols: put_button_request, ButtonRequest, button_request_handler, ButtonRequestMsg).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@core/src/trezor/ui/__init__.py`:
- Around line 342-350: In put_button_request, remove the redundant "msg and"
when building the ButtonRequest since msg is already checked for None; directly
construct br = ButtonRequest(code=msg[0], name=msg[1],
pages=self.layout.page_count()) and then call
self.button_request_handler.put(br) (symbols: put_button_request, ButtonRequest,
button_request_handler, ButtonRequestMsg).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 7c20ca49-5e1e-4548-a848-035bd21f995f
📒 Files selected for processing (2)
core/src/trezor/ui/__init__.pycore/src/trezor/wire/protocol_common.py
204a167 to
d6a847e
Compare
7df7642 to
6dbb78e
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@core/src/trezor/wire/protocol_common.py`:
- Around line 166-171: The join() method currently uses self.box.put(None,
replace=True) which can overwrite a queued ButtonRequest and cause the handler
(_handle) to exit without sending/acking the last prompt; change this to enqueue
a shutdown sentinel without replacing existing items — e.g. call
self.box.put(None) (omit replace=True) or, better, enqueue a unique sentinel
object (SENTINEL = object()) via self.box.put(SENTINEL) so the mailbox will not
drop any queued ButtonRequest and the _handle loop can process all work before
seeing the sentinel.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 95dcfc04-739c-46e8-9ddf-7302f3a13a36
📒 Files selected for processing (2)
core/src/trezor/ui/__init__.pycore/src/trezor/wire/protocol_common.py
🚧 Files skipped from review as they are similar to previous changes (1)
- core/src/trezor/ui/init.py
ButtonRequest-related code into ButtonRequestHandlerButtonRequest-related code into ButtonRequestHandler
6dbb78e to
d0a935d
Compare
d0a935d to
830ee66
Compare
c754fc8 to
374d32f
Compare
mmilata
left a comment
There was a problem hiding this comment.
Thanks for making this limited in scope, it's still scary:) Have you tried running the tests on hardware? Might help discover potential timing issues, unless we want to merge this before freeze.
| has_br = self.put_button_request(self.layout.button_request()) | ||
| if __debug__: | ||
| self._is_attached = not has_br | ||
| notify_layout_change(self) |
There was a problem hiding this comment.
Why is it ok to call this if has_br is True? IIUC previously this was not the case.
| self.ctx = ctx | ||
| self.box: loop.mailbox[ButtonRequest | None] = loop.mailbox() | ||
| self.is_done: loop.mailbox[None] = loop.mailbox() | ||
| self.pending = False |
There was a problem hiding this comment.
Nit: consider adding short explanations of what each member variable means?
There was a problem hiding this comment.
c4e1d20 (also moved self.pending under __debug__)
|
Ran device tests on T3T1 @ 374d32f: Also ran them on T3B1 @ c4e1d20 |
|
Let's merge it after the freeze - it's not needed for the upcoming release. |
c4e1d20 to
80f484b
Compare
…Handler` Also, minimize context-related access in `ui.Layout`. [no changelog]
80f484b to
1a197d9
Compare




























































































































































Also, minimize context-related access in
ui.Layoutand move debug-specific functionality under__debug__.Will be used to resolve #6348 (Case 1) by #6651 (stopping button-request handling in backup flow after an error/timeout).
Existing functionality doesn't change, so AFAIU there is no specific QA needed.