You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
-sMAIN_MODULE=2 with -pthread misses exports __emscripten_thread_crashed and __embind_initialize_bindings #21844
I'm using (a custom build of) Qt 6.7 with emscripten 3.1.58, with both -pthread and -sMAIN_MODULE=2.
When trying to run the build in a webbrowser, I'm getting an exception because __emscripten_thread_crashed is undefined, which is caused by an exception because __embind_initialize_bindings is undefined.
Setting -sEXPORTED_FUNCTIONS=_main,__emscripten_thread_crashed,__embind_initialize_bindings works around this (but this wasn't needed on 3.1.55 I think).
I'm guessing DCE deletes these because they're only called from javascript?
emcc --version:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.58 (a41843e0860e52c948c1fce20307933c6631c800)
Copyright (C) 2014 the Emscripten authors (see AUTHORS.txt)
This is free and open source software under the MIT license.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Those lines you highlighted in link.py also most likely executed, since I'm getting the warnings right above them.
Should I try to make a more minimal reproducer? For reference, this is the Dockerfile that causes the exception, but it takes around 1-2 hours to build without cache so you probably wouldn't want to build that one.
Edit: Also I forgot to mention, this error does not occur in debug mode
Ok, so the -sMAIN_MODULE=2 flag was a red herring, the same problem occurs when disabling it. With -O2 it works properly. The following program (still depending on Qt) has the same error:
#include <QNetworkAccessManager>
#include <QNetworkReply>
int main() {
auto manager = new QNetworkAccessManager();
auto request = QNetworkRequest(QUrl("https://example.com"));
request.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain");
manager->post(request, "msg");
}
Removing the calls resolves the issue, even if the same Qt libraries are linked with the same build options.
Bisecting didn't work as the commit for #21701 didn't include the statx symbol, which was included in the commit right after it. The commit with statx still has the problem though.
Actually, scrap that about -sMAIN_MODULE=2, I don't know if removing it does anything or not because removing it causes the symbols to be minified and I can't grep for __emscripten_thread_crashed anymore.
I added a stub statx and #21701 does indeed introduce the problem.
Are there any news here? I am facing the same issue without -sMAIN_MODULE=2. From my tests currently using emsdk 3.1.59 compiling with optimizations -Os, -O3 with pthreads is broken, rest work fine.
Compile flags: -O3 -Wall -Wextra -Wundef -Werror -Wno-error=pthreads-mem-growth -sDISABLE_EXCEPTION_CATCHING=0 -pthread
Link flags: --bind -sSTACK_SIZE=1MB -sALLOW_MEMORY_GROWTH=1 -sDISABLE_EXCEPTION_CATCHING=0 -sEXCEPTION_STACK_TRACES=1 -sENVIRONMENT=web,worker -sERROR_ON_UNDEFINED_SYMBOLS=1 -sEXPORTED_RUNTIME_METHODS=ccall -sNO_EXIT_RUNTIME=1 -sMAX_WEBGL_VERSION=2 -sMIN_WEBGL_VERSION=1 -sWASM=1 -sTEXTDECODER=0 -lidbfs.js -sWEBSOCKET_URL='wss://' -Wl,--shared-memory,--no-check-features
It seems while the functions are exported they are not visible in the worker context causing them to crash. This is a screenshot from MS Edge console showing one call when the Javascript context is set to top (main thread) and second call inside context em-pthread:
The _emscripten_run_callback_on_thread undefined symbol can be fixed by adding -lhtml5.js. This is needed because you added -sSTRICT which disables AUTO_JS_LIBRARIES by default.
I ended up solving my problem by adding __emscripten_thread_crashed and __embind_initialize_bindings to the EXPORTED_FUNCTIONS. But this further points out that there is a problem here because this should not be needed. A guess of mine is that DCE still eliminates these functions in higher optimizations because wasmExports['_emscripten_thread_crashed'] and wasmExports['_embind_initialize_bindings'] appear as undefined.
Are there any news here? I am facing the same issue without -sMAIN_MODULE=2. From my tests currently using emsdk 3.1.59 compiling with optimizations -Os, -O3 with pthreads is broken, rest work fine. Compile flags: -O3 -Wall -Wextra -Wundef -Werror -Wno-error=pthreads-mem-growth -sDISABLE_EXCEPTION_CATCHING=0 -pthread Link flags: --bind -sSTACK_SIZE=1MB -sALLOW_MEMORY_GROWTH=1 -sDISABLE_EXCEPTION_CATCHING=0 -sEXCEPTION_STACK_TRACES=1 -sENVIRONMENT=web,worker -sERROR_ON_UNDEFINED_SYMBOLS=1 -sEXPORTED_RUNTIME_METHODS=ccall -sNO_EXIT_RUNTIME=1 -sMAX_WEBGL_VERSION=2 -sMIN_WEBGL_VERSION=1 -sWASM=1 -sTEXTDECODER=0 -lidbfs.js -sWEBSOCKET_URL='wss://' -Wl,--shared-memory,--no-check-features
It seems while the functions are exported they are not visible in the worker context causing them to crash. This is a screenshot from MS Edge console showing one call when the Javascript context is set to top (main thread) and second call inside context em-pthread:
According to my tests with the Godot Web threaded builds (godotengine/godot#94725), wasm-metadce is stripping the _emscripten_thread_crashed symbol (along few others), and this causes the closure compiler to complain (this is how we found out):
@sbc100 one thing to note, is that in Godot's case, we only have the error when building with regular thread builds (i.e not MAIN_MODULE/SIDE_MODULE builds).
I suspect this is because we build dynlink builds MAIN_MODULE=1 and EXPORT_ALL=1, so in that case, meta-dce doesn't strip anything.
@brianpmaher you should not need to add __emscripten_thread_crashed or __embind_initialize_bindings to EXPORTED_FUNCTIONS. Those are internal things that emscripten should absolutely handle itself. Something very odd must be going on with your build if you are finding that you need to explicitly add them.
@brianpmaher I was able to reproduce you issue with wasmPromiseResolve is not a function. It was another side effect of building with -sSTRICT. I have a fix in flight but you can do -sINCOMING_MODULE_JS_API=instantaiteWasm in the mean time.
I removed the EXPORTED_FUNCTIONS and that continued with the same error. Added -sINCOMING_MODULE_JS_API=instantiateWasm (note there was a typo in your code block there s/instantaite/instantiate). and that worked for wasmPromiseResolve being defined.
I'm working through another issue with thread pool size not being large enough, but I think that's unrelated now.
I ended up solving my problem by adding __emscripten_thread_crashed and __embind_initialize_bindings to the EXPORTED_FUNCTIONS. But this further points out that there is a problem here because this should not be needed. A guess of mine is that DCE still eliminates these functions in higher optimizations because wasmExports['_emscripten_thread_crashed'] and wasmExports['_embind_initialize_bindings'] appear as undefined.
Are there any news here? I am facing the same issue without -sMAIN_MODULE=2. From my tests currently using emsdk 3.1.59 compiling with optimizations -Os, -O3 with pthreads is broken, rest work fine. Compile flags: -O3 -Wall -Wextra -Wundef -Werror -Wno-error=pthreads-mem-growth -sDISABLE_EXCEPTION_CATCHING=0 -pthread Link flags: --bind -sSTACK_SIZE=1MB -sALLOW_MEMORY_GROWTH=1 -sDISABLE_EXCEPTION_CATCHING=0 -sEXCEPTION_STACK_TRACES=1 -sENVIRONMENT=web,worker -sERROR_ON_UNDEFINED_SYMBOLS=1 -sEXPORTED_RUNTIME_METHODS=ccall -sNO_EXIT_RUNTIME=1 -sMAX_WEBGL_VERSION=2 -sMIN_WEBGL_VERSION=1 -sWASM=1 -sTEXTDECODER=0 -lidbfs.js -sWEBSOCKET_URL='wss://' -Wl,--shared-memory,--no-check-features
It seems while the functions are exported they are not visible in the worker context causing them to crash. This is a screenshot from MS Edge console showing one call when the Javascript context is set to top (main thread) and second call inside context em-pthread:
Update: This seems to be resolved in version 4.0.0
Activity
sbc100 commentedon Apr 28, 2024
Looks like a real bug which I imagine was introduced in #21701. I can take a look at this tomorrow.
sbc100 commentedon Apr 28, 2024
Actually I can't see how this is possible since
__emscripten_thread_crashedand__embind_initialize_bindingsshould be included viaemscripten/tools/link.py
Line 488 in 0e4c599
emscripten/tools/link.py
Line 496 in 0e4c599
I'm having trouble reproducing as when I build with the above options I see this in the generated JS file:
And the wasm file contains that
_emscripten_thread_crashedexport.8051Enthusiast commentedon Apr 28, 2024
Hmm, for me the js file contained the
var __emscripten_thread_crashed = ...line, but the exports don't contain it:Wasm exports
Those lines you highlighted in
link.pyalso most likely executed, since I'm getting the warnings right above them.Should I try to make a more minimal reproducer? For reference, this is the Dockerfile that causes the exception, but it takes around 1-2 hours to build without cache so you probably wouldn't want to build that one.
Edit: Also I forgot to mention, this error does not occur in debug mode
sbc100 commentedon Apr 29, 2024
Anything you can do to reduce the side the reproducer would be great. For example, can you try other opt levels (e.g.
-O2or-O1).You could also try to bisect to figure out if #21701 was really the change that introduced this issue. See https://emscripten.org/docs/contributing/developers_guide.html?highlight=developer#bisecting
8051Enthusiast commentedon Apr 29, 2024
Ok, so the
-sMAIN_MODULE=2flag was a red herring, the same problem occurs when disabling it. With-O2it works properly. The following program (still depending on Qt) has the same error:As does this one:
Removing the calls resolves the issue, even if the same Qt libraries are linked with the same build options.
Bisecting didn't work as the commit for #21701 didn't include the
statxsymbol, which was included in the commit right after it. The commit withstatxstill has the problem though.8051Enthusiast commentedon Apr 29, 2024
Actually, scrap that about
-sMAIN_MODULE=2, I don't know if removing it does anything or not because removing it causes the symbols to be minified and I can't grep for__emscripten_thread_crashedanymore.I added a stub
statxand #21701 does indeed introduce the problem.dsamo commentedon May 16, 2024
Are there any news here? I am facing the same issue without
-sMAIN_MODULE=2. From my tests currently using emsdk 3.1.59 compiling with optimizations-Os, -O3with pthreads is broken, rest work fine.Compile flags:
-O3 -Wall -Wextra -Wundef -Werror -Wno-error=pthreads-mem-growth -sDISABLE_EXCEPTION_CATCHING=0 -pthreadLink flags:
--bind -sSTACK_SIZE=1MB -sALLOW_MEMORY_GROWTH=1 -sDISABLE_EXCEPTION_CATCHING=0 -sEXCEPTION_STACK_TRACES=1 -sENVIRONMENT=web,worker -sERROR_ON_UNDEFINED_SYMBOLS=1 -sEXPORTED_RUNTIME_METHODS=ccall -sNO_EXIT_RUNTIME=1 -sMAX_WEBGL_VERSION=2 -sMIN_WEBGL_VERSION=1 -sWASM=1 -sTEXTDECODER=0 -lidbfs.js -sWEBSOCKET_URL='wss://' -Wl,--shared-memory,--no-check-featuresIt seems while the functions are exported they are not visible in the worker context causing them to crash. This is a screenshot from MS Edge console showing one call when the Javascript context is set to

top(main thread) and second call inside contextem-pthread:andreamancuso commentedon May 16, 2024
+1
Normally (on 3.1.56) I'd use the following linker flags:
-O3 -pthread -lembind --embind-emit-tsd interface.d.ts -s ENVIRONMENT='web,worker' -s MODULARIZE=1 -s ALLOW_MEMORY_GROWTH=1 -s WASM=1 -s USE_GLFW=3 -s USE_WEBGPU=1 -s NO_FILESYSTEM=1 -s NO_EXIT_RUNTIME=0 -s STANDALONE_WASM=0 -s EXIT_RUNTIME=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 -s MIN_WEBGL_VERSION=2 -s MAX_WEBGL_VERSION=2 -s DISABLE_EXCEPTION_CATCHING=0 -s PTHREAD_POOL_SIZE=12I just upgraded to 3.1.59 and added
-s STRICTbecause of #20580I got:
wasm-ld: error: C:\Users\[user]\AppData\Local\Temp\tmpm1_qwmwllibemscripten_js_symbols.so: undefined symbol: _emscripten_run_callback_on_thread. Required by emscripten_set_fullscreenchange_callback_on_threadwasm-ld: error: C:\Users\[user]\AppData\Local\Temp\tmpm1_qwmwllibemscripten_js_symbols.so: undefined symbol: _emscripten_run_callback_on_thread. Required by emscripten_set_wheel_callback_on_threadwasm-ld: error: C:\Users\[user]\AppData\Local\Temp\tmpm1_qwmwllibemscripten_js_symbols.so: undefined symbol: _emscripten_run_callback_on_thread. Required by emscripten_get_element_css_sizewasm-ld: error: C:\Users\[user]\AppData\Local\Temp\tmpm1_qwmwllibemscripten_js_symbols.so: undefined symbol: _emscripten_run_callback_on_thread. Required by emscripten_set_resize_callback_on_threadSwitching to
-O2appears to solve the issue, however I then get:Any help with this would be much appreciated.
sbc100 commentedon May 16, 2024
The
_emscripten_run_callback_on_threadundefined symbol can be fixed by adding-lhtml5.js. This is needed because you added-sSTRICTwhich disablesAUTO_JS_LIBRARIESby default.dsamo commentedon May 17, 2024
I ended up solving my problem by adding
__emscripten_thread_crashedand__embind_initialize_bindingsto theEXPORTED_FUNCTIONS. But this further points out that there is a problem here because this should not be needed. A guess of mine is that DCE still eliminates these functions in higher optimizations becausewasmExports['_emscripten_thread_crashed']andwasmExports['_embind_initialize_bindings']appear as undefined.andreamancuso commentedon May 17, 2024
Thank you.
I changed the linker flags to
-O3 -pthread -lembind --embind-emit-tsd interface.d.ts -lhtml5.js -lhtml5_webgl.js -s STRICT -s ENVIRONMENT='web,worker' -s MODULARIZE=1 -s ALLOW_MEMORY_GROWTH=1 -s WASM=1 -s USE_GLFW=3 -s USE_WEBGPU=1 -s NO_FILESYSTEM=1 -s NO_EXIT_RUNTIME=0 -s STANDALONE_WASM=0 -s EXIT_RUNTIME=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 -s MIN_WEBGL_VERSION=2 -s MAX_WEBGL_VERSION=2 -s DISABLE_EXCEPTION_CATCHING=0 -sPTHREAD_POOL_SIZE=12Sadly I am still getting:
I also tried adding
-s EXPORTED_FUNCTIONS=__emscripten_run_callback_on_threadbut it didn't make a difference.sbc100 commentedon Jun 18, 2024
You need to also add
-lhtml5(to link against the native version of libhtml5). Alternatively you should add-sAUTO_NATIVE_LIBRARIES.Faless commentedon Jul 26, 2024
According to my tests with the Godot Web threaded builds (godotengine/godot#94725),
wasm-metadceis stripping the_emscripten_thread_crashedsymbol (along few others), and this causes the closure compiler to complain (this is how we found out):Sadly, I was not yet able to make a reproducing test using the emscripten testing framework.
add: instructions for multithreaded build, extra linker flags
brianpmaher commentedon Aug 9, 2024
Any update on this? I'm encountering the same issue
2 remaining items
Faless commentedon Aug 10, 2024
@sbc100 we found out with 3.1.64, but also confirmed the error in 3.1.63 and 3.1.62 (minimum required version for godot builds with dynlink).
Faless commentedon Aug 10, 2024
@sbc100 one thing to note, is that in Godot's case, we only have the error when building with regular thread builds (i.e not
MAIN_MODULE/SIDE_MODULEbuilds).I suspect this is because we build dynlink builds
MAIN_MODULE=1andEXPORT_ALL=1, so in that case, meta-dce doesn't strip anything.brianpmaher commentedon Aug 10, 2024
@sbc100 sorry I should have been more specific.
Emscripten version 3.1.63
I was able to fix the
__emscripten_thread_crashedmissing symbol, but now getting:This is called as
wasmPromiseResolve(msgData["wasmModule"])from the wasm worker code path.Here are the flags:
The
-sASSERTIONSis temporary while trying to diagnose the issues heresbc100 commentedon Aug 11, 2024
@brianpmaher you should not need to add
__emscripten_thread_crashedor__embind_initialize_bindingstoEXPORTED_FUNCTIONS. Those are internal things that emscripten should absolutely handle itself. Something very odd must be going on with your build if you are finding that you need to explicitly add them.Perhaps you could share some kind of reproducer?
sbc100 commentedon Aug 11, 2024
@brianpmaher I was able to reproduce you issue with
wasmPromiseResolve is not a function. It was another side effect of building with-sSTRICT. I have a fix in flight but you can do-sINCOMING_MODULE_JS_API=instantaiteWasmin the mean time.brianpmaher commentedon Aug 11, 2024
Thanks, that worked!
I removed the
EXPORTED_FUNCTIONSand that continued with the same error. Added-sINCOMING_MODULE_JS_API=instantiateWasm(note there was a typo in your code block there s/instantaite/instantiate). and that worked for wasmPromiseResolve being defined.I'm working through another issue with thread pool size not being large enough, but I think that's unrelated now.
Fix pthreads in `-sSTRICT` mode
Fix pthreads in `-sSTRICT` mode
Fix pthreads in `-sSTRICT` mode (#22436)
wasm: Export required functions for Embind
dsamo commentedon Jan 23, 2025
Update: This seems to be resolved in version 4.0.0
sbc100 commentedon Jan 23, 2025
Great news!
fix: pin EXPORTED_FUNCTIONS to stop DCE stripping __emscripten_thread…
fix: pin EXPORTED_FUNCTIONS to stop DCE stripping __emscripten_thread…