From e475aa1b68e3d54730a619064be9340236e34d64 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Sat, 13 Jun 2026 14:03:04 -0700 Subject: [PATCH] Add a couple more checks/assertions to pthread startup code. NFC I recently saw an issue in Firefox where the CMD_LOAD message was never received. This then caused CMD_RUN to crash which in turn caused `__emscripten_thread_crashed` to be referenced before initialization. --- src/postamble_minimal.js | 2 +- src/runtime_common.js | 2 +- src/runtime_pthread.js | 5 ++++- test/codesize/test_codesize_minimal_pthreads.json | 8 ++++---- .../test_codesize_minimal_pthreads_memgrowth.json | 8 ++++---- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/postamble_minimal.js b/src/postamble_minimal.js index bf8b65b7af6ef..132faf1d0b06f 100644 --- a/src/postamble_minimal.js +++ b/src/postamble_minimal.js @@ -117,7 +117,7 @@ function run() { #endif function initRuntime(wasmExports) { -#if ASSERTIONS || SAFE_HEAP || USE_ASAN || MODULARIZE +#if ASSERTIONS || SAFE_HEAP || USE_ASAN || MODULARIZE || PTHREADS runtimeInitialized = true; #endif diff --git a/src/runtime_common.js b/src/runtime_common.js index 23a3915cb14cc..aea1cefcef55d 100644 --- a/src/runtime_common.js +++ b/src/runtime_common.js @@ -80,7 +80,7 @@ if (ENVIRONMENT_IS_NODE && {{{ ENVIRONMENT_IS_WORKER_THREAD() }}}) { var HEAP_DATA_VIEW; #endif -#if !MINIMAL_RUNTIME || ASSERTIONS || SAFE_HEAP || USE_ASAN || MODULARIZE +#if !MINIMAL_RUNTIME || ASSERTIONS || SAFE_HEAP || USE_ASAN || MODULARIZE || PTHREADS var runtimeInitialized = false; #endif diff --git a/src/runtime_pthread.js b/src/runtime_pthread.js index 9423a22822487..69023d15a3408 100644 --- a/src/runtime_pthread.js +++ b/src/runtime_pthread.js @@ -131,6 +131,9 @@ if (ENVIRONMENT_IS_PTHREAD) { } else if (cmd == {{{ CMD_RUN }}}) { #if ASSERTIONS assert(msgData.pthread_ptr); +#if !WASM_ESM_INTEGRATION + assert(wasmMemory, "CMD_RUN received before CMD_LOAD"); +#endif #endif // Call inside JS module to set up the stack frame for this pthread in JS module scope. // This needs to be the first thing that we do, as we cannot call to any C/C++ functions @@ -190,7 +193,7 @@ if (ENVIRONMENT_IS_PTHREAD) { err(`worker: onmessage() captured an uncaught exception: ${ex}`); if (ex?.stack) err(ex.stack); #endif - __emscripten_thread_crashed(); + if (runtimeInitialized) __emscripten_thread_crashed(); throw ex; } }; diff --git a/test/codesize/test_codesize_minimal_pthreads.json b/test/codesize/test_codesize_minimal_pthreads.json index a60cc29cf4110..d0c9cb6e314ee 100644 --- a/test/codesize/test_codesize_minimal_pthreads.json +++ b/test/codesize/test_codesize_minimal_pthreads.json @@ -1,10 +1,10 @@ { - "a.out.js": 6949, - "a.out.js.gz": 3427, + "a.out.js": 6969, + "a.out.js.gz": 3438, "a.out.nodebug.wasm": 19063, "a.out.nodebug.wasm.gz": 8803, - "total": 26012, - "total_gz": 12230, + "total": 26032, + "total_gz": 12241, "sent": [ "a (memory)", "b (exit)", diff --git a/test/codesize/test_codesize_minimal_pthreads_memgrowth.json b/test/codesize/test_codesize_minimal_pthreads_memgrowth.json index ad0524a4dd8a1..177e08d93e5b5 100644 --- a/test/codesize/test_codesize_minimal_pthreads_memgrowth.json +++ b/test/codesize/test_codesize_minimal_pthreads_memgrowth.json @@ -1,10 +1,10 @@ { - "a.out.js": 7357, - "a.out.js.gz": 3627, + "a.out.js": 7377, + "a.out.js.gz": 3640, "a.out.nodebug.wasm": 19064, "a.out.nodebug.wasm.gz": 8804, - "total": 26421, - "total_gz": 12431, + "total": 26441, + "total_gz": 12444, "sent": [ "a (memory)", "b (exit)",