emscripten-core / emscripten-core/emscripten

Runtime does not exit after sleep with ASYNCIFY + pthread + EXIT_RUNTIME

Open
#16,940 0 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

When calling Asyncify.handleSleep/handleAsync or using EM_ASYNC_JS, a multithreaded program will fail to shutdown after main exits, despite EXIT_RUNTIME being set.

In the same scenario, using emscripten_sleep/emscripten_call_async/emscripten_set_timeout all shut down fine. It looks like there is special handling in safeSetTimeout that handles runtime lifetime.

**Version of emscripten/emsdk:**
> emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.10 (c3fe57af0504fe24fc4ba697feb8c204f3c80022)
clang version 15.0.0 (https://github.com/llvm/llvm-project 8bc29d14273b05b05d5a56e34c07948dc2c770d3)
Target: wasm32-unknown-emscripten
Thread model: posix

**Minimal repro source:**
```
#include
#include

EM_JS(void, pauseBriefly, (), {
Asyncify.handleSleep(wake => {
setTimeout(() => {
#ifdef WORKAROUND
callUserCallback(wake);
#else
wake();
#endif
}, 0);
});
});

int main() {
pauseBriefly();
std::cout << "Exiting" << std::endl;
return 0;
}
```

**Failing test:**
```
> emcc test.cpp -s ASYNCIFY=1 -s ASYNCIFY_IMPORTS=pauseBriefly -pthread -s PTHREAD_POOL_SIZE=1 -s EXIT_RUNTIME=1
> node --experimental-wasm-threads .\a.out.js
Exiting
```
Runtime does not exit, node keeps running.

**With workaround:**
```
> emcc test.cpp -s ASYNCIFY=1 -s ASYNCIFY_IMPORTS=pauseBriefly -pthread -s PTHREAD_POOL_SIZE=1 -s EXIT_RUNTIME=1 -D WORKAROUND
> node --experimental-wasm-threads .\a.out.js
Exiting
>
```
Runtime exits as expected, node exits.

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.