1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00
denoland-deno/runtime/js
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
..
01_errors.js feat(cli): use NotCapable error for permission errors (#25431) 2024-09-10 11:12:24 -07:00
01_version.ts refactor: use core.ensureFastOps() (#21888) 2024-01-10 15:37:25 -07:00
06_util.js refactor: use web utils and lazy load utils from core (#22289) 2024-02-06 22:28:32 +01:00
10_permissions.js refactor(runtime): align error messages (#25563) 2024-09-13 11:38:45 +02:00
11_workers.js fix(runtime): use more null proto objects (#23921) 2024-05-23 00:03:35 +02:00
30_os.js fix: validate integer values in Deno.exitCode setter (#24068) 2024-06-02 21:29:01 -04:00
40_fs_events.js chore: lint 40_fs_events.js (#25672) 2024-09-16 22:54:57 +00:00
40_process.js fix(node): Pass NPM_PROCESS_STATE to subprocesses via temp file instead of env var (#25896) 2024-09-27 12:35:37 -07:00
40_signals.js fix(runtime): use more null proto objects (#23921) 2024-05-23 00:03:35 +02:00
40_tty.js chore(tty): soft-remove Deno.isatty() (#25410) 2024-09-04 18:12:11 +10:00
41_prompt.js refactor: migrate extensions to virtual ops module (#22135) 2024-01-26 23:46:46 +01:00
90_deno_ns.js chore: cleanup unused deprecated code (#25839) 2024-09-26 02:21:38 +02:00
98_global_scope_shared.js refactor: cleanup unstable checks for WebGPU, FFI and FS APIs (#25586) 2024-09-12 12:27:16 +00:00
98_global_scope_window.js chore: cleanup remaining internals.future code (#25624) 2024-09-16 09:28:35 +10:00
98_global_scope_worker.js fix(runtime): use more null proto objects again (#25040) 2024-09-06 12:52:59 +02:00
99_main.js fix(node): fix worker_threads issues blocking Angular support (#26024) 2024-10-04 09:26:32 -07:00
README.md chore: fix outdated note in runtime/js/README.md (#23673) 2024-05-05 01:30:53 +00:00

Runtime JavaScript Code

This directory contains Deno runtime code written in plain JavaScript.

Each file is an ES module and is prefixed with a number, telling in which order scripts should be loaded into V8 isolate.

Deno Web APIs

This directory facilities Web APIs that are available in Deno.

Please note, that some implementations might not be completely aligned with specification.

Some Web APIs are using ops under the hood, eg. console, performance.

Implemented Web APIs