Repository navigation
Conversation
`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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Type
Bug fix
Which @angular/* package(s) are the source of the bug?
zone.js
Description
findPromiseRejectionHandlerinpackages/zone.js/lib/browser/browser.tsbuilds thePromiseRejectionEventfor an unhandledZoneAwarePromiserejection and then callseventTask.invoke(evt).For event tasks scheduled with
useG, which is whataddEventListeneruses by default (OPTIMIZED_ZONE_EVENT_TASK_DATAinlib/common/events.ts), theZoneTaskconstructor does not create a per-taskinvokeclosure.this.invokeis the staticZoneTask.invokeTask(task, target, args):Calling it with the event alone therefore passes the event where the task belongs,
task.zone.runTask(...)readszoneoff the event, and throws.handleUnhandledRejectionwraps that call incatch (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:
This change calls
invokethe same way infindPromiseRejectionHandler, so the task reachesrunTaskin both modes.rejectionhandledgoes 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.promiseasPromise<any>, so constructing the event with aZoneAwarePromiseproduces 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:TypeError: Cannot read properties of undefined (reading 'runTask'))The four tests added to
test/browser/browser.spec.tscover:unhandledrejectionlisteners receive the constructed event, with the original promise and rejection reason.rejectionhandledgoes through the same path and works.To check that the change does not alter unrelated event behaviour, the surrounding
eventListener hookssuite 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 withoutuseGkeep working: the staticinvokeTaskstarts withif (!task) { task = this; }, and for those tasksinvokeis a closure that already ignores its arguments and callsinvokeTask.call(global, self, this, arguments). Event tasks bound withuseG: false(node/events.ts,extra/socket-io.ts,webapis-rtc-peer-connection.ts) therefore behave as before.Checklist
invoketakes the task)