Skip to content

Fix multithreading to work in Deno and Bun - #25947

Merged
sbc100 merged 4 commits into
emscripten-core:mainfrom
Macil:fixThreadingForDenoAndBun
Jan 10, 2026
Merged

sbc100 merged 4 commits into
emscripten-core:mainfrom
Macil:fixThreadingForDenoAndBun

Conversation

@Macil

@Macil Macil commented Dec 13, 2025 •

Copy link
Copy Markdown
Contributor

Projects using Emscripten with pthreads don't work in Deno or Bun because those runtimes implement Web Worker APIs that Node doesn't, and Emscripten attempts to re-implement those APIs when running in Node/Deno/Bun in a way that conflicts with Deno and Bun's own implementations. This PR feature-detects Deno and Bun's postMessage implementation and avoids setting up its own conflicting implementation in that case.

Fixes: denoland/deno#17171

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

We also now have some testing under bun as of #25903. Can you add 1 or more pthread-based tests to the lists of bun tests in .circleci/config.yml?

Comment thread src/runtime_common.js Resolved
@Macil
Macil force-pushed the fixThreadingForDenoAndBun branch 6 times, most recently from 60733b1 to b42b004 Compare December 13, 2025 23:03
@Macil
Macil marked this pull request as draft December 13, 2025 23:03
@Macil
Macil force-pushed the fixThreadingForDenoAndBun branch 7 times, most recently from f9c2ff3 to ab5c5c6 Compare December 14, 2025 03:03
Comment thread src/wasm_worker.js Outdated
return f;
}

const parentPort = worker_threads['parentPort'];

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(The lines below were relying on this variable being set in runtime_common.js, which I had moved into an if-block and made not global. I discovered this after tests failed. I figured the simplest solution was to make this code use its own local variable instead of re-introducing the global variable.)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To save on code size and keep this patch as small as possible can you revert this and hoist const parentPort = worker_threads['parentPort']; outside the if?

Also, we still use var in most places in our codebase sadly (there are a couple of reasons, that are probably not worth explaining here... i really should create an FAQ entry or some such for it).

@Macil
Macil marked this pull request as ready for review December 14, 2025 04:02
@Macil

Macil commented Dec 14, 2025

Copy link
Copy Markdown
Contributor Author

I've added some pthread tests to the Bun tests and added some Deno tests too.

(I originally added a few tests for Deno that happened to use Emscripten's PROXY_TO_PTHREAD option, but those tests failed. I spent a while debugging it so I could maybe fix that issue in this PR too, but it turns out to be because Deno doesn't implement Atomics.waitAsync.)

Comment thread test/common.py Outdated
return config.DENO_ENGINE
return None

def get_node_bun_or_deno(self):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I'm not sure if some increasingly specific helper functions like this were the best route to go. One alternative would have been to make the get/require_node helpers to allow Deno/Bun too because they're mostly interchangeable, but it's probably better to be explicit. Another alternative would've been to make some generic helpers that take named parameters declaring which of Node/Node-Canary/Deno/Bun are allowed.)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm hoping we can just avoid this completely, but allowing NODE_JS_TEST to be set to /path/to/bun or /path/to/deno. No need for new config vars I think/hope.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I embraced that idea and it's much simpler now.

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep this change small and focused can you remove all the Deno stuff and we can consider that in a separate PR.

Comment thread test/common.py Resolved
Comment thread src/pthread_esm_startup.mjs Resolved
@Macil
Macil marked this pull request as draft December 16, 2025 02:58
@Macil
Macil force-pushed the fixThreadingForDenoAndBun branch 4 times, most recently from 29a7b1f to ff084dd Compare December 16, 2025 03:46
@Macil
Macil marked this pull request as ready for review December 16, 2025 03:56
@Macil
Macil force-pushed the fixThreadingForDenoAndBun branch from ff084dd to 5bd061c Compare December 16, 2025 03:58
Comment thread tools/shared.py Outdated
if override:
actual = override
else:
actual = utils.run_process(nodejs + ['--version'], stdout=PIPE).stdout.strip()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this change would live only in test/ code and only effect NODE_JS_TEST, not the main version of node js that we use.

i.e. can you make local function in test/common.py that does this override? Then use the local version instead of shared.get_node_version?

auto-merge was automatically disabled January 9, 2026 06:54

Head branch was pushed to by a user without write access

Comment thread .circleci/config.yml
echo "BUN_ENGINE = os.path.expanduser('~/.bun/bin/bun')" >> ~/emsdk/.emscripten
echo "JS_ENGINES = [BUN_ENGINE]" >> ~/emsdk/.emscripten
echo "NODE_JS_TEST = os.path.expanduser('~/.bun/bin/bun')" >> ~/emsdk/.emscripten
echo "JS_ENGINES = [NODE_JS_TEST]" >> ~/emsdk/.emscripten

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remind my why this change is necessary? I can see that it might be needed, but it also seem unfortunate that it would be.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pthread tests look at JS_ENGINES and require that Node is used and that it's at least a specific version.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I guess I was hoping your OVERRIDE_NODE_JS_TEST_VERSION thing would by pass that check.

This change LGTM then, I can take a look at cleaning this stuff up as a followup.

Comment thread test/test_other.py Resolved
@sbc100

sbc100 commented Jan 10, 2026

Copy link
Copy Markdown
Collaborator

Would you be interested in creating a separate PR to add some minimal Deno testing to the CI?

@sbc100
sbc100 enabled auto-merge (squash) January 10, 2026 23:43
@sbc100
sbc100 merged commit ff7ac7b into emscripten-core:main Jan 10, 2026
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jan 15, 2026
sbc100 added a commit that referenced this pull request Jan 15, 2026
This was originally part of #25947.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jan 15, 2026
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jan 15, 2026
sbc100 added a commit that referenced this pull request Jan 15, 2026
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jan 15, 2026
Also, remove the need Deno/Bun to set NODE_JS_TEST and the special
OVERRIDE_NODE_JS_TEST_VERSION that was added in emscripten-core#25947
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jan 15, 2026
Also, remove the need Deno/Bun to set NODE_JS_TEST and the special
OVERRIDE_NODE_JS_TEST_VERSION that was added in emscripten-core#25947
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jan 15, 2026
Also, remove the need Deno/Bun to set NODE_JS_TEST and the special
OVERRIDE_NODE_JS_TEST_VERSION that was added in emscripten-core#25947
sbc100 added a commit that referenced this pull request Jan 15, 2026
Also, remove the need Deno/Bun to set `NODE_JS_TEST` and the special
`OVERRIDE_NODE_JS_TEST_VERSION` that was added in #25947
inolen pushed a commit to inolen/emscripten that referenced this pull request Feb 13, 2026
Projects using Emscripten with pthreads don't work in Deno or Bun
because those runtimes implement Web Worker APIs that Node doesn't, and
Emscripten attempts to re-implement those APIs when running in
Node/Deno/Bun in a way that conflicts with Deno and Bun's own
implementations. This PR feature-detects Deno and Bun's `postMessage`
implementation and avoids setting up its own conflicting implementation
in that case.

Fixes: denoland/deno#17171

---------

Co-authored-by: Alon Zakai <[email protected]>
inolen pushed a commit to inolen/emscripten that referenced this pull request Feb 13, 2026
inolen pushed a commit to inolen/emscripten that referenced this pull request Feb 13, 2026
Also, remove the need Deno/Bun to set `NODE_JS_TEST` and the special
`OVERRIDE_NODE_JS_TEST_VERSION` that was added in emscripten-core#25947
sbc100 added a commit to sbc100/emscripten that referenced this pull request Aug 20, 2026
In emscripten-core#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: emscripten-core#27580
sbc100 added a commit to sbc100/emscripten that referenced this pull request Aug 22, 2026
In emscripten-core#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: emscripten-core#27580
sbc100 added a commit to sbc100/emscripten that referenced this pull request Aug 22, 2026
In emscripten-core#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: emscripten-core#27580
sbc100 added a commit to sbc100/emscripten that referenced this pull request Aug 26, 2026
In emscripten-core#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: emscripten-core#27580
sbc100 added a commit to sbc100/emscripten that referenced this pull request Aug 26, 2026
In emscripten-core#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 defined,
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: emscripten-core#27580
sbc100 added a commit to sbc100/emscripten that referenced this pull request Aug 26, 2026
In emscripten-core#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 defined,
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: emscripten-core#27580
sbc100 added a commit to sbc100/emscripten that referenced this pull request Aug 27, 2026
In emscripten-core#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 defined,
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: emscripten-core#27580
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

node-compat: z3-solver

4 participants