Skip to content

Commit d6e3038

Browse files
committed
Fix for exceptions in Wasm Workers under node
Without this change exceptions thrown in Wasm Workers are lost under node.
1 parent 54ef4c6 commit d6e3038

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

src/lib/libwasm_worker.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,17 @@ if (ENVIRONMENT_IS_WASM_WORKER
223223
#if ENVIRONMENT_MAY_BE_NODE
224224
if (ENVIRONMENT_IS_NODE) {
225225
/** @suppress {checkTypes} */
226-
worker.on('message', (msg) => worker.onmessage({ data: msg }));
226+
worker.on('message', (msg) => {
227+
if (msg['cmd'] == 'uncaughtException') {
228+
// Message handler for Node.js specific out-of-order behavior:
229+
// https://github.com/nodejs/node/issues/59617
230+
// A worker sent an uncaught exception event. Re-raise it on the main thread.
231+
err(`worker sent an error! ${msg.error.message}`);
232+
throw msg.error;
233+
} else {
234+
worker.onmessage({ data: msg });
235+
}
236+
});
227237
}
228238
#endif
229239
#if RUNTIME_DEBUG

test/test_core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9713,6 +9713,10 @@ def test_wasm_worker_hello(self):
97139713
self.maybe_closure()
97149714
self.do_run_in_out_file_test('wasm_worker/hello_wasm_worker.c', cflags=['-sWASM_WORKERS'])
97159715

9716+
@requires_wasm_workers
9717+
def test_wasm_worker_exceptions(self):
9718+
self.do_runf('wasm_worker/test_wasm_worker_exceptions.c', 'worker sent an error! Aborted', assert_returncode=NON_ZERO, cflags=['-sWASM_WORKERS'])
9719+
97169720
@requires_wasm_workers
97179721
def test_wasm_worker_malloc(self):
97189722
self.do_run_in_out_file_test('wasm_worker/malloc_wasm_worker.c', cflags=['-sWASM_WORKERS'])
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
#include <emscripten/emscripten.h>
5+
#include <emscripten/console.h>
6+
#include <emscripten/em_asm.h>
7+
#include <emscripten/wasm_worker.h>
8+
9+
void do_abort() {
10+
emscripten_out("Hello from wasm worker!");
11+
abort();
12+
}
13+
14+
int main() {
15+
emscripten_out("in main");
16+
emscripten_wasm_worker_post_function_v(emscripten_malloc_wasm_worker(1024), do_abort);
17+
emscripten_exit_with_live_runtime();
18+
assert(false && "should never get here");
19+
}

0 commit comments

Comments
 (0)