emscripten-core / emscripten-core/emscripten
Threads with dynamic linking produce unexpected memory out of bounds error if recreating threads immediately after join
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
sorry for posting a new issue, since I posted earlier wasn't really helpful, (I'll link that to this one) I've been trying to figure out this problem for sometime now and come up with a more accurate version , and also a bare minimum example to reproduce this.
Please include the following in your bug report:
**Version of emscripten/emsdk:**
```emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.34 (57b21b8fdcbe3ebb523178b79465254668eab408)
clang version 17.0.0 (https://github.com/llvm/llvm-project a031f72187ce495b9faa4ccf99b1e901a3872f4b)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: D:\emsdk\upstream\bin
```

Without Hack , plain call to ```boot(NULL);``` Gif [play button's on the top right corner, you can also click the image]

I have included a bare-minimum example to reproduce this error.
[threadjoin(updated2).zip](https://github.com/emscripten-core/emscripten/files/11257732/threadjoin.updated2.zip)
you need to have python installed, run ```make```
server will be at localhost:8000/a.html
in the dev console, if you do ```Module.ccall('closeThread', null, [])``` , first time there's a unreachable error, second time there's an error in the screenshot, I did some experiment, it appears this only happen when dynamic linking is enabled, i'm not sure if this is always true.
(you might need to run it and refresh the page serveral times to reproduce this error )
note this line:
```
boot(NULL); // This line produce an error , since it boot too fast
// Hack: uncomment line below and comment the line above can eliminate the error, can i avoid this hack.
// emscripten_async_call(&boot, nullptr, 1000);
```
if i use boot(NULL); it recreates thread immediately after join, and it will result in an error, (This won't happen at all if in normal situation ) but in this case it does,
but, if you change that line to ```emscripten_async_call(&boot, nullptr, 1000);```, meaning wait for sometime after join, then everything seems working.
but I don't like this hack, is there a way to guarantee this would work? like 100% safe.
to reproduce this error , you need to call ```Module.ccall('closeThread', null, [])``` in the console ,maybe need to call several times.
the nameing wasn't helpful, even the function named ```closeThread```, but it should be named as ```closeThreadAndCreateNew```, sorry I was only testing, I should have elaborated it a little bit, anyway, I hope that you guys are not confused by that, since I called ``` boot(NULL); ``` in the ```closeThread```
with Hack , [Gif]

With Hack.

how to avoid this hack?
Contributor guide
Assessment
This issue has not been assessed yet.