Repository navigation
Fix multithreading to work in Deno and Bun - #25947
Conversation
60733b1 to
b42b004
Compare
f9c2ff3 to
ab5c5c6
Compare
| return f; | ||
| } | ||
|
|
||
| const parentPort = worker_threads['parentPort']; |
There was a problem hiding this comment.
(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.)
There was a problem hiding this comment.
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).
|
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 |
| return config.DENO_ENGINE | ||
| return None | ||
|
|
||
| def get_node_bun_or_deno(self): |
There was a problem hiding this comment.
(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.)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I embraced that idea and it's much simpler now.
sbc100
left a comment
There was a problem hiding this comment.
To keep this change small and focused can you remove all the Deno stuff and we can consider that in a separate PR.
29a7b1f to
ff084dd
Compare
ff084dd to
5bd061c
Compare
| if override: | ||
| actual = override | ||
| else: | ||
| actual = utils.run_process(nodejs + ['--version'], stdout=PIPE).stdout.strip() |
There was a problem hiding this comment.
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?
Head branch was pushed to by a user without write access
| 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
The pthread tests look at JS_ENGINES and require that Node is used and that it's at least a specific version.
There was a problem hiding this comment.
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.
|
Would you be interested in creating a separate PR to add some minimal Deno testing to the CI? |
This was originally part of emscripten-core#25947.
This was originally part of #25947.
This was originally part of emscripten-core#25947.
This was originally part of emscripten-core#25947.
This was originally part of #25947.
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
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
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
Also, remove the need Deno/Bun to set `NODE_JS_TEST` and the special `OVERRIDE_NODE_JS_TEST_VERSION` that was added in #25947
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]>
This was originally part of emscripten-core#25947.
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
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
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
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
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
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
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
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
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
postMessageimplementation and avoids setting up its own conflicting implementation in that case.Fixes: denoland/deno#17171