1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00
denoland-deno/runtime
Nathan Whitaker dd8cbf5e29
fix(node): fix worker_threads issues blocking Angular support (#26024)
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-10-04 09:26:32 -07:00
..
examples/extension refactor: bury descriptor parsing in PermissionsContainer (#25936) 2024-09-30 09:19:24 -04:00
js fix(node): fix worker_threads issues blocking Angular support (#26024) 2024-10-04 09:26:32 -07:00
ops refactor: bury descriptor parsing in PermissionsContainer (#25936) 2024-09-30 09:19:24 -04:00
permissions fix: don't prompt when using Deno.permissions.request with --no-prompt (#25811) 2024-10-03 12:28:38 +00:00
Cargo.toml chore: release deno_* crates (#25987) 2024-10-02 14:27:34 +00:00
clippy.toml feat(compile): unstable npm and node specifier support (#19005) 2023-05-10 20:06:59 -04:00
code_cache.rs fix: use hash of in-memory bytes only for code cache (#23966) 2024-05-24 10:15:46 -04:00
errors.rs Reland "refactor(fetch): reimplement fetch with hyper instead of reqwest" (#24593) 2024-07-18 01:37:31 +02:00
fmt_errors.rs feat: Add suggestion for packages using Node-API addons (#25975) 2024-10-01 21:49:32 +00:00
fs_util.rs refactor: use deno_path_util (#25918) 2024-09-28 07:55:01 -04:00
inspector_server.rs feat: upgrade deno_core (#25042) 2024-08-19 14:51:16 +00:00
js.rs refactor: remove snapshotting from deno_runtime (#21794) 2024-01-10 16:30:50 +01:00
lib.rs fix: better error for Deno.UnsafeWindowSurface, correct HttpClient name, cleanup unused code (#25833) 2024-09-24 07:04:52 -07:00
permissions.rs refactor: bury descriptor parsing in PermissionsContainer (#25936) 2024-09-30 09:19:24 -04:00
README.md fix (doc): Typo in runtime/README.md (#20020) 2023-12-13 17:24:32 +00:00
shared.rs BREAKING(buffer): remove Deno.Buffer (#25441) 2024-09-06 18:28:05 +10:00
snapshot.rs refactor: bury descriptor parsing in PermissionsContainer (#25936) 2024-09-30 09:19:24 -04:00
tokio_util.rs chore: enable clippy::print_stdout and clippy::print_stderr (#23732) 2024-05-08 22:45:06 -04:00
web_worker.rs refactor: bury descriptor parsing in PermissionsContainer (#25936) 2024-09-30 09:19:24 -04:00
worker.rs refactor: bury descriptor parsing in PermissionsContainer (#25936) 2024-09-30 09:19:24 -04:00
worker_bootstrap.rs BREAKING: Remove --unstable flag (#25522) 2024-09-09 23:44:29 +02:00

deno_runtime crate

crates docs

This is a slim version of the Deno CLI which removes typescript integration and various tooling (like lint and doc). Basically only JavaScript execution with Deno's operating system bindings (ops).

Stability

This crate is built using battle-tested modules that were originally in the deno crate, however the API of this crate is subject to rapid and breaking changes.

MainWorker

The main API of this crate is MainWorker. MainWorker is a structure encapsulating deno_core::JsRuntime with a set of ops used to implement Deno namespace.

When creating a MainWorker implementors must call MainWorker::bootstrap to prepare JS runtime for use.

MainWorker is highly configurable and allows to customize many of the runtime's properties:

  • module loading implementation
  • error formatting
  • support for source maps
  • support for V8 inspector and Chrome Devtools debugger
  • HTTP client user agent, CA certificate
  • random number generator seed

Worker Web API

deno_runtime comes with support for Worker Web API. The Worker API is implemented using WebWorker structure.

When creating a new instance of MainWorker implementors must provide a callback function that is used when creating a new instance of Worker.

All WebWorker instances are descendents of MainWorker which is responsible for setting up communication with child worker. Each WebWorker spawns a new OS thread that is dedicated solely to that worker.