GoogleChromeLabs / wasm-bindgen-rayon

An adapter for enabling Rayon-based concurrency on the Web with WebAssembly.
https://github.com/RReverser/wasm-bindgen-rayon
Apache License 2.0
404 stars 35 forks source link

RuntimeError: Atomics.wait cannot be called in this context #31

Closed ZhaoXiangXML closed 2 years ago

ZhaoXiangXML commented 2 years ago

I'm trying to use wasm-bindgen-rayon 1.0.3 in my project, I'm using Chrome 103.0.5060.114 on windows.

I've this code to init the wasm part:

import init, { initThreadPool } from "../physics/pkg";

async function main() {
    await init();
    await initThreadPool(navigator.hardwareConcurrency);
    await import("./index");
}

main();

However I got this issue:

bootstrap.ts:10 Uncaught (in promise) RuntimeError: Atomics.wait cannot be called in this context
    at core::core_arch::wasm32::atomic::memory_atomic_wait32::h5440a6c4a0a06846 (80b835a348a02018643f.wasm:0x3ff326)
    at std::sys::wasm::mutex::Mutex::lock::ha4a05c48513bbf2a (80b835a348a02018643f.wasm:0x2de528)
    at std::sys_common::mutex::MovableMutex::raw_lock::he81e94f0080a928c (80b835a348a02018643f.wasm:0x3fdc57)
    at std::sync::mutex::Mutex<T>::lock::he4f53ecd85d7d977 (80b835a348a02018643f.wasm:0x3ef0cb)
    at spmc::channel::Sender<T>::send::hd7b538f9d01fbf7a (80b835a348a02018643f.wasm:0x17b836)
    at wasm_bindgen_rayon::wbg_rayon_PoolBuilder::build::{{closure}}::hd621a7d1ef754496 (80b835a348a02018643f.wasm:0x29fa94)
    at <rayon_core::registry::CustomSpawn<F> as rayon_core::registry::ThreadSpawn>::spawn::h176a0862da85c4cb (80b835a348a02018643f.wasm:0x241cad)
    at rayon_core::registry::Registry::new::h90276e299016d06f (80b835a348a02018643f.wasm:0xdbf98)
    at rayon_core::registry::init_global_registry::{{closure}}::h848379b093be6bd0 (80b835a348a02018643f.wasm:0x2abb05)
    at rayon_core::registry::set_global_registry::{{closure}}::h73f6010cde18cd75 (80b835a348a02018643f.wasm:0x24aa0b)

Should I call initThreadPool from an webworker or something?

ZhaoXiangXML commented 2 years ago

I found this bug exits in the test project of wasm-bindgen-rayon

RReverser commented 2 years ago

Yes, this is described in the Caveats section.

ZhaoXiangXML commented 2 years ago

So I should call initThreadPool from a worker thread? Would you please kindly upgrade your test project? From what I can tell, https://github.com/GoogleChromeLabs/wasm-bindgen-rayon/blob/c3a12c4843b35d5c528fbed178aad1e9142c8072/test/index.js#L18 is calling it from main thread and It does not work on my machine.

ZhaoXiangXML commented 2 years ago

I've noticed https://github.com/GoogleChromeLabs/wasm-bindgen-rayon/issues/18#issuecomment-960949808 and I'll check out the demo instead of the test.

RReverser commented 2 years ago

So I should call initThreadPool from a worker thread? Would you please kindly upgrade your test project? From what I can tell,

https://github.com/GoogleChromeLabs/wasm-bindgen-rayon/blob/c3a12c4843b35d5c528fbed178aad1e9142c8072/test/index.js#L18

is calling it from main thread and It does not work on my machine.

Yeah for tests it didn't matter for some reason, but demo was intended to be an actual end-to-end demo of how you'd realistically use the library.