Skip to content

fix(core): destroy partially created views when creation fails - #72222

Closed
JeanMeche wants to merge 1 commit into
angular:mainfrom
JeanMeche:fix/72210-destroy-failed-views
Closed

JeanMeche wants to merge 1 commit into
angular:mainfrom
JeanMeche:fix/72210-destroy-failed-views

Conversation

@JeanMeche

Copy link
Copy Markdown
Member

When an error is thrown while creating an embedded view or dynamic component, the view is never attached to a container or returned to the caller. Any earlier siblings' DestroyRef callbacks, ngOnDestroy hooks, effects, and listeners are left alive indefinitely (and accumulate on @boundary $reset() retries).

This PR tears down partially created views before rethrowing the creation error:

  • Guard executeOnDestroys against null/undefined slots so uninstantiated directives, pipes, and child component views do not invoke ngOnDestroy on null.
  • Guard processCleanups and applyForeignNodes against incomplete cleanup/node arrays on partially created views.
  • Catch and report any secondary teardown errors via handleUncaughtError without masking the original creation error.
  • Apply the same teardown in ComponentFactory.create when dynamic component creation fails.

Supersedes #72220.
Fixes #72210

When an error is thrown while creating an embedded view or dynamic component, the view is never attached to a container or returned to the caller, leaving earlier siblings' destroy hooks, effects, and listeners alive indefinitely.

Tear down the partially created view before rethrowing the creation error:
- Guard `executeOnDestroys` against `null`/`undefined` slots so uninstantiated directives, pipes, and child views do not invoke `ngOnDestroy` on `null`.
- Guard `processCleanups` and `applyForeignNodes` against incomplete cleanup/node arrays on partially created views.
- Catch and report any secondary teardown errors via `handleUncaughtError` without masking the original creation error.
- Apply the same cleanup in `ComponentFactory.create` when dynamic component creation fails.

Fixes angular#72210
@JeanMeche
JeanMeche requested a review from atscott November 26, 2026 09:11
@angular-robot angular-robot Bot added the area: core Issues related to the framework runtime label Nov 26, 2026
@ngbot ngbot Bot added this to the Backlog milestone Nov 26, 2026
@JeanMeche JeanMeche closed this Nov 26, 2026
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.

@boundary keeps subscriptions alive after a sibling constructor throws

1 participant