Skip to content

fix(desktop): move crypto commands off the main thread - #1222

Merged
wpfleger96 merged 1 commit into
mainfrom
worktree-wpfleger-async-tauri-crypto
Jun 23, 2026
Merged

fix(desktop): move crypto commands off the main thread#1222
wpfleger96 merged 1 commit into
mainfrom
worktree-wpfleger-async-tauri-crypto

Conversation

@wpfleger96

Copy link
Copy Markdown
Member

Four Tauri commands in identity.rssign_event, create_auth_event, nip44_encrypt_to_self, and nip44_decrypt_from_self — were synchronous, running Schnorr signing and NIP-44 encryption directly on the webview main thread. Under burst traffic (reconnect replay resubscribing to 21+ channels, presence updates, read-state sync) this blocked the event loop long enough to trigger the macOS beachball and prevent WebSocket pong responses from being processed, contributing to relay disconnect cycles.

Investigation of local agent subprocess logs revealed ~14,000 disconnect/reconnect events in a single day against the staging relay, confirming a server-side component as well (tracked separately in block-coder-tf-stacks#2181). But the beachball correlation with disconnects pointed to these synchronous crypto commands as an independent client-side contributor — a blocked main thread can't process inbound pong frames, so the relay's 3-missed-pongs (90s) timeout fires even when the network is healthy.

  • Convert all four commands from pub fn to pub async fn with tauri::async_runtime::spawn_blocking so crypto work runs on the tokio blocking thread pool instead of the webview main thread
  • Clone Keys from the std::sync::Mutex before entering spawn_blocking since State<'_> cannot cross the boundary
  • No changes to lib.rstauri::generate_handler! handles both sync and async commands identically

Related: block-coder-tf-stacks#2181 (staging relay observability, PDB, replica scaling)

sign_event, create_auth_event, nip44_encrypt_to_self, and
nip44_decrypt_from_self were synchronous Tauri commands running
Schnorr signing and NIP-44 encryption on the webview main thread.
Under burst traffic (reconnect replay, presence updates) this blocked
the event loop long enough to trigger the macOS beachball and prevent
WebSocket pong responses from being processed, contributing to
relay disconnect cycles.
@wpfleger96
wpfleger96 marked this pull request as ready for review June 23, 2026 21:58
@wpfleger96
wpfleger96 merged commit e35e84b into main Jun 23, 2026
22 of 23 checks passed
@wpfleger96
wpfleger96 deleted the worktree-wpfleger-async-tauri-crypto branch June 23, 2026 22:02
wpfleger96 added a commit that referenced this pull request Jun 24, 2026
The nip44_encrypt_to_peer / nip44_decrypt_from_peer commands ran the
CPU-bound NIP-44 encrypt/decrypt synchronously while holding the keys
lock on the main thread. #1222 already moved the equivalent *_self
commands off-thread via async + spawn_blocking; these DM commands
predate that change and were left on the asymmetric path. Mirror the
*_self template so the hottest DM paths (encrypt-on-send,
decrypt-on-render) no longer block the main thread. Callers are
unchanged — both already await through invokeTauri.

Co-authored-by: Will Pfleger <pfleger.will@gmail.com>
Signed-off-by: Will Pfleger <pfleger.will@gmail.com>
wpfleger96 added a commit that referenced this pull request Jun 25, 2026
The nip44_encrypt_to_peer / nip44_decrypt_from_peer commands ran the
CPU-bound NIP-44 encrypt/decrypt synchronously while holding the keys
lock on the main thread. #1222 already moved the equivalent *_self
commands off-thread via async + spawn_blocking; these DM commands
predate that change and were left on the asymmetric path. Mirror the
*_self template so the hottest DM paths (encrypt-on-send,
decrypt-on-render) no longer block the main thread. Callers are
unchanged — both already await through invokeTauri.

Co-authored-by: Will Pfleger <pfleger.will@gmail.com>
Signed-off-by: Will Pfleger <pfleger.will@gmail.com>
wpfleger96 added a commit that referenced this pull request Jun 25, 2026
The nip44_encrypt_to_peer / nip44_decrypt_from_peer commands ran the
CPU-bound NIP-44 encrypt/decrypt synchronously while holding the keys
lock on the main thread. #1222 already moved the equivalent *_self
commands off-thread via async + spawn_blocking; these DM commands
predate that change and were left on the asymmetric path. Mirror the
*_self template so the hottest DM paths (encrypt-on-send,
decrypt-on-render) no longer block the main thread. Callers are
unchanged — both already await through invokeTauri.

Co-authored-by: Will Pfleger <pfleger.will@gmail.com>
Signed-off-by: Will Pfleger <pfleger.will@gmail.com>
wpfleger96 added a commit that referenced this pull request Jul 29, 2026
…in thread (#3415)

Opening the agent observer feed could beachball the app. In Tauri 2, a
sync (`pub fn`) command body runs on the **main thread** — only `async
fn` commands run on the runtime pool. Five commands on the observer-feed
open path were sync, so panel open ran SQLite I/O and secp256k1 work on
the macOS main thread:

| Command | Main-thread work |
|---|---|
| `decrypt_observer_event` | Schnorr ID + signature verify, then NIP-44
decrypt — once per frame |
| `read_archived_observer_events_for_channel` | Opens the archive DB,
runs the channel-index JOIN, returns up to 200 raw JSON blobs per page |
| `read_unindexed_observer_rows` | Opens the DB, returns **all**
not-yet-indexed kind-24200 rows in one shot |
| `index_observer_channel_id` | Opens the DB, loops N upserts |
| `delete_save_subscription` | Opens the DB, one delete |

Eager hydration loads up to 10 pages × 200 frames on panel open, so
that's up to 10 main-thread DB reads plus up to 2,000 sequential
verify+decrypt calls before any scrolling. The one-shot backfill makes
it worse on the first open after history accumulates: one read of every
unindexed row, a decrypt per row, then a batch upsert — all on the main
thread, and all proportional to archive size.

The four archive commands now route their DB work through the existing
`run_archive_db_task` helper (`spawn_blocking` + `open_db`), matching
`list_save_subscriptions`, `read_archived_events`, and `archive_events`
directly around them. `decrypt_observer_event` becomes `async fn` +
`tauri::async_runtime::spawn_blocking`, with `state.signing_keys()`
extracted before the spawn since `State` is not `Send` — the same
pattern `sign_event` uses from #1222.

No frontend changes: `invoke` is already promise-based, so the TS
wrappers in `tauriArchive.ts` and `tauriObserver.ts` are unchanged.

This removes the freeze, not the work. Eager hydration still takes the
same wall time — the feed shows a loading state instead of blocking the
UI. Batching the per-frame decrypt IPC (2,000 round-trips into one
command) would cut the latency itself; that's deliberately out of scope
here.

Signed-off-by: Will Pfleger <pfleger.will@gmail.com>
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.

1 participant