emscripten-core / emscripten-core/emscripten

[asyncify] : pthread_join() does not return

Open
#17,552 6 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

Hi,

It seems calling pthread_join() on a thread who ran an em_async_js function does not return to the calling thread.

```
#include
#include
#include
#include
#include

EM_ASYNC_JS(int, async_call, (), {
let p = new Promise((resolve) => {
setTimeout(() => {
console.log("debug timeout!");
resolve();
}, 5000);

});
await p;
return 42;
});

void *run(void *args) {
int ret = async_call();
assert(ret == 42);
printf("debug %d\n", ret);

return NULL;
}

int main() {
pthread_t id;
pthread_create(&id, NULL, run, NULL);
printf("joining thread!\n");
pthread_join(id, NULL);
printf("joined thread!\n");

return 0;
}

```

compiled with :
```
emcc main.c -o test.html -s EXIT_RUNTIME=1 -pthread -s PROXY_TO_PTHREAD=1 -s ASYNCIFY
```

will never print "joined thread!"

If no_exit_runtime is set, the pthread_join returns but does not block until the promise is fulfilled. I was wondering if it was a bug and if there was another way to wait for a promise in a joinable pthread.

**Version of emscripten/emsdk:**
```
$ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.11-git (4cf36b6df367326e4d54ab6004466efe7ed3f265)
clang version 15.0.0 (https://github.com/llvm/llvm-project.git e0892614b162f5f53aa44c88080d986649a50982)
Target: wasm32-unknown-emscripten
```

**Failing command line in full:**

**[Full link command and output with `-v` appended:](https://pastebin.com/QvaV7XiV)**

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.