Emscripten: Uncaught unwind if call emscripten_set_main_loop() after emscripten_sleep()

Created on 18 Nov 2020  路  4Comments  路  Source: emscripten-core/emscripten

test.cpp:

#include <iostream>
#include <emscripten.h>

void on_frame() {
  std::cout << "."; // some work 
}

int main() {
  std::cout << "1"; // some code
  emscripten_sleep(100);
  std::cout << "2"; // some code
  emscripten_set_main_loop(on_frame, -1, 1);
  return 0;
}

to compile:
call emcc test.cpp -s ASYNCIFY=1 -s EXIT_RUNTIME=1 -o test.html

good first bug help wanted

All 4 comments

I suspect the issue here is that the main() function has a try-catch to hide the exception thrown by set_main_loop (we throw one to jump out of currently running code). But when asyncify is enabled then main exits and is called again from other JS code, when it resumes, and that call doesn't have such a try-catch. The fix is probably to add the try-catch to library_async.js in the proper place.

hello, I would like to help with this issue but I am pretty new and I'd need some more hints
up to now I:

  • run the test with node.js, obtaining the "unwind" exception as explained here
  • looked at the jsStackTrace that just contains:

at jsStackTrace (/home/torta/change/emscripten/emsdk/upstream/emscripten/test.js:1678:19)
at process. (/home/torta/change/emscripten/emsdk/upstream/emscripten/test.js:119:16)
at process.emit (events.js:315:20)
at process._fatalException (internal/process/execution.js:165:25)

  • tried to put a console.log(...) in the of callMain function of test.js and realized it is being called just once

any hint on how to proceed? Thank you

hi @giatorta

I think callMain is indeed called just once. But the main function itself is called again later. The way I would debug this is to build with -s WASM=0 which emits JS, all in a single file, which is simpler. You can then find function main and a console.log in there should show it is called twice.

Btw, you may get a better stack trace with a newer version of node. At some point the stack traces got a lot better. Or you can view stacks in a browser's devtools.

thanks @kripken

using -s WASM=0 and the -g option, I've now a js source with a __original_main() function corresponding to the C++ main()

I've put a console.log() into such function and, yes!, it gets called twice

now investigating where it's called the second time and trying to figure out the fix

Was this page helpful?
0 / 5 - 0 ratings