emscripten-core / emscripten-core/emscripten

`-s LINKABLE=1 -s ASYNCIFY=1` causes main loop to stop working

Open
#23,389 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

**Version of emscripten/emsdk:**
```
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.74 (1092ec30a3fb1d46b1782ff1b4db5094d3d06ae5)
clang version 20.0.0git (https:/github.com/llvm/llvm-project 322eb1a92e6d4266184060346616fa0dbe39e731)
Target: wasm32-unknown-emscripten
Thread model: posix
```

Take the sample code in `emtest.c`:
```c
#include
#include

#ifdef __EMSCRIPTEN__
#include
#endif

void mainloop(void)
{
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
printf("%s", asctime(timeinfo));
}

int main()
{
printf("Hello from main!\n");
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(mainloop, 0, 0);
#else
while(1)
mainloop();
#endif
printf("Bye from main!\n");
return 0;
}
```
Any of the below commands will produce a working program; i.e. prints the current time forever.
```
emcc emtest.c -o emtest.html -s MAIN_MODULE=1
emcc emtest.c -o emtest.html -s ASYNCIFY=1
emcc emtest.c -o emtest.html -s ASYNCIFY=1 -s MAIN_MODULE=2
```
However with these options set, `main` is called, but emscripten encounters an exception before `mainloop` is called.
```
emcc emtest.c -o emtest.html -s ASYNCIFY=1 -s MAIN_MODULE=1
emcc emtest.c -o emtest.html -s ASYNCIFY=1 -s LINKABLE=1
```

The exception is below:
```
Uncaught Internal Error! Attempted to invoke wasm function pointer with signature "v", but no such functions have gotten exported!
quit_ @ emtest.js:50
handleException @ emtest.js:1168
callUserCallback @ emtest.js:1219
runIter @ emtest.js:1456
MainLoop_runner @ emtest.js:1558
requestAnimationFrame
requestAnimationFrame @ emtest.js:1478
MainLoop_scheduler_rAF @ emtest.js:1362
setMainLoop @ emtest.js:1574
_emscripten_set_main_loop @ emtest.js:1583
imports. @ emtest.js:4395
$__original_main @ emtest.wasm:0x3e3f
$main @ emtest.wasm:0x3f9e
ret. @ emtest.js:4427
(anonymous) @ emtest.js:593
callMain @ emtest.js:5151
doRun @ emtest.js:5201
(anonymous) @ emtest.js:5210
setTimeout
run @ emtest.js:5208
runCaller @ emtest.js:5136
removeRunDependency @ emtest.js:526
receiveInstance @ emtest.js:714
receiveInstantiationResult @ emtest.js:732
createWasm @ emtest.js:755
await in createWasm
(anonymous) @ emtest.js:4797
```
I built with `emcc emtest.c -o emtest.html -s MAIN_MODULE=1 -s ASYNCIFY=1 -s ASYNCIFY_DEBUG=1 -s DYLINK_DEBUG=1 -s ASSERTIONS=2` and attached the full console output to this issue.

[localhost-1736814751439.log](https://github.com/user-attachments/files/18403601/localhost-1736814751439.log)

Also note that compiling with at least `-O1` *and without* at least `-s ASSERTIONS=1`, the error becomes silent.

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.