Skip to content

Cap wasm worker pool growth#3

Merged
rlch merged 1 commit into
rlch:masterfrom
raqeebshafeeque:tutero/hard-max-wasm-worker-pool
Jun 26, 2026
Merged

Cap wasm worker pool growth#3
rlch merged 1 commit into
rlch:masterfrom
raqeebshafeeque:tutero/hard-max-wasm-worker-pool

Conversation

@raqeebshafeeque

Copy link
Copy Markdown

Summary

Cap wasm WorkerPool growth and add an explicit lazy hard-cap constructor for web worker lanes.

Problem

WorkerPool::new(Some(1), ...) prewarmed one worker, but did not enforce a maximum of one live worker. Under bursty FRB traffic, the pool could spawn additional workers and retain them.

For embedders using FRB thread lanes as logical single-worker lanes, this meant a “single worker” lane could accidentally become a growing worker pool, increasing browser worker count and memory usage.

Changes

  • Add WorkerPool::new_with_max(initial, max_workers, ...).
  • Add WorkerPool::new_raw_with_max(...).
  • Track live workers separately from idle workers.
  • Queue work FIFO once max_workers is reached.
  • Reuse workers after completion.
  • Revoke generated blob worker URLs after worker creation.
  • Preserve existing WorkerPool::new(...) API while making its worker count act as a hard cap.
  • On wasm, run Thread::Main normal/async calls inline instead of dispatching them through a worker pool.

Why

This allows embedders to create lanes like:

WorkerPool::new_with_max(0, 1, ...)

Meaning:

  • lazy spawn / no startup prewarm
  • hard maximum of one worker
  • FIFO queue under contention

This is useful for strict single-threaded wasm lanes such as CRDT/Loro document access.

Validation

  • cargo fmt -p flutter_rust_bridge
  • cargo check -p flutter_rust_bridge --target wasm32-unknown-unknown --features serde_json,uuid -q

Add a hard maximum to the wasm-bindgen WorkerPool and queue additional work FIFO once all capped workers are busy. This keeps WorkerPool::new behavior compatible while exposing new_with_max for embedders that need lazy prewarm with a strict cap.\n\nOn wasm, execute Thread::Main normal/async calls inline instead of sending them through a worker pool, avoiding unnecessary worker creation for main-thread-safe work.
@rlch
rlch merged commit b9f795d into rlch:master Jun 26, 2026
12 of 115 checks passed
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.

2 participants