emscripten-core / emscripten-core/emscripten

Fail to emscripten_sleep(0) after emscripten_webgl_commit_frame() with -sOFFSCREENCANVAS_SUPPORT -sASYNCIFY -sASSERTIONS

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

Please include the following in your bug report:

**Version of emscripten/emsdk:**
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

I'm trying to fix the high CPU load in `-sOFFSCREEN_FRAMEBUFFER` mode with render pthread, since my render thread use a while loop without separate frame render iteration callback function, i want to first have a try with `emscripten_sleep(0) after emscripten_webgl_commit_frame()` solution, but failed:

```
Uncaught RuntimeError: memory access out of bounds
at _emscripten_thread_sync_code (dynlink.c:239)
at _emscripten_yield (emscripten_yield.c:45)
at emscripten_futex_wait (emscripten_futex_wait.c:116)
at __timedwait_cp (__timedwait.c:93)
at __timedwait (__timedwait.c:116)
at __pthread_mutex_timedlock (pthread_mutex_timedlock.c:89)
at __pthread_mutex_lock (pthread_mutex_lock.c:9)
at dlmalloc (dlmalloc.c:4638)
at operator new(unsigned operator new(unsigned long) (new.cpp:67)
```

I dont know why this happened, since there is only 1 place I use `emscripten_sleep(0) after emscripten_webgl_commit_frame()` trick to yield to js main thread:

```
void CommGLContextImpl::commit() {
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_RESULT r = emscripten_webgl_commit_frame();
assert(r == EMSCRIPTEN_RESULT_SUCCESS);

//see https://github.com/emscripten-core/emscripten/issues/10309
// "explicit swapping will be restricted to the OFFSCREEN_FRAMEBUFFER feature - and in OFFSCREEN_CANVAS, to swap one will need to yield back to browser event loop"
//also see https://emscripten.org/docs/porting/emscripten-runtime-environment.html
// "Another option is to use Asyncify which will rewrite the program so that it can return to the browser’s main event loop by just calling emscripten_sleep()."
emscripten_sleep(0); //needs -sASYNCIFY
#else
```

I had enabled emcc linker flags: -O3 -g -sOFFSCREENCANVAS_SUPPORT -sASYNCIFY -sASSERTIONS ...

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.