emscripten-core/emscripten

-sNO_EXIT_RUNTIME=1 is ignored if used with -pthread

Opened this issue · 1 comments

Let's consider the code below:

#include <stdio.h>
#include <emscripten.h>

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:

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: #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

For now our workaround is adding 'noExitRuntime' to INCOMING_MODULE_JS_API