Skip to content

Deadlock in dedicated worker #32

@sinui0

Description

@sinui0

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();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions