Skip to content

runtime hang in side thread with flto #16021

Closed

Description

@sps-gold

Building main\side with flto with thread invocation in side module results in hang during runtime in node env

Version of emscripten/emsdk:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.1 (1934a98)

Failing command line in full:

em++ side.cpp -o side.wasm -sSIDE_MODULE=2 -pthread -sPTHREAD_POOL_SIZE=1 -flto
em++ main.cpp -sMAIN_MODULE=2 side.wasm -sEXIT_RUNTIME -pthread -sPTHREAD_POOL_SIZE=1 -flto
node --experimental-wasm-threads --experimental-wasm-bulk-memory a.out.js

Full link command and output with -v appended:

SIDE
"/home/sps/Source/github/sps-gold/emsdk/upstream/bin/wasm-ld" -o side.wasm /home/sps/Learn/emscripten_temp_lsbli5qt/side_0.o -L/home/sps/Source/github/sps-gold/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/lto-pic /home/sps/Source/github/sps-gold/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/lto-pic/crtbegin.o -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --import-undefined --import-memory --shared-memory --strip-debug --export-if-defined=main --export-if-defined=_emscripten_thread_init --export-if-defined=_emscripten_thread_exit --export-if-defined=emscripten_tls_init --export-if-defined=emscripten_current_thread_process_queued_calls --export-if-defined=pthread_self --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=fflush --export-if-defined=setThrew --export-if-defined=emscripten_stack_get_end --export-if-defined=emscripten_stack_get_free --export-if-defined=emscripten_stack_set_limits --export-if-defined=stackSave --export-if-defined=stackRestore --export-if-defined=stackAlloc --export-if-defined=__wasm_call_ctors --export-if-defined=__errno_location --export-if-defined=emscripten_dispatch_to_thread_ --export-if-defined=_emscripten_main_thread_futex --export-if-defined=_emscripten_thread_free_data --export-if-defined=_emscripten_allow_main_runtime_queued_calls --export-if-defined=emscripten_main_browser_thread_id --export-if-defined=emscripten_main_thread_process_queued_calls --export-if-defined=emscripten_run_in_main_runtime_thread_js --export-if-defined=emscripten_sync_run_in_main_thread_2 --export-if-defined=emscripten_sync_run_in_main_thread_4 --export-if-defined=memalign --export-if-defined=malloc --export-if-defined=free --experimental-pic -shared --no-export-dynamic
 "/home/sps/Source/github/sps-gold/emsdk/upstream/bin/wasm-emscripten-finalize" --minimize-wasm-changes --dyncalls-i64 --pass-arg=legalize-js-interface-export-originals --side-module side.wasm -o side.wasm --detect-features
 "/home/sps/Source/github/sps-gold/emsdk/upstream/bin/llvm-objcopy" side.wasm side.wasm --remove-section=.debug* --remove-section=producers
MAIN
"/home/sps/Source/github/sps-gold/emsdk/upstream/bin/wasm-ld" -o a.out.wasm /home/sps/Learn/emscripten_temp_owzf0yt3/main_0.o side.wasm -L/home/sps/Source/github/sps-gold/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/lto-pic /home/sps/Source/github/sps-gold/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/lto-pic/crtbegin.o -lGL-mt -lal -lhtml5 -lstubs-debug -lc-mt-debug -lcompiler_rt-mt -lc++-mt-noexcept -lc++abi-mt-noexcept -ldlmalloc-mt -lc_rt-mt -lsockets-mt -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --import-undefined --import-memory --shared-memory --strip-debug --export-if-defined=main --export-if-defined=_emscripten_thread_init --export-if-defined=_emscripten_thread_exit --export-if-defined=emscripten_tls_init --export-if-defined=emscripten_current_thread_process_queued_calls --export-if-defined=pthread_self --export-if-defined=_Znwm --export-if-defined=_ZdlPv --export-if-defined=_ZNKSt3__28ios_base6getlocEv --export-if-defined=_ZNSt3__28ios_base5clearEj --export-if-defined=_ZNKSt3__26locale9use_facetERNS0_2idE --export-if-defined=_ZNSt3__212basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEED1Ev --export-if-defined=_ZNSt3__25ctypeIcE2idE --export-if-defined=_ZNSt3__215__thread_structD1Ev --export-if-defined=_ZNSt3__215__thread_structC1Ev --export-if-defined=_ZNSt3__220__throw_system_errorEiPKc --export-if-defined=emscripten_builtin_memalign --export-if-defined=_ZNSt3__26threadD1Ev --export-if-defined=_ZNSt3__213basic_ostreamIcNS_11char_traitsIcEEE5flushEv --export-if-defined=_ZNSt3__219__thread_local_dataEv --export-if-defined=_ZNSt3__212basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE6__initEmc --export-if-defined=_ZNSt3__213basic_ostreamIcNS_11char_traitsIcEEE6sentryD1Ev --export-if-defined=__memory_base --export-if-defined=printf --export-if-defined=_ZNSt3__213basic_ostreamIcNS_11char_traitsIcEEE3putEc --export-if-defined=_ZNSt3__26localeD1Ev --export-if-defined=_ZNSt3__213basic_ostreamIcNS_11char_traitsIcEEE6sentryC1ERS3_ --export-if-defined=_ZNSt3__26thread4joinEv --export-if-defined=strlen --export-if-defined=pthread_create --export-if-defined=_ZNSt3__212basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE6__initEPKcm --export-if-defined=_ZNSt3__24coutE --export-if-defined=__table_base --export-if-defined=pthread_setspecific --export-if-defined=__stack_pointer --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=fflush --export=setThrew --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_set_limits --export=stackSave --export=stackRestore --export=stackAlloc --export=__wasm_call_ctors --export=__errno_location --export=emscripten_dispatch_to_thread_ --export=_emscripten_main_thread_futex --export=_emscripten_thread_free_data --export=_emscripten_allow_main_runtime_queued_calls --export=emscripten_main_browser_thread_id --export=emscripten_main_thread_process_queued_calls --export=emscripten_run_in_main_runtime_thread_js --export=emscripten_sync_run_in_main_thread_2 --export=emscripten_sync_run_in_main_thread_4 --export=memalign --export=__funcs_on_exit --export=malloc --export=free --export=__dl_seterr --experimental-pic -pie --no-export-dynamic -z stack-size=5242880 --initial-memory=16777216 --no-entry --max-memory=16777216
 "/home/sps/Source/github/sps-gold/emsdk/upstream/bin/wasm-emscripten-finalize" --minimize-wasm-changes --dyncalls-i64 --pass-arg=legalize-js-interface-export-originals a.out.wasm -o a.out.wasm --detect-features
 "/home/sps/Source/github/sps-gold/emsdk/node/14.18.2_64bit/bin/node" /home/sps/Source/github/sps-gold/emsdk/upstream/emscripten/src/compiler.js /home/sps/Learn/tmp6xgf9i38.json
 "/home/sps/Source/github/sps-gold/emsdk/upstream/bin/llvm-objcopy" a.out.wasm a.out.wasm --remove-section=.debug* --remove-section=producers
 "/home/sps/Source/github/sps-gold/emsdk/node/14.18.2_64bit/bin/node" /home/sps/Source/github/sps-gold/emsdk/upstream/emscripten/tools/preprocessor.js /home/sps/Learn/emscripten_temp_owzf0yt3/settings.js worker.js --expandMacros

Runtime
node v17.3.1

test case
#16020

Result
hangs while calling threaded function in side

hello from main
hello from side
^C

expected: same as if we remove -flto flag

hello from main
hello from side
call thread fn in side:success
value from side = 123
call thread fn in main:success

Activity

  1. changed the title [-]node runtime hang in side thread with flto[/-] [+]runtime hang in side thread with flto[/+] on Jan 17, 2022
  2. sps-gold commented on Jan 17, 2022

    @sps-gold
    ContributorAuthor

    same behavior observed with Chrome 97. attaching log files
    chrome97_hang.log
    chrome97_nohang_without_flto.log

  3. sps-gold commented on Jan 17, 2022

    @sps-gold
    ContributorAuthor
  4. sps-gold commented on Jan 21, 2022

    @sps-gold
    ContributorAuthor

    I encounter another hang with thread in side even without building with flto. And it looks like a different issue, though it could be related, so logging it here
    #16079

  5. sps-gold commented on Jan 24, 2022

    @sps-gold
    ContributorAuthor

    hi @sbc100 any thoughts/comments on this flto specific issue

  6. sbc100 commented on Jan 24, 2022

    @sbc100
    Collaborator

    Its most likely a but in pthreads + dynamic linking (which is still experimental so bugs are to be expected). Its not clear to me how/why LTO would trigger it.

    Clearly a genuine bug though. I will try to look into it when I get a chance.

  7. sbc100 commented on Jan 31, 2022

    @sbc100
    Collaborator

    The error message here was being hidden because the main thread was busy. I have a fix for that issue in #16161... although this doesn't actually fix the underlying issue which is that the wasmTable is not in sync:

    worker.js onmessage() captured an uncaught exception: RangeError: WebAssembly.Table.get(): invalid index 42 into function table
    RangeError: WebAssembly.Table.get(): invalid index 42 into function table
        at getWasmTableEntry (eval at importScripts (/usr/local/google/home/sbc/dev/wasm/emscripten/a.out.worker.js:38:16), <anonymous>:2394:53)
        at Object.invokeEntryPoint (eval at importScripts (/usr/local/google/home/sbc/dev/wasm/emscripten/a.out.worker.js:38:16), <anonymous>:2404:14)
        at self.onmessage (/usr/local/google/home/sbc/dev/wasm/emscripten/a.out.worker.js:139:48)
        at MessagePort.<anonymous> (/usr/local/google/home/sbc/dev/wasm/emscripten/a.out.worker.js:24:5)
        at MessagePort.[nodejs.internal.kHybridDispatch] (internal/event_target.js:399:24)
        at MessagePort.exports.emitMessage (internal/per_context/messageport.js:18:26)
    
  8. added a commit that references this issue on Feb 10, 2022
    12b4de3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions