emscripten-core / emscripten-core/emscripten

Threads with dynamic linking produce unexpected memory out of bounds error if recreating threads immediately after join

Open
#19,199 9 comments 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

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
```

![Screenshot 2023-04-18 101437](https://user-images.githubusercontent.com/128656803/232652431-e4ae1e73-1203-46c2-9f06-8292d67c93e7.jpg)

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

![Animation3](https://user-images.githubusercontent.com/128656803/232668647-09e3a6e4-af62-4af0-aef8-99a2594948aa.gif)

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]
![Animation3](https://user-images.githubusercontent.com/128656803/232668983-c9bb22f6-0d6d-4521-a848-57ca484779bc.gif)

With Hack.
![Animation4](https://user-images.githubusercontent.com/128656803/232669000-417cbdb5-2b78-4e10-9fe1-0a9e94157b8c.gif)

how to avoid this hack?

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.