Skip to content

fix(zone.js): dispatch the constructed event to rejection listeners - #72238

Open
Goldloli wants to merge 1 commit into
angular:mainfrom
Goldloli:fix/zonejs-dispatch-promise-rejection-event
Open

Goldloli wants to merge 1 commit into
angular:mainfrom
Goldloli:fix/zonejs-dispatch-promise-rejection-event

Conversation

@Goldloli

Copy link
Copy Markdown

PR Type

Bug fix

Which @angular/* package(s) are the source of the bug?

zone.js

Description

findPromiseRejectionHandler in packages/zone.js/lib/browser/browser.ts builds the PromiseRejectionEvent for an unhandled ZoneAwarePromise rejection and then calls eventTask.invoke(evt).

For event tasks scheduled with useG, which is what addEventListener uses by default (OPTIMIZED_ZONE_EVENT_TASK_DATA in lib/common/events.ts), the ZoneTask constructor does not create a per-task invoke closure. this.invoke is the static ZoneTask.invokeTask(task, target, args):

// lib/zone-impl.ts, ZoneTask constructor
if (type === eventTask && options && (options as any).useG) {
  this.invoke = ZoneTask.invokeTask;
}

Calling it with the event alone therefore passes the event where the task belongs, task.zone.runTask(...) reads zone off the event, and throws. handleUnhandledRejection wraps that call in catch (err) {}, so the listener is never invoked and nothing is reported.

zone.js' own global event callback already calls the same function with the task first:

// lib/common/events.ts, globalZoneAwareCallback
task.invoke(task, target, [event]);

This change calls invoke the same way in findPromiseRejectionHandler, so the task reaches runTask in both modes. rejectionhandled goes through the same helper and is fixed with it.

The bug is not visible in Chrome and Safari today only because those engines still type PromiseRejectionEventInit.promise as Promise<any>, so constructing the event with a ZoneAwarePromise produces a separate native rejection that the browser reports itself. Firefox ships the current spec (required object promise, see whatwg/html#9897), so no such rejection is produced and the listener is never called there. As soon as the other engines follow the spec, the same happens.

Fixes #72159

How was this verified

The dispatch path was exercised directly with the real handler, in a jsdom window that provides a spec-shaped PromiseRejectionEvent, so that zone.js takes the same branch as a real browser.

A/B on the same tests, changing only lib/browser/browser.ts:

bundle result
without the fix 0 of 4 new tests pass (TypeError: Cannot read properties of undefined (reading 'runTask'))
with the fix 4 of 4 new tests pass

The four tests added to test/browser/browser.spec.ts cover:

  • unhandledrejection listeners receive the constructed event, with the original promise and rejection reason.
  • rejectionhandled goes through the same path and works.
  • every listener registered for the event is reached.
  • the listener runs in the zone that registered it.

To check that the change does not alter unrelated event behaviour, the surrounding eventListener hooks suite was run against both bundles under the same runner: both produce the same 59 passes and the same 20 failures, and the failures are jsdom limitations of that runner (no real XHR/WebSocket/passive-event support), not differences caused by this change.

Prettier reports the two changed files as formatted.

Risk

The change only affects how zone.js dispatches its own PromiseRejectionEvent. Tasks created without useG keep working: the static invokeTask starts with if (!task) { task = this; }, and for those tasks invoke is a closure that already ignores its arguments and calls invokeTask.call(global, self, this, arguments). Event tasks bound with useG: false (node/events.ts, extra/socket-io.ts, webapis-rtc-peer-connection.ts) therefore behave as before.

Checklist

  • I have read the contributing guidelines
  • Documentation added (the inline comment explains why invoke takes the task)
  • Tests updated

`findPromiseRejectionHandler` built the `PromiseRejectionEvent` and then
called `eventTask.invoke(evt)`. Event tasks scheduled with `useG`, which is
the default for `addEventListener`, share the static
`ZoneTask.invokeTask(task, target, args)` instead of getting a per-task
closure, so `evt` was used as the task and `task.zone` was undefined.
`runTask` threw, and because `handleUnhandledRejection` swallows the error,
the listener was never called and nothing was reported.

Call `invoke` the way `globalZoneAwareCallback` already does, with the task,
the target and the event, so the task reaches `runTask` in both modes.
`rejectionhandled` uses the same helper and is fixed with it.

Add regression tests for the dispatch of both events, for multiple
listeners, and for the zone the listener runs in.

Fixes angular#72159
@pullapprove
pullapprove Bot requested a review from atscott November 27, 2026 14:22
@angular-robot angular-robot Bot added the area: zones Issues related to zone.js label Nov 27, 2026
@ngbot ngbot Bot added this to the Backlog milestone Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: zones Issues related to zone.js

Projects

None yet

Development

Successfully merging this pull request may close these issues.

zone.js: unhandledrejection listeners are never called by zone.js's own dispatch (eventTask.invoke(evt) passes the event as the task)

1 participant