Skip to content

fix(core): prevent destroyed views from being detached#66726

Open
edusperoni wants to merge 1 commit intoangular:mainfrom
edusperoni:fix/remove-view-during-destroy
Open

fix(core): prevent destroyed views from being detached#66726
edusperoni wants to merge 1 commit intoangular:mainfrom
edusperoni:fix/remove-view-during-destroy

Conversation

@edusperoni
Copy link
Copy Markdown
Contributor

@edusperoni edusperoni commented Jan 23, 2026

A destroyed view will always remove itself after destruction, so it should never be allowed to be detached before that as doing so will break the destroy chain

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

When destroying a component, one might inadvertently try to remove the element from its ViewContainerRef. Doing so will break the destroy lifecycle hook as angular is unable to go back up the destroy tree.

Issue Number: N/A

What is the new behavior?

Attempting to detach/remove a node that's destroyed will fail to do so.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

I hit this when playing with CDK portals and doing:

const ref = portalOutlet.attach(portal);
ref.onDestroy(() => {
  portalOutlet.dispose();
});

portalOutlet will remove the item from the ViewContainerRef, which would stop destroying components with no error.

Maybe we could instead throw an error with a ngDevMode check instead?

@pullapprove pullapprove bot requested a review from mmalerba January 23, 2026 17:40
@angular-robot angular-robot bot added the area: core Issues related to the framework runtime label Jan 23, 2026
@ngbot ngbot bot added this to the Backlog milestone Jan 23, 2026
@mmalerba mmalerba removed their request for review January 25, 2026 17:33
@edusperoni edusperoni force-pushed the fix/remove-view-during-destroy branch from 58f19b2 to 26677af Compare January 26, 2026 21:03
@JeanMeche
Copy link
Copy Markdown
Member

The change seems to be consistenly breaking as its shown by our local tests and ones in Google's code base.

@edusperoni
Copy link
Copy Markdown
Contributor Author

@JeanMeche can you clarify where it breaks? It seems like the available tests all pass and I believe the behavior is undesired right now (breaking the destroy flow)

A destroyed view will always remove itself after destruction, so it should never be allowed to be detached before that as doing so will break the destroy chain
@JeanMeche JeanMeche force-pushed the fix/remove-view-during-destroy branch from 26677af to a2367a1 Compare February 13, 2026 10:27
@JeanMeche
Copy link
Copy Markdown
Member

It breaks some devtools and angular.dev tests. The timeouts are likely to be tied to some tests stuck on a whenStable() meaning we are breaking some other assumptions that currently caught in our core tests.

@JeanMeche JeanMeche requested a review from atscott February 13, 2026 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Issues related to the framework runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants