Skip to content

Reuse one web worker for every sync instead of spawning per call - #6

Open
sqhell wants to merge 3 commits into
red-dev-inc:mainfrom
sqhell:fix/reuse-sync-worker
Open

sqhell wants to merge 3 commits into
red-dev-inc:mainfrom
sqhell:fix/reuse-sync-worker

Conversation

@sqhell

@sqhell sqhell commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

What

  • WebWallet::sync runs every sync on a single long-lived web worker, created on first use and fed through a channel; requests run one at a time.
  • webzjs-wallet declares tokio's sync feature explicitly (it uses tokio::sync::{mpsc, oneshot} and only compiled because another dependency enabled it).

Why

Leaving the wallet open on Account Summary crashed after roughly 75 minutes with Thread count overflowed the configured max count. Thread index = 128, max threads = 128 followed by RuntimeError: unreachable, after which the wallet hung. Each sync spawned a fresh worker; the tracing registry allows 128 thread slots on wasm32 and a slot is only returned from a thread-local destructor that never runs for these workers, so sync number 129 panicked. Idle workers also kept their stacks, so memory grew per sync.

Stack

Position 2 of 15 in the WebZjs release order, to be merged in this order into main. Independent of the feature PRs but rebased on #20 (chore/rust-checks) so its checks are green; the commits of this PR alone are here: sqhell/WebZjs@chore/rust-checks...fix/reuse-sync-worker

  1. Make rustfmt and clippy pass on main #20 chore/rust-checks
  2. Reuse one web worker for every sync instead of spawning per call #6 fix/reuse-sync-worker (this PR)
  3. Accept Trailers-Only gRPC responses from lightwalletd #7 fix/grpc-web-trailers-only
  4. Keep the Full Resync form usable during background syncs #8 fix/resync-dialog
  5. Send again from the transaction history, with a caution for new recipients #9 feat/send-again
  6. Surface PCZT parse and signer errors instead of trapping; readable transfer errors #10 fix/pczt-parse-error
  7. Sign the change-paired Orchard spends NU6.3 bundles require #11 fix/sign-change-paired-spends
  8. Stop a full resync from an old birthday exhausting the wasm heap #12 fix/historic-sync-oom
  9. Point to Full Resync from the end of the transaction history #13 feat/history-resync-hint
  10. Make wallet persistence refuse stale and silent overwrites #14 fix/wallet-store-epochs
  11. Spend and count the Ironwood (NU6.3) pool #15 feat/ironwood-pool
  12. Classify history by what the wallet spent, not only by what it received #16 fix/history-from-spends
  13. Wallet stats card on the account summary #17 feat/wallet-stats
  14. Bounded sync steps, instance ids, close and worker poisoning #18 feat/resumable-sync
  15. Resumable, checkpointed full resync driven by sync steps #19 feat/resync-resume

Rust pins reference sqhell/zcash_client_memory and sqhell/librustzcash by revision so CI builds exactly what was tested; once the zcash_client_memory stack (red-dev-inc/zcash_client_memory #16 to #21) merges, the memory pin goes back to red-dev-inc/zcash_client_memory feat/snap-nu63. The integration branch test/all-fixes on the fork merges all fourteen and is the build that was tested end to end.

Tests

just build

Rust-Checks has failed on main since the NU6.3 toolchain bump: rustfmt
differs in nine files, and clippy on nightly-2025-06-15 rejects ten
uninlined format arguments in webzjs-keys and one map iteration in
webzjs-requests under -D warnings. Apply rustfmt and clippy's own
suggestions; no behaviour changes.
Leaving the wallet open on Account Summary crashed after roughly 75
minutes with:

    panicked at sharded-slab-0.1.7/src/shard.rs:295:9:
    Thread count overflowed the configured max count.
    Thread index = 128, max threads = 128.

followed by `RuntimeError: unreachable`, after which the wallet hung.

`WebWallet::sync` spawned a fresh web worker for every call, and the
periodic rescan calls it every 35 seconds. The tracing registry stores
spans in a sharded slab that allows 128 thread slots on wasm32; a thread
takes a slot the first time it creates a span (block scanning does) and
gives it back only from its thread-local destructor. Those destructors
never run here: wasm_thread is built with `keep_worker_alive`, so
finished workers are not closed at all, and closing a worker skips Rust
thread-local drops anyway. So every sync consumed one slot for good, and
sync number 129 panicked. The idle workers also kept their stacks and
thread-local storage, so memory grew per sync as well.

Run all syncs on a single long-lived worker, created on first use and
fed through a channel. Requests run one at a time, which the JS caller
already enforces with its sync-in-progress guard.

A trap inside the worker (a Rust panic compiled to wasm) is not recovered
from: the trapped call never resolves and later syncs wait behind it until
the page is reloaded. That was already the case with a worker per call, which
held the wallet lock forever after a trap.
The shared sync worker uses tokio::sync::{mpsc, oneshot}, which only compiled
because another dependency happened to enable the feature.

This branch has not been deployed

No deployments
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