wasm: Avoid blocking the browser main thread#174
Open
Aleksbgbg wants to merge 1 commit into
Open
Conversation
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.
ce136ed to
1c94fab
Compare
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.
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_mutexcrate. 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_mutexfor 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_mutexis only used on Wasm platforms.Fixes #173.