1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00
denoland-deno/tests/unit_node
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
..
_fs feat: TypeScript 5.6 and npm:@types/node@22 (#25614) 2024-09-14 11:58:47 +01:00
crypto fix(ext/node): support x509 certificates in createPublicKey (#25731) 2024-09-19 19:12:23 +05:30
internal chore: update to std@2024.07.19 (#24715) 2024-07-25 15:30:28 +10:00
testdata fix(ext/crypto): support md4 digest algorithm (#25656) 2024-09-16 11:04:40 +00:00
_test_utils.ts test: remove usage of --unstable flag (#25549) 2024-09-10 11:28:59 +00:00
assert_test.ts fix(node/assert): throws not checking error instance (#24466) 2024-07-08 21:28:39 +02:00
assertion_error_test.ts chore: update to std@2024.07.19 (#24715) 2024-07-25 15:30:28 +10:00
async_hooks_test.ts fix: add test ensuring als works across dynamic import (#25593) 2024-09-12 05:42:26 +00:00
buffer_test.ts chore: update to std@2024.07.19 (#24715) 2024-07-25 15:30:28 +10:00
child_process_test.ts feat: TypeScript 5.6 and npm:@types/node@22 (#25614) 2024-09-14 11:58:47 +01:00
cluster_test.ts fix(node/cluster): improve stubs to make log4js work (#25146) 2024-08-26 11:19:58 +02:00
console_test.ts chore: update to std@2024.07.19 (#24715) 2024-07-25 15:30:28 +10:00
dgram_test.ts chore: update to std@2024.07.19 (#24715) 2024-07-25 15:30:28 +10:00
domain_test.ts chore: update to std@2024.07.19 (#24715) 2024-07-25 15:30:28 +10:00
events_test.ts feat: TypeScript 5.6 and npm:@types/node@22 (#25614) 2024-09-14 11:58:47 +01:00
fs_test.ts feat(cli): use NotCapable error for permission errors (#25431) 2024-09-10 11:12:24 -07:00
http2_test.ts BREAKING(ext/net): improved error code accuracy (#25383) 2024-09-27 14:07:20 +00:00
http_test.ts fix(ext/node): close upgraded socket when the underlying http connection is closed (#25387) 2024-09-05 13:30:18 +09:00
inspector_test.ts fix(node/inspector): Session constructor should not throw (#25041) 2024-08-14 13:34:24 +00:00
module_test.ts feat(check): turn on useUnknownInCatchVariables (#25465) 2024-09-05 15:22:31 +02:00
net_test.ts feat(cli): use NotCapable error for permission errors (#25431) 2024-09-10 11:12:24 -07:00
os_test.ts feat(cli): use NotCapable error for permission errors (#25431) 2024-09-10 11:12:24 -07:00
path_test.ts chore: update to std@2024.07.19 (#24715) 2024-07-25 15:30:28 +10:00
perf_hooks_test.ts chore: update to std@2024.07.19 (#24715) 2024-07-25 15:30:28 +10:00
process_test.ts fix(ext/node): don't throw error for unsupported signal binding on windows (#25699) 2024-09-19 12:22:01 +09:00
punycode_test.ts chore: update to std@2024.07.19 (#24715) 2024-07-25 15:30:28 +10:00
querystring_test.ts chore: update to std@2024.07.19 (#24715) 2024-07-25 15:30:28 +10:00
readline_test.ts chore: update to std@2024.07.19 (#24715) 2024-07-25 15:30:28 +10:00
repl_test.ts chore: update to std@2024.07.19 (#24715) 2024-07-25 15:30:28 +10:00
stream_test.ts feat: TypeScript 5.6 and npm:@types/node@22 (#25614) 2024-09-14 11:58:47 +01:00
string_decoder_test.ts chore: update to std@2024.07.19 (#24715) 2024-07-25 15:30:28 +10:00
timers_test.ts fix(node/timers/promises): add scheduler APIs (#24802) 2024-07-31 12:00:01 +02:00
tls_test.ts BREAKING(ext/net): improved error code accuracy (#25383) 2024-09-27 14:07:20 +00:00
tty_test.ts BREAKING(fs): remove Deno.FsFile.prototype.rid (#25499) 2024-09-11 07:19:34 +10:00
util_test.ts feat(ext/node): add abort helpers, process & streams fix (#25262) 2024-09-05 09:22:52 +02:00
v8_test.ts chore: update to std@2024.07.19 (#24715) 2024-07-25 15:30:28 +10:00
vm_test.ts feat: vm rewrite (#24596) 2024-08-06 12:52:53 +00:00
wasi_test.ts fix(ext/node): register node:wasi built-in (#25134) 2024-08-22 09:03:52 +05:30
worker_threads_test.ts fix(node): fix worker_threads issues blocking Angular support (#26024) 2024-10-04 09:26:32 -07:00
zlib_test.ts fix(ext/node): createBrotliCompress params (#24984) 2024-08-11 14:26:30 +05:30