I've encountered an unexpected deadlock when using wasm_thread in a binary running directly in a dedicated web worker.
It can be reproduced reliably as demonstrated here.
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_dedicated_worker);
#[wasm_bindgen_test]
fn test_thread_join() {
let handle = thread::spawn(|| 1234);
assert_eq!(handle.join().unwrap(), 1234);
}
After some digging I believe the deadlock stems from here
|
if is_web_worker_thread() { |
|
WorkerMessage::SpawnThread(BuilderRequest { builder: self, context }).post(); |
|
} else { |
|
self.spawn_for_context(context); |
|
} |
is_web_worker_thread will evaluate to true, which will post a message to the parent page which has no handler for it.
spawn_for_context must be called at least once regardless of whether it is a worker or the browser main thread. This can possibly be addressed by adding a global and changing it to this:
if is_main_thread() {
self.spawn_for_context(context);
} else {
WorkerMessage::SpawnThread(BuilderRequest { builder: self, context }).post();
}
I've encountered an unexpected deadlock when using
wasm_threadin a binary running directly in a dedicated web worker.It can be reproduced reliably as demonstrated here.
After some digging I believe the deadlock stems from here
wasm_thread/src/wasm32/mod.rs
Lines 254 to 258 in 7ec4868
is_web_worker_threadwill evaluate totrue, which will post a message to the parent page which has no handler for it.spawn_for_contextmust be called at least once regardless of whether it is a worker or the browser main thread. This can possibly be addressed by adding a global and changing it to this: