denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
94.51k stars 5.25k forks source link

hang in WebAssembly.instantiateStreaming #18547

Open ACGNnsj opened 1 year ago

ACGNnsj commented 1 year ago

I built a ESM module by wasm-pack and imported it into a js file. I can call the function from the module properly using 'await', while something went wrong when 'Promise.then' applied.

import init, {add} from "./rust-lib-esm/pkg/rust_lib_esm.js";

/*init().then(wasm => {
  window.console.log(wasm)
  window.console.log(wasm.add(423, 4322))
})*/
await init()
window.console.log(add(423, 4322))

I found it stuck here.

return await WebAssembly.instantiateStreaming(module, imports);

Both methods work well in chrome.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
<script type="importmap">
{
  "imports": {
    "rust-lib-esm": "./rust-lib-esm/pkg/rust_lib_esm.js"
  }
}
</script>
<script type="module">
  import * as e from "rust-lib-esm";

  e.default().then(wasm => {
    window.console.log(wasm)
    console.log(e.add(2351, 53422));
  });
</script>
<script type="module">
  import * as e from "rust-lib-esm";

  await e.default()
  window.console.log(e.add(423, 4325))
</script>
</html>
aapoalas commented 1 year ago

This sounds like it might have something to do with the internal implementation of WebAssembly streaming initialization.

andreubotella commented 1 year ago

I can't reproduce with wasm-pack 0.10.3 (with --target web), it works with both await and Promise.then. But I can't seem to make wasm-pack 0.11.0 (which has deno as a target) work on my system without running into segfaults or into rust-wasm/wasm-pack#1245.

The build I did with wasm-pack 0.10.3 did use WebAssembly.instantiateStreaming, so it's not a simple case of that method not working. Having the wasm-pack generated code in question, or some more detailed reproduction instructions (which version of wasm-pack? which target?) would be helpful.

Edit: Apparently it makes a difference whether you install wasm-pack with cargo install or with the command at https://rustwasm.github.io/wasm-pack/installer/, at least on a linux system, because I'm not getting such errors for wasm-pack 0.11 when I use that command. I still can't reproduce though.

ACGNnsj commented 1 year ago

I was using wasm-pack 0.11.0 installed through cargo on Windows, and the target argument was web.

hansSchall commented 3 weeks ago

I can confirm that there are (extremely strange) issues with WASM streaming and the event loop where Deno behaves different than Firefox, Edge, Node and Bun (all tested with exactly/nearly the same code).

hansSchall commented 3 weeks ago

Minimal reproduction example: https://github.com/hansSchall/deno-wasm-streaming-mre

devsnek commented 2 weeks ago

@hansSchall i'm unable to reproduce this.

hansSchall commented 2 weeks ago

I am using Deno 1.46.3 on Windows 11, maybe its a windows-only thing