Skip to content

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

Open

Description

@bcardarella

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

zone.js

Is this a regression?

No

Description

zone.js forwards an unhandled ZoneAwarePromise rejection to unhandledrejection listeners registered on window through findPromiseRejectionHandler in packages/zone.js/lib/browser/browser.ts (zone.js 0.16.3, as in the Angular v22.2.6 tree):

// lib/browser/browser.ts, findPromiseRejectionHandler (line 313)
const evt = new PromiseRejectionEvent(evtName, {
  promise: e.promise,
  reason: e.rejection,
});
eventTask.invoke(evt);

A listener added to window with the patched addEventListener is an event task created with useG: true (OPTIMIZED_ZONE_EVENT_TASK_DATA, lib/common/events.ts line 84), and for such a task the ZoneTask constructor does not make a closure:

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

ZoneTask.invokeTask is the static invokeTask(task, target, args) (line 1426), so eventTask.invoke(evt) passes the event as the task: task.zone.runTask(...) throws a TypeError because evt.zone is undefined. handleUnhandledRejection (lib/common/promise.ts line 82) swallows the error with catch (err) {}, so the listener is never called and nothing is reported.

zone.js's own global event callback calls the same function with the task first: task.invoke(task, target, [event]) (lib/common/events.ts line 239). Calling it that way in findPromiseRejectionHandler, eventTask.invoke(eventTask, global, [evt]), looks like the fix for useG tasks. That is a suggestion only; we have not tested it against the suite.

Why the zone.js test still passes in Chrome and Safari

packages/zone.js/test/common/zone.spec.ts, "should surface unhandled promise rejections via unhandledrejection event (if the flag is enabled)", passes in Chrome and Safari for a different reason. In those two browsers PromiseRejectionEventInit.promise is still typed Promise<any> (Blink third_party/blink/renderer/core/events/promise_rejection_event_init.idl; WebKit Source/WebCore/dom/PromiseRejectionEvent.idl). So new PromiseRejectionEvent(..., {promise: zoneAwarePromise}) resolves the ZoneAwarePromise thenable into a new native promise, which rejects with the same reason, and nothing handles it. The browser itself then fires a native unhandledrejection with that native promise, and the listener receives that event. The An error was thrown in a Promise outside a test line in Chrome's test log is jasmine's handler seeing the same native rejection.

The HTML Standard changed the member to required object promise in whatwg/html#9897 (merged 2023-12-12). Firefox ships that (dom/webidl/PromiseRejectionEvent.webidl), so no native promise is made and the listener is never called. whatwg/html#11547 records that Chrome and Safari still differ from the spec here. As soon as a browser follows the current spec, the test fails there, and zone.js applications never receive unhandledrejection for ZoneAwarePromise rejections. That happens in Firefox today, and in any browser engine that follows the current spec, as Firefox does.

Related issues

#51357 and #55590 report that new PromiseRejectionEvent(...) in the same function throws when e.promise is undefined, which happens when DISABLE_WRAPPING_UNCAUGHT_PROMISE_REJECTION is true (the default since #52492). This report is the failure behind that one: with the flag false, as zone.js's own browser tests set it, the event constructs, and dispatch then fails at eventTask.invoke(evt). Fixing the constructor's input alone will not make listeners fire.

Expected: zone.js calls the listener with the event it built (its promise is the ZoneAwarePromise, reason is 'Error happened :(') in every browser, whatever the browser's PromiseRejectionEventInit type.

Please provide a link to a minimal reproduction of the bug

Self-contained page below (no link needed).

Serve this page next to zone.umd.js (zone.js 0.16.3, bundles/zone.umd.js) from any static HTTP server and open it. The first script sets the same flag the zone.js browser tests set (test/browser-zone-setup.ts).

<!doctype html><body>
<script>window.__zone_symbol__DISABLE_WRAPPING_UNCAUGHT_PROMISE_REJECTION = false;</script>
<script src="zone.umd.js"></script>
<script>
const log = (...a) => console.log(a.map(String).join(' '));
window.onerror = () => {};
const sym = Zone.__symbol__('unhandledPromiseRejectionHandler');
const orig = Zone[sym];
// Only records whether zone.js's own dispatch throws; it constructs no event itself.
Zone[sym] = function (e) {
  try { orig.call(this, e); log('zone dispatch returned'); } catch (err) { log('zone dispatch threw: ' + err); }
};
window[Zone.__symbol__('enable_native_microtask_draining')] = true;
let calls = 0;
Zone.current.fork({name: 'probe'}).run(() => {
  window.addEventListener('unhandledrejection', (e) => {
    calls++;
    log('listener call ' + calls + ': reason=' + e.reason +
        ' native promise=' + (e.promise instanceof window[Zone.__symbol__('Promise')]));
  });
  const s = document.createElement('script');
  s.innerHTML = "Promise.reject('Error happened :(')";
  document.body.append(s);
});
setTimeout(() => log('result: listener calls=' + calls), 100);
</script>

The wrapper calls the original handler inside a try, so the TypeError that zone.js swallows shows up in the console.

Results (2026-11-22)

Browser zone.js's dispatch listener calls
Chrome for Testing 157 TypeError: Cannot read properties of undefined (reading 'runTask') 1: the event's promise is a native promise, not the ZoneAwarePromise
Safari 27.0 TypeError: undefined is not an object (evaluating 'task.zone.runTask') 1: the event's promise is a native promise
Firefox 159.0 TypeError: can't access property "runTask", task.zone is undefined 0

zone.js's own dispatch throws in all three browsers. Chrome and Safari call the listener only through the native rejection that their Promise<any> conversion creates.

Please provide the exception or error you saw

Chrome 157:  TypeError: Cannot read properties of undefined (reading 'runTask')
Safari 27:   TypeError: undefined is not an object (evaluating 'task.zone.runTask')
Firefox 159: TypeError: can't access property "runTask", task.zone is undefined

The error is swallowed by the catch (err) {} in handleUnhandledRejection (lib/common/promise.ts line 82).

Please provide the environment you discovered this bug in (run ng version)

zone.js 0.16.3 (Angular v22.2.6 repository, `packages/zone.js`)
Chrome for Testing 157, Safari 27.0, Firefox 159.0 on macOS

Anything else?

whatwg/html#9897 (merged 2023-12-12) changed PromiseRejectionEventInit.promise to required object; whatwg/html#11547 tracks Chrome and Safari still converting it to a native promise.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: zones

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions