Skip to content

Never run callbacks synchronously in emscripten_dlopen #24481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Jun 3, 2025

Having sometimes-syncrinous callbacks is a bad idea, and it was actually causing real issues with the emscripten_dlopen_promice API.

This introduced a new emscripten_queue_microtask API to match the long standing web API (also available in node).
See https://developer.mozilla.org/en-US/docs/Web/API/Window/queueMicrotask

See #24461 and pyodide/pyodide#5610 (comment)

Having sometimes-syncrinous callbacks is a bad idea, and it was actually
causing real issues with the emscripten_dlopen_promice API.

See emscripten-core#24461 and pyodide/pyodide#5610 (comment)
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jun 3, 2025
This change adds a new `emscripten_queue_microtask` API to match the
long standing web API (also available in node).

See https://developer.mozilla.org/en-US/docs/Web/API/Window/queueMicrotask

For now, I'm adding this without a polyfill so usage of the API will
crash on very old targets.  We could potentially polyfill using
something like setTimeout, but the semantics are a little different so
I'm not sure it worth doing.

Split out from emscripten-core#24481
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jun 3, 2025
This change adds a new `emscripten_queue_microtask` API to match the
long standing web API (also available in node).

See https://developer.mozilla.org/en-US/docs/Web/API/Window/queueMicrotask

For now, I'm adding this without a polyfill so usage of the API will
crash on very old targets.  We could potentially polyfill using
something like setTimeout, but the semantics are a little different so
I'm not sure it worth doing.

Split out from emscripten-core#24481
@ryanking13
Copy link
Contributor

ryanking13 commented Jun 4, 2025

Thanks for fixing this! I tested this patch in my Pyodide PR that use emscripten_dlopen and I get the following error during the build

cache:INFO: generating system library: sysroot/lib/wasm32-emscripten/pic/libc.a... (this will be cached in "/src/emsdk/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/pic/libc.a" for subsequent builds)
../../../system/lib/libc/dynlink.c:635:5: error: call to undeclared function 'emscripten_queue_microtask'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  635 |     emscripten_queue_microtask(run_success_callback, d);

Interestingly, adding #include <emscripten/eventloop.h> in dynlink.c doesn't seem to fix the issue. So I don't really understand the error...

Full cmdline was

/src/emsdk/emsdk/upstream/emscripten/emcc -c -O2 -Wall -fno-unroll-loops -std=c99 -D_XOPEN_SOURCE=700 -Wno-unused-result -Os -fno-inline-functions -fno-builtin -Wno-ignored-attributes -Wno-macro-redefined -Wno-shift-op-parentheses -Wno-string-plus-int -Wno-missing-braces -Wno-logical-op-parentheses -Wno-bitwise-op-parentheses -Wno-unused-but-set-variable -Wno-unused-variable -Wno-unused-label -Wno-pointer-sign -g -sSTRICT -Werror -sRELOCATABLE -DEMSCRIPTEN_DYNAMIC_LINKING -ffile-prefix-map=/src/emsdk/emsdk/upstream/emscripten=/emsdk/emscripten -ffile-prefix-map=../../..=/emsdk/emscripten -fdebug-compilation-dir=/emsdk/emscripten -I/src/emsdk/emsdk/upstream/emscripten/system/lib/libc/musl/src/internal -I/src/emsdk/emsdk/upstream/emscripten/system/lib/libc/musl/src/include -I/src/emsdk/emsdk/upstream/emscripten/system/lib/libc/musl/include -I/src/emsdk/emsdk/upstream/emscripten/system/lib/libc -I/src/emsdk/emsdk/upstream/emscripten/system/lib/pthread -DNDEBUG ../../../system/lib/libc/pthread_sigmask.c ../../../system/lib/libc/raise.c ../../../system/lib/libc/sigaction.c ../../../system/lib/libc/sigtimedwait.c ../../../system/lib/libc/wasi-helpers.c ../../../system/lib/libc/system.c ../../../system/lib/libc/dynlink.c ../../../system/lib/pthread/thread_profiler.c ../../../system/lib/libc/compat/__synccall.c ../../../system/lib/libc/compat/aligned_alloc.c ../../../system/lib/libc/compat/strlwr.c ../../../system/lib/libc/compat/strtol_l.c ../../../system/lib/libc/compat/strupr.c

@sbc100
Copy link
Collaborator Author

sbc100 commented Jun 4, 2025

Thanks for fixing this! I tested this patch in my Pyodide PR that use emscripten_dlopen and I get the following error during the build

cache:INFO: generating system library: sysroot/lib/wasm32-emscripten/pic/libc.a... (this will be cached in "/src/emsdk/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/pic/libc.a" for subsequent builds)
../../../system/lib/libc/dynlink.c:635:5: error: call to undeclared function 'emscripten_queue_microtask'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  635 |     emscripten_queue_microtask(run_success_callback, d);

Interestingly, adding #include <emscripten/eventloop.h> in dynlink.c doesn't seem to fix the issue. So I don't really understand the error...

Full cmdline was

/src/emsdk/emsdk/upstream/emscripten/emcc -c -O2 -Wall -fno-unroll-loops -std=c99 -D_XOPEN_SOURCE=700 -Wno-unused-result -Os -fno-inline-functions -fno-builtin -Wno-ignored-attributes -Wno-macro-redefined -Wno-shift-op-parentheses -Wno-string-plus-int -Wno-missing-braces -Wno-logical-op-parentheses -Wno-bitwise-op-parentheses -Wno-unused-but-set-variable -Wno-unused-variable -Wno-unused-label -Wno-pointer-sign -g -sSTRICT -Werror -sRELOCATABLE -DEMSCRIPTEN_DYNAMIC_LINKING -ffile-prefix-map=/src/emsdk/emsdk/upstream/emscripten=/emsdk/emscripten -ffile-prefix-map=../../..=/emsdk/emscripten -fdebug-compilation-dir=/emsdk/emscripten -I/src/emsdk/emsdk/upstream/emscripten/system/lib/libc/musl/src/internal -I/src/emsdk/emsdk/upstream/emscripten/system/lib/libc/musl/src/include -I/src/emsdk/emsdk/upstream/emscripten/system/lib/libc/musl/include -I/src/emsdk/emsdk/upstream/emscripten/system/lib/libc -I/src/emsdk/emsdk/upstream/emscripten/system/lib/pthread -DNDEBUG ../../../system/lib/libc/pthread_sigmask.c ../../../system/lib/libc/raise.c ../../../system/lib/libc/sigaction.c ../../../system/lib/libc/sigtimedwait.c ../../../system/lib/libc/wasi-helpers.c ../../../system/lib/libc/system.c ../../../system/lib/libc/dynlink.c ../../../system/lib/pthread/thread_profiler.c ../../../system/lib/libc/compat/__synccall.c ../../../system/lib/libc/compat/aligned_alloc.c ../../../system/lib/libc/compat/strlwr.c ../../../system/lib/libc/compat/strtol_l.c ../../../system/lib/libc/compat/strupr.c

Likely the sysroot would need updating to get the header file.

You can use ./embuilder build sysroot --force to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants