emscripten-core / emscripten-core/emscripten

Exported functions break inside web worker under specific circumstances

Open
#17,663 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

main.cpp

```
#include
using namespace emscripten;

int cube(int number) {
return number * number * number;
}

EMSCRIPTEN_BINDINGS(module) {
function("cube", &cube);
}
```
scripts.js
```
const worker = new Worker("worker.js");

worker.postMessage(20);
worker.onmessage = e => {
console.log(e.data);
worker.postMessage(200); // this line is very important
}
```
worker.js
```
onmessage = e => {
importScripts("./a.out.js") // this is the compiled javascript file

Module["onRuntimeInitialized"] = () => {
postMessage(Module.cube(e.data));
}
}
```
index.html
```


```

Command: `em++ main.cpp -lembind`

Upon running the code, the following output appears on the console
```
8000 [scripts.js:5:10](http://localhost:8080/scripts.js)
too much recursion [a.out.js:1403:9](http://localhost:8080/a.out.js)
```

However, if I change worker.js to
```
onmessage = e => {
postMessage(e.data * e.data * e.data);
}
```
The console output is:
```
​8000000 132079 [scripts.js:5:10](http://localhost:8080/scripts.js)
```
Which is correct, and the program is constantly printing out 8000000.

Version of emscripten/emsdk:
```
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.15 (8b4443a87f5eab5dbb9adb690f9ebed0a9da4bd9)
clang version 15.0.0 (https://github.com/llvm/llvm-project 27abff670bc7ad9702c4f9fc8b82aae6b530bf0f)
Target: wasm32-unknown-emscripten
Thread model: posix
```

So using web assembly exported functions inside a web worker that is recursively sent messages doesn't work and causes an error, while using a javascript function does not. Anyone know the cause for this? Thanks a lot.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.