Skip to content

NG0911: View has already been destroyed. #54527

@dwilches

Description

@dwilches

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions