From cc426e2e80f31497c817be6665aac2e1284cca22 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Thu, 20 Aug 2026 16:38:27 -0700 Subject: [PATCH] Fix Bun 1.4 + pthreads by always bridging `parentPort` in worker threads In #25947, a check for `!globalThis.postMessage` was added before wiring `parentPort.on('message') -> globalThis.onmessage` and `globalThis.postMessage = (msg) => parentPort.postMessage(msg)`. However, in Bun 1.4+, `node:worker_threads` deliveries are isolated to `parentPort` (they are no longer automatically routed to `globalThis.onmessage`), while `globalThis.postMessage` remains defined on the global object. Because `globalThis.postMessage` was truthy, Emscripten skipped hooking `parentPort.on('message')`, leaving worker threads unresponsive to initialization messages from the main thread. Removing the `if (!globalThis.postMessage)` guard ensures `parentPort` is always correctly wired whenever running in a Node-style worker thread. Fixes: #27580 --- .circleci/config.yml | 5 +---- src/pthread_esm_startup.mjs | 9 ++------- src/runtime_common.js | 9 ++------- test/codesize/test_codesize_minimal_pthreads.json | 8 ++++---- .../test_codesize_minimal_pthreads_memgrowth.json | 8 ++++---- 5 files changed, 13 insertions(+), 26 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index bfb12cd2ed94e..2c4d7219d47fc 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -805,10 +805,7 @@ jobs: - run: name: install bun command: | - # Temporally pin to 1.3.14 (the last non-rust version) until we - # can investigate the breakages: - # https://github.com/emscripten-core/emscripten/issues/27580 - curl -fsSL https://bun.com/install | bash -s "bun-v1.3.14" + curl -fsSL https://bun.com/install | bash echo "BUN = '~/.bun/bin/bun'" >> ~/emsdk/.emscripten echo "JS_ENGINES = [BUN]" >> ~/emsdk/.emscripten - run-tests: diff --git a/src/pthread_esm_startup.mjs b/src/pthread_esm_startup.mjs index 6054fb99b9237..7a5f534022dd5 100644 --- a/src/pthread_esm_startup.mjs +++ b/src/pthread_esm_startup.mjs @@ -20,13 +20,8 @@ if ({{{ nodeDetectionCode() }}}) { var worker_threads = await import('node:worker_threads'); globalThis.Worker = worker_threads.Worker; var parentPort = worker_threads.parentPort; - // Deno and Bun already have `postMessage` defined on the global scope and - // deliver messages to `globalThis.onmessage`, so we must not duplicate that - // behavior here if `postMessage` is already present. - if (!globalThis.postMessage) { - parentPort.on('message', (msg) => globalThis.onmessage?.({ data: msg })); - globalThis.postMessage = (msg) => parentPort.postMessage(msg); - } + parentPort.on('message', (msg) => globalThis.onmessage?.({ data: msg })); + globalThis.postMessage = (msg) => parentPort.postMessage(msg); } #endif diff --git a/src/runtime_common.js b/src/runtime_common.js index 09bf17956aa6d..379d12ac4690f 100644 --- a/src/runtime_common.js +++ b/src/runtime_common.js @@ -39,13 +39,8 @@ if (ENVIRONMENT_IS_NODE && {{{ ENVIRONMENT_IS_WORKER_THREAD() }}}) { // Create as web-worker-like an environment as we can. globalThis.self = globalThis; var parentPort = worker_threads.parentPort; - // Deno and Bun already have `postMessage` defined on the global scope and - // deliver messages to `globalThis.onmessage`, so we must not duplicate that - // behavior here if `postMessage` is already present. - if (!globalThis.postMessage) { - parentPort.on('message', (msg) => globalThis.onmessage?.({ data: msg })); - globalThis.postMessage = (msg) => parentPort.postMessage(msg); - } + parentPort.on('message', (msg) => globalThis.onmessage?.({ data: msg })); + globalThis.postMessage = (msg) => parentPort.postMessage(msg); // Node.js Workers do not pass postMessage()s and uncaught exception events to the parent // thread necessarily in the same order where they were generated in sequential program order. // See https://github.com/nodejs/node/issues/59617 diff --git a/test/codesize/test_codesize_minimal_pthreads.json b/test/codesize/test_codesize_minimal_pthreads.json index d0651f8052264..0e32d28d17872 100644 --- a/test/codesize/test_codesize_minimal_pthreads.json +++ b/test/codesize/test_codesize_minimal_pthreads.json @@ -1,10 +1,10 @@ { - "a.out.js": 6883, - "a.out.js.gz": 3422, + "a.out.js": 6857, + "a.out.js.gz": 3416, "a.out.nodebug.wasm": 19132, "a.out.nodebug.wasm.gz": 8845, - "total": 26015, - "total_gz": 12267, + "total": 25989, + "total_gz": 12261, "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 c258456676ca6..822216dfa330e 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": 7341, - "a.out.js.gz": 3639, + "a.out.js": 7315, + "a.out.js.gz": 3633, "a.out.nodebug.wasm": 19133, "a.out.nodebug.wasm.gz": 8846, - "total": 26474, - "total_gz": 12485, + "total": 26448, + "total_gz": 12479, "sent": [ "a (memory)", "b (exit)",