summaryrefslogtreecommitdiff
path: root/ext/web/message_port.rs
AgeCommit message (Collapse)Author
2024-10-17refactor(ext/web): use concrete error types (#26185)Leo Kettmeir
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-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-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-11fix(ext/node) implement receiveMessageOnPort for node:worker_threads (#22766)mash-graz
Implementation of `receiveMessageOnPort` for `node:worker_threads` Fixes: #22702
2024-01-01chore: update copyright to 2024 (#21753)David Sherret
2023-09-14refactor: rewrite more ops to op2 macro (#20478)Bartek Iwańczuk
2023-09-11chore: update inner #\![allow] to #[allow] (#20463)Matt Mastracci
Functions should generally be annotated with `#[allow]` blocks rather than using inner `#![allow]` annotations.
2023-01-14chore: use rustfmt imports_granularity option (#17421)Divy Srivastava
Closes https://github.com/denoland/deno/issues/2699 Closes https://github.com/denoland/deno/issues/2347 Uses unstable rustfmt features. Since dprint invokes `rustfmt` we do not need to switch the cargo toolchain to nightly. Do we care about formatting stability of our codebase across Rust versions? (I don't)
2023-01-13chore: add `copyright_checker` tool and add the missing copyright (#17285)Yiyu Lin
2022-07-11chore(web, worker): Use `DetachedBuffer` for `postMessage` ops (#15133)Andreu Botella
This commit uses `DetachedBuffer` instead of `ZeroCopyBuf` in the ops that back `Worker.prototype.postMessage` and `MessagePort.prototype.postMessage`. This is done because the serialized buffer is then copied to the destination isolate, even though it is internal to runtime code and not used for anything else, so detaching it and transferring it instead saves an unnecessary copy.
2022-05-13chore(runtime): Make some ops in ext and runtime infallible. (#14589)Andreu Botella
Co-authored-by: Aaron O'Mullan <aaron.omullan@gmail.com>
2022-03-14feat(ops): custom arity (#13949)Aaron O'Mullan
Also cleanup & drop ignored wildcard op-args
2022-03-14feat(core): codegen ops (#13861)Divy Srivastava
Co-authored-by: Aaron O'Mullan <aaron.omullan@gmail.com>
2021-11-03fix: Deno.emit crashes with BorrowMutError (#12627)Ryan Dahl
Warn on await_holding_refcell_ref clippy rule to avoid this in the future. Fixes #12453
2021-08-25feat: ArrayBuffer in structured clone transfer (#11840)Luca Casonato
2021-08-15refactor(ops): return BadResource errors in ResourceTable calls (#11710)Aaron O'Mullan
* refactor(ops): return BadResource errors in ResourceTable calls Instead of relying on callers to map Options to Results via `.ok_or_else(bad_resource_id)` at over 176 different call sites ...
2021-08-11Rename extensions/ directory to ext/ (#11643)Ryan Dahl