Skip to content

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

Closed
arturovt wants to merge 1 commit intoangular:mainfrom
arturovt:fix/core-change-detection-global-listeners
Closed

fix(core): do not run change detection on global error events#60944
arturovt wants to merge 1 commit intoangular:mainfrom
arturovt:fix/core-change-detection-global-listeners

Conversation

@arturovt
Copy link
Copy Markdown
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 framework runtime label Apr 21, 2025
@ngbot ngbot bot added this to the Backlog milestone Apr 21, 2025
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown

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 policy.

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 framework 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