Skip to content

[JSC] Hand Bun__reportUnhandledError the async context the failed queueMicrotask job ran in - #685

Merged
dylan-conway merged 4 commits into
mainfrom
claude/report-unhandled-error-async-context
Sep 17, 2026
Merged

dylan-conway merged 4 commits into
mainfrom
claude/report-unhandled-error-async-context

Conversation

@dylan-conway

@dylan-conway dylan-conway commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

What

The job that runs a queueMicrotask() callback (BunPerformMicrotaskJob in JSMicrotask.cpp) enters the callback's async context, calls it, restores the previous context, and only then reports an exception the callback threw:

AsyncContextSwapScope asyncContextScope(vm, globalObject, arguments[1]);
callMicrotask(...);
asyncContextScope.restoreEarly();
if (auto* exception = catchScope.exception())
    Bun__reportUnhandledError(globalObject, JSValue::encode(exception));

By the time Bun's hook runs, the global object's async context is the restored one, so the embedder has no way to tell which context the error happened in.

This reads the live async context just before it is restored, when the callback threw, and passes it to the hook as a third argument. It is the context current at the throw: the one the job was entered with, or whatever AsyncLocalStorage.enterWith() moved it to during the callback.

What does not change

The context is still restored before reporting, in the same order as before, and the path without an exception does no extra work. Only the hook's argument list grows. An embedder whose Bun__reportUnhandledError takes two arguments keeps working: the extra argument is ignored.

Why

Bun attributes an uncaught error to the async context it happened in. For every other way into JavaScript the report happens while that context is still current. This is the one site where it has already been put back.

…ueMicrotask job ran in

The job that runs a queueMicrotask() callback enters the callback's async
context, calls it, restores the previous context, and only then reports an
exception the callback threw. By the time Bun's hook is called the global
object's async context is the restored one, so the embedder cannot tell which
context the error happened in.

The job still has that value: it is the argument it entered. Pass it to the
hook. Nothing else changes: the context is restored before reporting, as
before. An embedder whose hook takes two arguments is unaffected.
@coderabbitai

coderabbitai Bot commented Sep 17, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack


Comment @coderabbitai help to get the list of available commands.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Beyond the inline finding, I also checked that the termination-exception path is unchanged (still returns before the hook, with the context already restored), that asyncContext stays valid until the call (a stack-held JSValue copied from the job's own argument slot, conservatively scanned), and that an embedder still defining the two-argument hook is unaffected on every targeted ABI (the extra argument is passed in a register or stack slot the callee never reads). The Bun-side definition needs to be updated in the same release for the new argument to be used.

Extended reasoning...

The change is a three-line signature and call-site extension of a weak extern "C" embedder hook inside a USE(BUN_JSC_ADDITIONS) block. The inline finding (the passed value is the context the job entered, not whatever enterWith() may have installed during the callback) is the substantive question a human should weigh; the additional items above were examined and ruled out from reading the diff, AsyncContextSwapScope.h, and the linker flag references in Source/cmake/WebKitCompilerFlags.cmake. No security-sensitive surface is involved.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two newer commits address the earlier inline note: the hook now receives the context read via AsyncContextSwapScope::current() before restoreEarly(), so an enterWith() inside the callback is reflected. I re-reviewed the updated diff and found no bugs; since the third parameter is a cross-repo ABI contract with Bun's definition of Bun__reportUnhandledError, a human look at the lockstep landing on the Bun side is still worthwhile.

What was reviewed:

  • Ordering around restoreEarly(): catchScope.exception() and current() are read first; neither restoreEarly() (a putInternalField) nor clearExceptionExceptTermination() can throw or allocate, so the captured JSValue on the stack is not invalidated.
  • current() under a pending exception: it only reads field 0 (or returns jsUndefined() when tracking is off), so it cannot trip exception-scope checks; the encoded value is never empty on the path that calls the hook.
  • Weak-symbol call with an extra argument: harmless for a 2-argument definition on the targeted ABIs; this file is the only declaration and call site in the repo.
Extended reasoning...

Overview

The PR touches one file, Source/JavaScriptCore/runtime/JSMicrotask.cpp, inside the USE(BUN_JSC_ADDITIONS) BunPerformMicrotaskJob case of runInternalMicrotask(). The weak extern "C" hook Bun__reportUnhandledError gains a third EncodedJSValue asyncContext parameter, and the job now reads the pending exception and AsyncContextSwapScope::current(vm, globalObject) before asyncContextScope.restoreEarly(), then passes that captured value to the hook after the restore. The two commits pushed after my earlier review (c8925fd, 2a98017) changed the value passed from arguments[1] (the entered context) to the live context at the throw, which is exactly what the earlier inline note asked for; the author also replied on that thread.

Security risks

None specific to this change. The code path reports an already-caught exception to the embedder; it does not touch authentication, permissions, or untrusted input parsing. The one cross-boundary concern is the ABI change: a Bun build compiled against the old two-argument declaration continues to work (the extra argument is ignored on the x64 SysV, Windows x64 and arm64 calling conventions used by the lanes), while a Bun build reading the third argument against an older WebKit would read an unspecified register/stack slot — an ordering issue for the Bun-side landing, not a vulnerability in this repository.

Level of scrutiny

Moderate. The diff is small and mechanical, and I verified the properties that matter: restoreEarly() is a single putInternalField with no allocation or throw, clearExceptionExceptTermination() does not allocate, current() only reads an internal field (or returns jsUndefined() when tracking is disabled) and therefore is safe to call while an exception is pending and never yields an empty value on the reporting path, the TerminationException early return is unchanged, and the non-exception path does no additional work. However, the file falls under /Source/JavaScriptCore in .github/CODEOWNERS (upstream's file, but it covers the path), and the change is one half of a two-repository contract, so approving on behalf of a human is not appropriate.

Other factors

The bug hunt ran dry (dry_streak) with no findings, and no candidate issues were ruled out beyond what I checked by hand. There is no test in this repository that exercises the hook (it is only observable from Bun), so correctness of the attribution ultimately depends on Bun's updated definition and its own tests. The remaining Bun__reportUnhandledError references in Source/cmake/WebKitCompilerFlags.cmake are linker -u/-U symbol flags and are unaffected by the signature change.

@dylan-conway
dylan-conway merged commit 2eb832d into main Sep 17, 2026
48 checks passed
@github-actions

Copy link
Copy Markdown

Preview build of 2a98017: autobuild-preview-pr-685-2a980172

dylan-conway added a commit to oven-sh/bun that referenced this pull request Sep 17, 2026
Whose an uncaught exception or unhandled rejection is was worked out from
three sources in turn: the functions on the stack (by their scope chain), then
the last exception the VM saw if its value matched, then the current async
context. It is the current async context alone now, the same thing that
decides who owns what the code opens. node:module's `new Module()` and
`createRequire()` ask it too.

- A rejection that native code reports itself (`unhandled_rejection`: a
  Bun.cron() tick's, a module's that failed to load, a macro's) had no owner at
  all; it is the current context's, like an uncaught exception.
- `onError` runs in the context the graph was made in, for every kind of
  error: it ran in the graph's own for exceptions and in the host's for
  rejections. What it throws, rejects or starts is its maker's, so the flag
  that kept a graph's errors from its own running handler is gone.
- JavaScriptCore's queueMicrotask job reports an exception after it has
  restored the async context. It hands the context that was current at the
  throw to `Bun__reportUnhandledError` (oven-sh/WebKit#685), which goes on to
  attribution. Bun's own callers of that hook report from inside the context
  and pass the live one.

A function of a graph's that the host calls directly runs in the host's
context, so what it throws later is the host's, as what it opens already was;
`run()` puts a call in the graph's. The promise `graph.import()` returns is its
caller's.
Jarred-Sumner added a commit to oven-sh/bun that referenced this pull request Sep 17, 2026
Bumps `WEBKIT_VERSION` from `c28156899e5f` to `000c48997255` (current
oven-sh/WebKit `main`). The
`autobuild-000c48997255272cd7e3ae7047e87b955b38989b` release exists.

oven-sh/WebKit changes picked up:

- [JSC] Count an adopted typed array vector once, and stop counting the
ArrayBuffers an eden collection freed (oven-sh/WebKit#684)
- [JSC] Run a CommonJS module's generator before its importing graph
links, not when its fetch completes (oven-sh/WebKit#662)
- SourceCodeKey: restore source string comparison in operator==
(oven-sh/WebKit#346)
- [JSC] Hand Bun__reportUnhandledError the async context the failed
queueMicrotask job ran in (oven-sh/WebKit#685)
- [JSC] AbstractModuleRecord::link: one throw scope for the whole
function (oven-sh/WebKit#688)
- [JSC] An Exception remembers the async context it was thrown in
(oven-sh/WebKit#689)

Not built or tested locally; relying on CI.
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.

1 participant