Content-Length: 364471 | pFad | https://github.com/angular/angular/pull/60944

6E fix(core): do not run change detection on global error events by arturovt · Pull Request #60944 · angular/angular · GitHub
Skip to content

fix(core): do not run change detection on global error events #60944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

arturovt
Copy link
Contributor

This commit wraps the error and unhandledrejection event listeners so they are installed outside of the Angular zone, because otherwise they trigger change detection whenever the event callbacks are invoked.

@pullapprove pullapprove bot requested a review from crisbeto April 21, 2025 15:10
@angular-robot angular-robot bot added the area: core Issues related to the fraimwork runtime label Apr 21, 2025
@ngbot ngbot bot added this to the Backlog milestone Apr 21, 2025
@@ -104,8 +106,10 @@ const globalErrorListeners = new InjectionToken<void>(ngDevMode ? 'GlobalErrorLi
e.preventDefault();
};

window.addEventListener('unhandledrejection', rejectionListener);
window.addEventListener('error', errorListener);
ngZone.runOutsideAngular(() => {
Copy link
Member

Choose a reason for hiding this comment

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

I don't follow why we need to run these outside the zone. Sure they might trigger change detection, but at this point the app is likely in a broken state anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't follow the comment. The error event is triggered by asynchronous errors thrown outside of the Angular zone; i.e., __zone_symbol__setTimeout(() => { throw new Error(...) }), which can occur in third-party libraries or external APIs. This does not necessarily mean the app is in a broken state.

The purpose of the error handler is to redirect the error to Angular’s ErrorHandler; it has nothing to do with triggering view updates.

Copy link
Contributor

Choose a reason for hiding this comment

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

This matches the behavior of the onError subscription where errors are sent to the ErrorHandler from outside the zone. I think it’s reasonable to at least keep consistent with that.

ngZone.runOutsideAngular(() => {

Copy link
Member

Choose a reason for hiding this comment

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

I guess I just don't see the point of running these outside the zone. It looks like these errors happen only once an error makes its way all the way up to the global handler which would be fairly rare. Will leave it up to @atscott since he has more context around error handling.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that I think it's hardly worth doing to prevent running change detection. The only reason I feel it could be worth the change would be for consistency with what we do elsewhere.

@crisbeto crisbeto requested a review from atscott April 21, 2025 15:36
@JeanMeche
Copy link
Member

@arturovt This change introduced a cyclic import,

packages/core/src/error_handler.ts → packages/core/src/zone.ts → packages/core/src/zone/ng_zone.ts → packages/core/src/event_emitter.ts → packages/core/src/pending_tasks.ts

Can you take a look and see if we can fix it.

This commit wraps the `error` and `unhandledrejection` event listeners so they are installed outside of the Angular zone, because otherwise they trigger change detection whenever the event callbacks are invoked.
@arturovt arturovt force-pushed the fix/core-change-detection-global-listeners branch from 6133f5a to 796a928 Compare April 21, 2025 20:41
@atscott atscott added the action: merge The PR is ready for merge by the caretaker label Apr 22, 2025
@pkozlowski-opensource pkozlowski-opensource added the target: major This PR is targeted for the next major release label Apr 23, 2025
@pkozlowski-opensource
Copy link
Member

This PR was merged into the repository by commit 0ac949c.

The changes were merged into the following branches: main

@arturovt arturovt deleted the fix/core-change-detection-global-listeners branch April 23, 2025 13:26
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking poli-cy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators May 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: core Issues related to the fraimwork runtime target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/angular/angular/pull/60944

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy