Skip to content
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

Tokio channels can cause panic in wasm context, when using multiple threads #7205

Open
binier opened this issue Mar 8, 2025 · 2 comments · May be fixed by #7207
Open

Tokio channels can cause panic in wasm context, when using multiple threads #7205

binier opened this issue Mar 8, 2025 · 2 comments · May be fixed by #7207
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-sync Module: tokio/sync

Comments

@binier
Copy link

binier commented Mar 8, 2025

Version
v1.37.0

Target
wasm32-unknown-unknown

Description
In the browser context, in it's main thread, blocking calls cause panic, so it's really not an option to use std::sync::Mutex, or anything that can block the main thread.

I assumed Tokio channels would be fully async and wouldn't use any blocking primitives, but it looks like that's not the case. When I checked it's code, found multiple places blocking can occur, like:

waiters: Mutex<WaitList>,

or
rx_waker: CachePadded<AtomicWaker>,

etc..

Here is one of the panics I encountered while using tokio channels in the browser:

console.js:36 RuntimeError: Atomics.wait cannot be called in this context
at openmina_node_web.wasm.std::sys::sync::mutex::futex::Mutex::lock_contended::h26da324537dc575a (openmina_node_web_bg.wasm:0x11cd519)
at openmina_node_web.wasm.tokio::runtime::park::wake::h73a00f0ed933a774 (openmina_node_web_bg.wasm:0x11ab2fc)
at openmina_node_web.wasm.tokio::sync::task::atomic_waker::AtomicWaker::wake::h0953ea3f1740360a (openmina_node_web_bg.wasm:0x1219144)
at openmina_node_web.wasm.tokio::sync::mpsc::chan::Tx<T,S>::send::h0b8b79b860905134 (openmina_node_web_bg.wasm:0x109fcb4)

I'm not sure if fixing this is in the scope of this project, but we probably should add to the docs case when those certain calls might panic right?

@binier binier added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Mar 8, 2025
@Darksonn Darksonn added the M-sync Module: tokio/sync label Mar 10, 2025
@Darksonn
Copy link
Contributor

This really applies to anything in tokio::sync. Using these utilities is completely normal and they only block for a short amount of time. It's certainly not something we're going to fix.

@Darksonn Darksonn reopened this Mar 10, 2025
@binier
Copy link
Author

binier commented Mar 10, 2025

@Darksonn Yes makes sense, but it would be very useful to extend docs with a note of potential panic in WASM context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-sync Module: tokio/sync
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants