Skip to content

wasm: Avoid blocking the browser main thread#174

Open
Aleksbgbg wants to merge 1 commit into
zesterer:masterfrom
Aleksbgbg:dev/wasm-safe-mutex
Open

wasm: Avoid blocking the browser main thread#174
Aleksbgbg wants to merge 1 commit into
zesterer:masterfrom
Aleksbgbg:dev/wasm-safe-mutex

Conversation

@Aleksbgbg
Copy link
Copy Markdown

@Aleksbgbg Aleksbgbg commented Feb 8, 2026

When using flume with Wasm in the browser, and without the spin feature, blocking when attempting to lock a mutex which is already locked on the main thread will panic and thereby crash the application.

To resolve the issue, use a spinlock on the browser main thread, through the wasm_safe_mutex crate. Web worker threads will continue to sleep when locking a mutex as usual.


Note: this isn't the most beautiful solution, as there are now 3 mutex implementations in the crate - unfortunately the ecosystem is in discord, with the standard library Mutex happily blocking the main thread on Wasm when this is a known problem, and browsers refusing to allow blocking on the main thread.

Theoretically we could only use wasm_safe_mutex for everything, as this provides the ability to spin or block and has async support, however (without any inspection whatsoever) I imagine that long-term the standard library's mutex is likely to be more efficient, especially as it would continuously receive updates and improvements over time from the entire community.

As such I have guarded this change behind flags so that wasm_safe_mutex is only used on Wasm platforms.

Fixes #173.

When using flume with Wasm in the browser, and without the spin feature,
blocking when attempting to lock a mutex which is already locked on the
main thread will panic and thereby crash the application.

To resolve the issue, use a spinlock on the browser main thread, through
the `wasm_safe_mutex` crate. Web worker threads will continue to sleep
when locking a mutex as usual.
@Aleksbgbg Aleksbgbg force-pushed the dev/wasm-safe-mutex branch from ce136ed to 1c94fab Compare February 8, 2026 12:03
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.

WASM: Options to send_spin / recv_spin?

1 participant