fix(desktop): move crypto commands off the main thread - #1222
Merged
Conversation
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
marked this pull request as ready for review
June 23, 2026 21:58
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Four Tauri commands in
identity.rs—sign_event,create_auth_event,nip44_encrypt_to_self, andnip44_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.
pub fntopub async fnwithtauri::async_runtime::spawn_blockingso crypto work runs on the tokio blocking thread pool instead of the webview main threadKeysfrom thestd::sync::Mutexbefore enteringspawn_blockingsinceState<'_>cannot cross the boundarylib.rs—tauri::generate_handler!handles both sync and async commands identicallyRelated: block-coder-tf-stacks#2181 (staging relay observability, PDB, replica scaling)