summaryrefslogtreecommitdiff
path: root/tests/unit_node/worker_threads_test.ts
AgeCommit message (Collapse)Author
2024-10-04fix(node): fix worker_threads issues blocking Angular support (#26024)Nathan Whitaker
Fixes #22995. Fixes #23000. There were a handful of bugs here causing the hang (each with a corresponding minimized test): - We were canceling recv futures when `receiveMessageOnPort` was called, but this caused the "receive loop" in the message port to exit. This was due to the fact that `CancelHandle`s are never reset (i.e., once you `cancel` a `CancelHandle`, it remains cancelled). That meant that after `receieveMessageOnPort` was called, the subsequent calls to `op_message_port_recv_message` would throw `Interrupted` exceptions, and we would exit the loop. The cancellation, however, isn't actually necessary. `op_message_port_recv_message` only borrows the underlying port for long enough to poll the receiver, so the borrow there could never overlap with `op_message_port_recv_message_sync`. - Calling `MessagePort.unref()` caused the "receive loop" in the message port to exit. This was because we were setting `messageEventListenerCount` to 0 on unref. Not only does that break the counter when multiple `MessagePort`s are present in the same thread, but we also exited the "receive loop" whenever the listener count was 0. I assume this was to prevent the recv promise from keeping the event loop open. Instead of this, I chose to just unref the recv promise as needed to control the event loop. - The last bug causing the hang (which was a doozy to debug) ended up being an unfortunate interaction between how we implement our messageport "receive loop" and a pattern found in `npm:piscina` (which angular uses). The gist of it is that piscina uses an atomic wait loop along with `receiveMessageOnPort` in its worker threads, and as the worker is getting started, the following incredibly convoluted series of events occurs: 1. Parent sends a MessagePort `p` to worker 2. Parent sends a message `m` to the port `p` 3. Parent notifies the worker with `Atomics.notify` that a new message is available 4. Worker receives message, adds "message" listener to port `p` 5. Adding the listener triggers `MessagePort.start()` on `p` 6. Receive loop in MessagePort.start receives the message `m`, but then hits an await point and yields (before dispatching the "message" event) 7. Worker continues execution, starts the atomic wait loop, and immediately receives the existing notification from the parent that a message is available 8. Worker attempts to receive the new message `m` with `receiveMessageOnPort`, but this returns `undefined` because the receive loop already took the message in 6 9. Atomic wait loop continues to next iteration, waiting for the next message with `Atomic.wait` 10. `Atomic.wait` blocks the worker thread, which prevents the receive loop from continuing and dispatching the "message" event for the received message 11. The parent waits for the worker to respond to the first message, and waits 12. The thread can't make any more progress, and the whole process hangs The fix I've chosen here (which I don't particularly love, but it works) is to just delay the `MessagePort.start` call until the end of the event loop turn, so that the atomic wait loop receives the message first. This prevents the hang. --- Those were the main issues causing the hang. There ended up being a few other small bugs as well, namely `exit` being emitted multiple times, and not patching up the message port when it's received by `receiveMessageOnPort`.
2024-08-28fix(ext/node): emit `online` event after worker thread is initialized (#25243)Nathan Whitaker
Fixes #23281. Part of #20613. We were emitting the `online` event in the constructor, so the caller could never receive it (since there was no time for them to add a listener). Instead, emit the event where it's intended – after the worker is initialized. --- After this parcel no longer freezes, but still will fail due to other bugs (which will be fixed in other PRs)
2024-07-25chore: update to `std@2024.07.19` (#24715)Asher Gomez
2024-05-20fix(node): patch MessagePort in worker_thread message (#23871)Marvin Hagemeister
Our `MessagePort` to Node's `MessagePort` conversion logic was missing the case where a `MessagePort` is sent _inside_ the message. This broke `tinypool` which is used by `vitest` as it relies on some node specific methods on `MessagePort`. Fixes https://github.com/denoland/deno/issues/23854 , Fixes https://github.com/denoland/deno/pull/23871
2024-05-15fix(node): wrong `worker_threads.terminate()` return value (#23803)Marvin Hagemeister
<!-- Before submitting a PR, please read https://docs.deno.com/runtime/manual/references/contributing 1. Give the PR a descriptive title. Examples of good title: - fix(std/http): Fix race condition in server - docs(console): Update docstrings - feat(doc): Handle nested reexports Examples of bad title: - fix #7123 - update docs - fix bugs 2. Ensure there is a related issue and it is referenced in the PR text. 3. Ensure there are tests that cover the changes. 4. Ensure `cargo test` passes. 5. Ensure `./tools/format.js` passes without changing files. 6. Ensure `./tools/lint.js` passes. 7. Open as a draft PR if your work is still in progress. The CI won't run all steps, but you can add '[ci]' to a commit message to force it to. 8. If you would like to run the benchmarks on the CI, add the 'ci-bench' label. --> Fixes https://github.com/denoland/deno/issues/23801 --------- Signed-off-by: Marvin Hagemeister <marvinhagemeister50@gmail.com> Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
2024-04-30fix(ext/node): support multiple message listeners on MessagePort (#23600)Satya Rohith
Closes https://github.com/denoland/deno/issues/23561
2024-04-24fix(ext/node): worker_threads copies env object (#23536)Bartek Iwańczuk
Most common argument to `env` option for `worker_threads.Worker` will be `process.env`. In Deno `process.env` is a `Proxy` which can't be cloned using structured clone algorithm. So to be safe, I'm creating a copy of actual object before it's sent to the worker thread. Ref #23522
2024-04-20fix(ext/node): Support `env` option in worker_thread (#23462)Divy Srivastava
Fixes https://github.com/denoland/deno/issues/23455
2024-04-16fix(ext/node): worker_threads.receiveMessageOnPort doesn't panic (#23406)Bartek Iwańczuk
Follow up to https://github.com/denoland/deno/pull/23386. Instead of using async `recv()` method, it was replaced with a poll based function that doesn't hold onto RefCell borrow across await point. Fixes https://github.com/denoland/deno/issues/23362
2024-04-16fix(ext/node): panic on 'worker_threads.receiveMessageOnPort' (#23386)Bartek Iwańczuk
Closes https://github.com/denoland/deno/issues/23362 Previously we were panicking if there was a pending read on a port and `receiveMessageOnPort` was called. This is now fixed by cancelling the pending read, trying to read a message and resuming reading in a loop.
2024-04-02fix(ext/node): MessagePort works (#22999)Satya Rohith
Closes https://github.com/denoland/deno/issues/22951 Closes https://github.com/denoland/deno/issues/23001 Co-authored-by: Divy Srivastava <dj.srivastava23@gmail.com>
2024-04-02chore: update `std` submodule to 0.221.0 (#23112)Asher Gomez
2024-03-20fix(ext/node): worker_threads ESM handling (#22841)mash-graz
Fixes #22840 Fixes #22964
2024-03-16fix(ext/node): support MessagePort in `WorkerOptions.workerData` (#22950)Bartek Iwańczuk
This commit fixes passing `MessagePort` instances to `WorkerOptions.workerData`. Before they were not serialized and deserialized properly when spawning a worker thread. Closes https://github.com/denoland/deno/issues/22935
2024-03-11test(ext/node): add worker_threads test for SharedArrayBuffer (#22850)Bartek Iwańczuk
Follow up to https://github.com/denoland/deno/pull/22815
2024-03-07feat(ext/node): ref/unref on workers (#22778)Matt Mastracci
Implements ref/unref on worker to fix part of #22629
2024-02-13chore: use `@std` import instead of `@test_util/std` (#22398)Asher Gomez
This PR: 1. Replaces `@test_util/std`-prefixed imports with `@std`. 2. Adds `@std/` import map entries to a few `deno.json` files.
2024-02-10chore: move cli/tests/ -> tests/ (#22369)Matt Mastracci
This looks like a massive PR, but it's only a move from cli/tests -> tests, and updates of relative paths for files. This is the first step towards aggregate all of the integration test files under tests/, which will lead to a set of integration tests that can run without the CLI binary being built. While we could leave these tests under `cli`, it would require us to keep a more complex directory structure for the various test runners. In addition, we have a lot of complexity to ignore various test files in the `cli` project itself (cargo publish exclusion rules, autotests = false, etc). And finally, the `tests/` folder will eventually house the `test_ffi`, `test_napi` and other testing code, reducing the size of the root repo directory. For easier review, the extremely large and noisy "move" is in the first commit (with no changes -- just a move), while the remainder of the changes to actual files is in the second commit.