Skip to content

Run Binary Ninja's MCRIT requests off the UI thread - #31

Merged
danielplohmann merged 7 commits into
familiary:mainfrom
r0ny123:binja-ui-thread
Oct 12, 2026
Merged

danielplohmann merged 7 commits into
familiary:mainfrom
r0ny123:binja-ui-thread

Conversation

@r0ny123

@r0ny123 r0ny123 commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

Closes #25.

On Binary Ninja, uploading the report, Function Scope and Block Scope queries, and fetching a matching result ran on the UI thread. A slow or unreachable MCRIT server froze Binary Ninja for up to the request timeout, and with live queries on, that happened on every cursor move.

How

  • Backend.run_request(work, on_done) runs work, the requests only, and then on_done on the UI thread.
    • Binary Ninja runs work on a plain thread, without the analysis wait that run_background has for exports.
    • The default runs synchronously, so IDA and the headless backend behave exactly as before.
  • Widgets: each reads what its request needs on the UI thread (the query report, the score threshold, the block hashes) and renders when the answer arrives.
    • An answer for a function the cursor has left is cached but not rendered.
    • When everything a render needs is cached, as after a spinbox or filter change, it renders at once without going through a thread. That keeps those interactions instant, and keeps the GUI test's immediate checks meaningful.
  • Result fetch: the job list, the job request and the result download each run off the UI thread. The chooser dialog and the Overview update stay on it.
  • McritInterface.uploadReport no longer refreshes the local info widget. The upload button's completion does that on the UI thread. The activity line was already signal-based and safe from any thread.
  • Unchanged: the upload offered when a file closes stays synchronous. So does the label fetch in the Function Overview, and the entry lookups behind a double-click, which were not part of Binary Ninja: move the plugin's network requests off the UI thread #25.

Checks

Local, against MCRIT 1.12.0 and smda 4.9.0:

  • ruff is clean, pytest passes 208 plus one PySide6 test that is skipped without it, and the sync checks and actionlint pass.
  • The Binary Ninja 6.0 GUI test passes all 173 checks. It now waits for the job request and the result fetch, and checks three more things:
    • the job request runs off the UI thread;
    • a Function Scope query runs off the UI thread;
    • a late answer for a function the cursor left is not rendered. That check fails when the guard is removed.
  • The IDA 9.3 GUI test in autonomous mode, -A (145 checks), the IDALib test and the headless test pass unchanged.

Uploading the report, Function Scope and Block Scope queries, and
fetching a matching result ran on the UI thread, so a slow or
unreachable server froze Binary Ninja for up to the request timeout, and
with live queries on that happened on every cursor move.

Backend.run_request(work, on_done) runs work, the requests only, and
then on_done on the UI thread. Binary Ninja runs work on a plain thread,
without the analysis wait an export needs. The default, and so IDA and
the headless backend, runs it synchronously as before. McritInterface's
upload no longer refreshes the local info widget itself; the upload
button's completion does. The activity line was already safe to update
from any thread.

The widgets read what the request needs (the query report, the score
threshold, the block hashes) on the UI thread, and render only when the
answer arrives. An answer for a function the cursor has left is cached
but not shown. When everything a render needs is cached, as after a
filter change, it renders at once without a request.

The Binary Ninja GUI test waits for the job request and the result
fetch. It checks that the job request and a Function Scope query run off
the UI thread, and that a late answer for a function the cursor left is
not rendered. That last check fails without the guard.

Closes familiary#25

@danielplohmann danielplohmann left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, moving the requests off the UI thread is the right direction, and the cached-path shortcut keeps filter changes snappy. The suite passes locally (211 tests). We have no Binary Ninja here, so I checked the threading by reading the code paths and ran what could run in isolation.

Four things to fix before merge, all from the UI thread now being free while a request runs:

  1. A label can be applied to the wrong function (inline on FunctionMatchWidget.py).
  2. Block Scope can render a stale answer for an older block-size threshold (inline on BlockMatchWidget.py).
  3. matched_function_entries is now mutated from a worker while the Function Overview may iterate it (inline on FunctionMatchWidget.py).
  4. A raising request leaves the widget on "Querying…" with no warning (inline on BinjaBackend.py).

Smaller, fine as a follow-up: Upload and Get Match Result stay enabled while their request runs, so a second click starts a second request (a second ResultChooserDialog, possibly a duplicate matching job). And Function Scope does not reset last_viewed when the remote cache is unavailable, while Block Scope resets last_viewed_function, so only Block Scope retries after the server comes back.

Comment thread mcrit_plugin/binja/BinjaBackend.py Resolved
Function Scope's query now runs off the UI thread, and last_viewed moves
to the new function as soon as the query starts. Until its answer
arrives, the names table still lists the previous function's labels and
stays clickable, so a double-click applied an old function's label to
the new one. The label now goes to current_function_offset, which is set
when the tables are filled.
Block Scope dropped an answer only when it was for another function. Two
requests for the same function with different block-size thresholds
(4, then 8 while the first was still running) both passed, and whichever
answered last was shown, so the table could list the blocks for 4 while
the spin box said 8. Each view now takes a number, and an answer is shown
only for the latest one. The function check stays, since with live
tracking off the cursor moves on without asking for a view.
queryFunctionEntriesById now runs on a worker thread for Function Scope,
and updated matched_function_entries in place while the Function
Overview may iterate the same dict on the UI thread, which raises
"dictionary changed size during iteration". Its None-to-{} check was
also a check-then-set between two workers. It now merges into a copy
under a lock and swaps the copy in, so a loop over the old dict is not
disturbed and no worker's entries are lost.
When a request raised, run_request only logged it and never called
on_done, so the widget stayed on "Querying matches for function 0x..."
with nothing on screen saying why. It now shows a warning like
run_background does.
When the family and sample lists could not be fetched, Block Scope
cleared last_viewed_function so the next cursor move in the function
tried again, but Function Scope kept last_viewed and skipped that
function until the cursor left it. It now clears last_viewed too.
Both now run off the UI thread and their actions stayed enabled, so a
second click started a second request: another upload, or a second
result chooser dialog after the first, each able to request a new
matching job. Upload is disabled until the upload answers, and Get Match
Result until the chosen result is shown or the job query fails.
@r0ny123

r0ny123 commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for checking the threading so carefully. All four points are fixed, plus both follow-ups; details are in the inline replies.

  • 210b0c7: a label goes to the function the tables show.
  • 15eb797: Block Scope shows only the latest view's answer, which covers block size changes too.
  • 0156def: matched_function_entries is merged into a copy under a lock and swapped in.
  • 1a7a650: a failed request shows a warning.
  • 55a8c80: Upload and Get Match Result are disabled while their request runs.
  • 9008a7a: Function Scope retries after the server comes back, like Block Scope.

New tests cover the Block Scope and cache fixes, and each fails without its fix. The suite passes (213 tests), and the Binary Ninja 6 GUI integration test passes against a local MCRIT 1.12.0 (173 checks, 0 failures).

The later PRs in the stack (#33–#41) have these commits merged in.

@danielplohmann danielplohmann left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, all four fixes and both follow-ups hold up. I checked each commit against its inline thread. updateActivityInfo goes through a signal, so the request methods can call it from the worker. Lint, the sync checks and the suite (213 tests) pass here, and merged with #30 the suite passes too (223 tests).

@danielplohmann
danielplohmann merged commit c816fea into familiary:main Oct 12, 2026
10 checks passed
@r0ny123
r0ny123 deleted the binja-ui-thread branch October 15, 2026 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Binary Ninja: move the plugin's network requests off the UI thread

2 participants