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 · 4 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.

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

3 participants