Skip to content

fix(js): stop thrown errors from aborting core or outliving the deadline - #343

Draft
oglassdev wants to merge 3 commits into
mainfrom
fix/js-v8-error-hardening
Draft

oglassdev wants to merge 3 commits into
mainfrom
fix/js-v8-error-hardening

Conversation

@oglassdev

Copy link
Copy Markdown
Contributor

Closes #300. #341 left two ways for application JavaScript to take down core while it converts a thrown error. Throwing an error whose errorAdditionalPropertyKeys is a huge array still aborted the process. An error with a looping message getter still kept running past the invocation deadline. Both happened because deno_core converts thrown values by running their JavaScript.

Now chunk-js formats every application error itself, and no application JavaScript runs while it does:

  • One reporting path. Invocation throws, queueMicrotask errors, unhandled rejections and top-level module errors all go through one op. It reads only V8's own message and captured frames, and copies at most 16 KiB out of V8 in total.
  • Terminations dispatch a plain value first. A deadline, cancellation or heap limit, or the report op itself, dispatches undefined before terminating, so deno_core only ever converts undefined. The watchdog and heap callback do this through a V8 interrupt in src/isolate.rs.
  • The watchdog keeps interrupting a terminated run every 2 ms until it ends, because deno_core cancels termination while it converts an error.
  • A failed call is settled before its runtime is kept. Its leftover microtasks and rejections are drained inside the deadline guard, so they can't surface in the next call. A call that was terminated is discarded as before.

Behaviour changes

  • Error text is V8's message plus up to 10 frames, capped at 16 KiB, and the bootstrap wrapper's frames now appear in it. The existing stack-format test still passes.
  • Running out of allocator memory now stops the run within about 2 ms. Before, it let the call run to its end before failing with the heap error.

Checks

  • cargo test -p chunk-js: 32 passed, plus the compile-fail doctest.
  • cargo clippy -p chunk-js --all-targets -- -D warnings and cargo fmt -p chunk-js --check are clean.
  • The new tests were run against the old runtime: the oversized-keys case aborts the process and the deadline case hangs for over 60 s.
  • Three review rounds. The last one found nothing.
  • The 100–200 ms deadline tests can fail under heavy CPU oversubscription. They fail at the same rate on main: 9 of 50 runs on each with 16 processes pinned to 2 CPUs.

🤖 Generated with Claude Code

…deadline

deno_core converted application-controlled values into JsError, and that conversion could
abort the process or run forever:

- A thrown error's errorAdditionalPropertyKeys array sized a Vec, so a huge length aborted.
- The conversion cancelled termination and then ran getters, so a looping message, name or
  cause getter outlived the deadline. Its fresh "execution terminated" error inherited both
  problems from Error.prototype.

Application errors now reach the host only through op_chunk_report, which formats them from
V8's message and captured frames without running JavaScript. Bootstrap installs it as the
report-exception and unhandled-rejection hooks and catches every invocation's error. Every
termination, whether from the report, the deadline, cancellation or the heap limit, first
dispatches undefined, so deno_core only ever converts that. The watchdog keeps interrupting
a terminated run until it ends, since deno_core cancels termination while converting.

Refs #300.
…error

describe converted whole message, function and script strings into Rust before truncating, so a
multi-megabyte message was copied in full and kept its capacity. Each string now copies at most the
remaining byte budget, and frames stop once it is used.

Adds tests that a failed invocation leaves nothing for the next call, and that reading error.stack
in a function that then hits its deadline still ends on time.

Refs #300
…ing its runtime

A rejection created in a microtask of a failed transaction invocation
stayed pending and failed the next call on the retained runtime. The
failed call now drains its event loop under the deadline guard, keeping
its first reported error, and skips the drain once a termination has been
recorded since that runtime is discarded.

Refs #300

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

chunk-js: URL.parse reentrancy and stack call-site caching can abort the process

1 participant