-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Panic on multithreaded browser Wasm #89
Comments
I don't think this is correct? Busy looping in the browser thread locks up the entire browser and leads to unpredictable behavior. Web frameworks go to great lengths to avoid busy looping for this reason. I'd rather pursue smol-rs/parking#20, but that's more complex. |
smol-rs/parking#20 actually already works the way it describes. Rust's std sync primitives use Wasm's atomic.wait when compiled with the
While busy-looping is bad, and is generally to be avoided, it is tolerable when the busy-loop is expected to run for a very short period of time. The alternative is offering an API to upstream users that can crash unpredictably and requires significant rearchitecting to avoid the crash. For example the I'm unfamiliar with the Now that I'm looking into it more I see there are a bunch of |
Coming back to this. I no longer think that it will be necessary (at least for now) to involve any sort of 'busy loop' hack. Instead Rust / Wasm programs can architect themselves to run This will require going through all of the I can work on pull requests for those changes. |
Apologies, I thought I typed up a response here, but I guess I forgot to hit "comment"? The main issue is that our current usage of A better solution would be to use a different way of initializing the I will accept a PR for this. |
Fixes #89. Uses @notgull's suggestion of using a `AtomicPtr` with a racy initialization instead of a `OnceCell`. For the addition of more `unsafe`, I added the `clippy::undocumented_unsafe_blocks` lint at a warn, and fixed a few of the remaining open clippy issues (i.e. `Waker::clone_from` already handling the case where they're equal). Removing `async_lock` as a dependency shouldn't be a SemVer breaking change.
Is OnceLock unavailable in all situations? Even when building with build-std etc? When you have a setup such as with Not sure if that would actually be acceptable in any shape or form, it likely would lead to a number of special cases, since the kind of setup that |
Instant still doesn't work, but I think |
This segment of code has assumptions that are incorrect for projects making use of multithreaded Wasm:
async-executor/src/lib.rs
Lines 276 to 288 in 6c70369
Multithreaded Wasm is possible with Rust, but the chief limitation is that the main thread panics if it tries to block/wait. However it is allowed to busy-loop as a form of waiting. Crates like wasm_sync reimplement synchronization primitives and use busy-looping instead of blocking on the main thread.
So here's the puzzle: projects like Bevy depend on
async-executor
, and right now I'm trying to make Bevy multithreaded. There needs to be a workaround to prevent outright crashing if contention is encountered on the main thread due to the above snippet of code.It could be modified to work like this on Wasm (preferably only on the main thread):
Alternatively the fix could go into the
async-lock
project.Would this project be willing to take on a fix like that to unblock upstream users of multithreaded Wasm?
Perhaps eventually this workaround could land in the standard library itself, but there are challenges with that as well. In an even better world browsers would allow waiting on the main thread, but that will not be considered any time soon.
The text was updated successfully, but these errors were encountered: