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

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

Open
foreinbug opened this issue Jun 6, 2024 · 5 comments

Comments

@foreinbug
Copy link

environment:

  • rustc 1.80.0-nightly (72fdf913c 2024-06-05)
  • cargo 1.80.0-nightly (34a6a87d8 2024-06-04)
  • wasm-pack 0.12.1
  • nightly-x86_64-pc-windows-msvc

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
Copy link
Owner

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

@foreinbug
Copy link
Author

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

@justinlovinger
Copy link

justinlovinger commented Oct 9, 2024

I have the same issue, but only on production. The app works over localhost, with the same headers. The production site is secured using an SSL certificate signed by Let's Encrypt. window.crossOriginIsolated returns true, including on production.

@justinlovinger
Copy link

justinlovinger commented Oct 10, 2024

With help from daxpedda on Discord, I discovered the issue is mostly likely related to compiling without the right flags, not cross-origin isolation. In my case, an issue with my build-script for production may be the culprit. As they pointed out, the memory that is failing to clone is ArrayBuffer, not SharedArrayBuffer.

@AThilenius
Copy link

AThilenius commented Dec 15, 2024

(Updated)

I'm running into the same issue with wasm-pack, though the wasm-bindgen based build works. Here is a repro in Docker

FROM rust
RUN rustup toolchain install nightly
RUN rustup component add rust-src --toolchain nightly
RUN cargo install wasm-bindgen-cli
RUN cargo install sfz
RUN curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

RUN git clone https://github.com/chemicstry/wasm_thread.git

WORKDIR /wasm_thread
RUN bash build_wasm.sh

WORKDIR /wasm_thread/examples-wasm-pack
RUN wasm-pack build --dev --out-dir ./module/target --target web --features wasm_thread/es_modules
RUN wasm-pack build --dev --out-dir ./no-module/target --target no-modules

WORKDIR /wasm_thread
CMD sfz . -b 0.0.0.0 --coi

Run is with

docker build . --tag wasm_thread_test
docker run --rm -p 5000:5000 wasm_thread_test

http://localhost:5000/examples/simple.html works correctly (though with warnings about deprecated initialization parameters) but both of the wasm_pack examples don't work. Same error as OP, and COEP and COOP headers are correctly set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants