chemicstry / wasm_thread

A rust `std::thread` replacement for wasm32 target
Apache License 2.0
123 stars 16 forks source link

An error occurred: Failed to execute 'postMessage' on 'Worker': #<Memory> could not be cloned. #24

Open foreinbug opened 3 months ago

foreinbug commented 3 months ago

environment:

No cross domain, use self-signed HTTPS;

cargo.toml

[package]
name = "simple"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
wasm_thread = { version = "0.3.0", default-features = false }
log = "0.4"
wasm-bindgen = "0.2"
console_log = { version = "1.0", features = ["color"] }
console_error_panic_hook = "0.1"

src/lib.rs

use std::time::Duration;

use wasm_bindgen::prelude::*;
use wasm_thread as thread;

#[wasm_bindgen(start)]
fn main() {
    console_log::init().unwrap();
    console_error_panic_hook::set_once();

    for _ in 0..2 {
        thread::spawn(|| {
            for i in 1..3 {
                log::info!(
                    "hi number {} from the spawned thread {:?}!",
                    i,
                    thread::current().id()
                );
                thread::sleep(Duration::from_millis(1));
            }
        });
    }

    for i in 1..3 {
        log::info!(
            "hi number {} from the main thread {:?}!",
            i,
            thread::current().id()
        );
    }
}

build.ps1

wasm-pack build --dev --out-dir ./module/target --target web --features wasm_thread/es_modules
simple.js:338 
 panicked at C:\Users\ASUS\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wasm_thread-0.3.0\src\wasm32\mod.rs:349:10:
called `Result::unwrap()` on an `Err` value: JsValue(DataCloneError: Failed to execute 'postMessage' on 'Worker': #<Memory> could not be cloned.
Error: Failed to execute 'postMessage' on 'Worker': #<Memory> could not be cloned.
chemicstry commented 3 months ago

Do you have cross origin isolation header set? It is required for web workers, see #23

foreinbug commented 3 months ago

Do you have cross origin isolation header set? It is required for web workers, see #23

However, there is no cross-domain problem, and the loaded files are all in the same domain

  ctx.set("Cross-Origin-Embedder-Policy", "require-corp");
  ctx.set("Cross-Origin-Opener-Policy", "same-origin");

I tried to add these response headers, but the error was the same