Repository navigation
Closed
Labels
Milestone
Description
Which @angular/* package(s) are the source of the bug?
core
Is this a regression?
Yes
Description
I recently introduced to my app DestroyRef to replace @ngneat/until-destroy and I started getting an error in automated tests: NG0911: View has already been destroyed.
This issue doesn't manifest while users are interacting with the app, but when I'm testing the app via automated means (where I click very fast in buttons and components are created and destroyed too quickly).
The code where the exception happened is the following (I marked which one is line 76, the one mentioned in the stack trace):
ngOnInit() {
this.myHttpClient.getCharts()
.then(allCharts => {
this.allCharts = allCharts;
this.myService.getPerms$("SOME_PERM")
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(hasPerms => this.refreshChartDetails(hasPerms)); # line 76
})
.catch(error => this.myErrorService.showError(error));
}
Please provide a link to a minimal reproduction of the bug
No response
Please provide the exception or error you saw
NG0911: View has already been destroyed.
at storeLViewOnDestroy (core.mjs:2846:15)
at NodeInjectorDestroyRef.onDestroy (core.mjs:14215:9)
at Observable._subscribe (rxjs-interop.mjs:27:41)
at Observable._trySubscribe (Observable.js:37:25)
at Observable.js:31:30
at errorContext (errorContext.js:19:9)
at Observable.subscribe (Observable.js:22:21)
at takeUntil.js:7:29
at SafeSubscriber.<anonymous> (lift.js:10:28)
at Observable.js:26:30
at errorContext (errorContext.js:19:9)
at AnonymousSubject.subscribe (Observable.js:22:21)
at my-chart-component.component.ts:76:22
at _ZoneDelegate.invoke (zone.js:368:26)
at Object.onInvoke (core.mjs:14832:33)
### Please provide the environment you discovered this bug in (run `ng version`)
```true
Angular CLI: 17.1.2
Node: 18.13.0
Package Manager: npm 8.19.3
OS: linux x64
Angular: 17.1.2
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, material, material-moment-adapter, platform-browser
... platform-browser-dynamic, router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1701.2
@angular-devkit/build-angular 17.1.2
@angular-devkit/core 17.1.2
@angular-devkit/schematics 17.1.2
@schematics/angular 17.1.2
rxjs 7.8.1
typescript 5.3.3
zone.js 0.14.3
Anything else?
No response
Reacted by Kleyguerth, Tobi, Ilya Borisov, Ramon, Максим Иванов, Alex Inkin, Jeff Barnes, Eugene Gusarov, Daniel Barta, Sergiobop and 12 more
Reacted by Ohmi Nakazawa, jleroy-dev and ajanuszandea
Activity
alxhub commentedon Feb 20, 2024
Without a running reproduction it's difficult to be certain, but this seems like it's working as intended. By the time the
Promiseresolves, the containing component is already destroyed. The.thencallback does.subscribe(), which subscribes through thetakeUntilDestroyedoperator, which tries to use theDestroyRefbut it's too late - the component is already dead.The root cause here is the promise callback which executes the
.subscribeunconditionally, even if the component is already destroyed.dwilches commentedon Feb 20, 2024
@alxhub Thanks, that explanation makes sense.
I feel that Angular shouldn't be logging an error in that case though, as it's a normal use case, otherwise users will need to add a second validation before each "subscribe" that involves "takeUntilDestroyed" to make sure the component has not been destroyed already.
Even if this is just a log, and not a real error, this causes trouble for tools that monitor the health of the application, for example we have
failOnConsoleErrorin Cypress, so this makes the tests fail from time to time (as it acts as a race-condition). Similarly, error reporting tools like Rollbar would need adjustment to learn to ignore errors if this happens in production.sysmat commentedon Feb 29, 2024
yes, have the same problem, but in runtime
sysmat commentedon Feb 29, 2024
I moved to old way
OnDestroyhook andtakeUntiland error is gonesysmat commentedon Mar 1, 2024
DmSor commentedon Mar 6, 2024
It would be convenient if DestroyRef had a destroyed flag.
It would be possible to check before creating a subscription.
alexkunin commentedon Mar 16, 2024
I tend to agree with @alxhub, things work as intended. It's not totally unreasonable to not log the error in this particular case, but generally I'd rather know if my code accesses resources that have been already freed.
Maybe it would be better to fix the code snippet listed above, instead of extending Angular responsibility with detecting right or wrong cases of "View has already been destroyed" error.
For example, that code snippet uses three somewhat orthogonal concepts:
What if we rewrite code like this:
So, now:
Also, I'd suggest to rewrite code to be more reactive, e.g.:
But I see too little context to be sure that's viable in your case.
smleake commentedon Apr 9, 2024
We've recently started experiencing this issue within our codebase as well and have identified that it's coming from our usages of
takeUntilDestroyed. We have found that a likely significant contributor to the errors is an Observable that shouldn't be using the operator in the first place (because it's within a hot function that get's invoked multiple times throughout the life of the component,take(1)would likely be a better fit). Just so I'm absolutely clear, this error occurs whenever we try to subscribe to an Observable utilizingtakeUntilDestroyedbut the view has been destroyed already. Is that a fair assumption? Thanks guys, appreciate your time!CosmoFruit commentedon Apr 23, 2024
Reproduced error NG0911 case with CVA:
https://stackblitz.com/edit/stackblitz-starters-ef36xs?file=src%2Fmain.ts
waterplea commentedon Apr 24, 2024
I think if we use
takeUntilDestroyedon an already destroyed view it should immediately terminate the subscription, rather than throw an error.31 remaining items
fix(core): call `DestroyRef` on destroy callback if view is destroyed (…
arturovt commentedon Apr 2, 2025
@dwilches you can close the issue since the fix has been merged (arturovt@af55238).
canutoalex commentedon Apr 7, 2025
@dwilches @thePunderWoman Any chance on making this available not only in major, but in a next 19.x version? We are experiencing it right now and we would like to avoid workarounds or reverting to OnDestroy / takeUntil in a lot of places.
angular-automatic-lock-bot commentedon May 8, 2025
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.
JeanMeche commentedon May 22, 2025
I'll re-open this issue for tracking purposes,
We decided to revert #58008, because it introduces a semi-synchronous behaviour that we'd like to avoid in the framework.
For those interested, a short read on mixing sync & async callbacks .
fix(core): `takeUntilDestroyed` completes immediately if DestroyRef a…
fix(core): `takeUntilDestroyed` completes immediately if DestroyRef a…
fix(core): `takeUntilDestroyed` completes immediately if DestroyRef a…
fix(core): `takeUntilDestroyed` completes immediately if DestroyRef a…
fix(core): `takeUntilDestroyed` completes immediately if DestroyRef a…
fix(core): `takeUntilDestroyed` completes immediately if DestroyRef a…