emscripten-core / emscripten-core/emscripten

-sNO_EXIT_RUNTIME=1 is ignored if used with -pthread

Open
#25,190 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

Let's consider the code below:
```cpp
#include
#include

void main_loop() {
printf("Looping!\n");
}

int main() {
printf("Hi!\n");

emscripten_set_main_loop(main_loop, 0, 0);
emscripten_pause_main_loop();

// Assume that we want to call resume main loop from JS at some later point in time...

return 0;
}
```

We print a message on runtime exit:
```javascript
Module["onExit"] = function() {
console.log("EXITED :(");
}
```

If we compile this code with `emcc main.cpp -o no_pthread.html -sSTRICT=1 -sNO_EXIT_RUNTIME=1 --pre-js=pre.js`, the runtime does not exit, and it is possible to resume the main loop from Javascript.

On the other hand, if we compile it with `emcc main.cpp -o no_pthread.html -sSTRICT=1 -sNO_EXIT_RUNTIME=1 --pre-js=pre.js -pthread`, then the runtime is shut down and it is no longer possible to resume the main loop.

The culprit is this PR: https://github.com/emscripten-core/emscripten/pull/22927

In case of PTHREADS && NO_EXIT_RUNTIME, before the PR:
`keepRuntimeAlive => !ENVIRONMENT_IS_PTHREAD`
After the PR:
`keepRuntimeAlive => runtimeKeepaliveCounter > 0`

Our example demonstrates the problem with the latter (it does not consider the main thread). Possible fix would be to set `keepRuntimeAlive` to `!ENVIRONMENT_IS_PTHREAD || runtimeKeepaliveCounter > 0`.

Here is the example code:
[ems_exit_fail.zip](https://github.com/user-attachments/files/22172138/ems_exit_fail.zip)

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.