Skip to content

Conversation

@crisbeto
Copy link
Member

@crisbeto crisbeto commented Oct 8, 2024

We stop tracking afterRender hooks as soon as they execute, but their on destroy callbacks stay registered until either the injector is destroyed or the user calls destroy manually. This was leading to memory leaks in the @defer triggers based on top of afterRender when placed inside long-lived views, because the callback would execute, but its destroy logic was waiting for the view to be destroyed.

These changes resolve the issue by destroying the AfterRenderRef once it is executed.

We stop tracking `afterRender` hooks as soon as they execute, but their on destroy callbacks stay registered until either the injector is destroyed or the user calls `destroy` manually. This was leading to memory leaks in the `@defer` triggers based on top of `afterRender` when placed inside long-lived views, because the callback would execute, but its destroy logic was waiting for the view to be destroyed.

These changes resolve the issue by destroying the `AfterRenderRef` once it is executed.
@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer area: core Issues related to the framework runtime target: patch This PR is targeted for the next patch release labels Oct 8, 2024
@ngbot ngbot bot added this to the Backlog milestone Oct 8, 2024
@angular-robot angular-robot bot added area: core Issues related to the framework runtime and removed area: core Issues related to the framework runtime labels Oct 8, 2024
@crisbeto crisbeto marked this pull request as ready for review October 8, 2024 09:02
@crisbeto crisbeto requested a review from AndrewKushnir October 8, 2024 09:03
this.sequences.delete(sequence);
// Destroy the sequence so its on destroy callbacks can be cleaned up
// immediately, instead of waiting until the injector is destroyed.
sequence.destroy();
Copy link
Member Author

Choose a reason for hiding this comment

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

I was a bit on the fence whether to do it here or afterRun. I did it here, because it seems like the AfterRenderRef isn't intended to have much logic, but to hold the current state.

Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

@crisbeto thanks for the fix 👍

@AndrewKushnir AndrewKushnir removed the action: review The PR is still awaiting reviews from at least one requested reviewer label Oct 8, 2024
@crisbeto crisbeto added the action: merge The PR is ready for merge by the caretaker label Oct 8, 2024
@AndrewKushnir
Copy link
Contributor

This PR was merged into the repository by commit 67db430.

The changes were merged into the following branches: main, 18.2.x

AndrewKushnir pushed a commit that referenced this pull request Oct 8, 2024
We stop tracking `afterRender` hooks as soon as they execute, but their on destroy callbacks stay registered until either the injector is destroyed or the user calls `destroy` manually. This was leading to memory leaks in the `@defer` triggers based on top of `afterRender` when placed inside long-lived views, because the callback would execute, but its destroy logic was waiting for the view to be destroyed.

These changes resolve the issue by destroying the `AfterRenderRef` once it is executed.

PR Close #58119
@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 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 Nov 8, 2024
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: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants