Skip to content

Commit 5a3a154

Browse files
arturovtmhevery
authored andcommitted
fix(core): unsubscribe from the onError when the root view is removed (#39940)
At the moment, when creating a root module, a subscription to the `onError` subject is also created. It captures the scope where `NgModuleRef` is created and prevents it from being garbage collected. Also note that this `NgModuleRef` has a reference to the root module instance (e.g. `AppModule`), which also prevents it from being GC'd. PR Close #39940
1 parent f01c713 commit 5a3a154

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

packages/core/src/application_ref.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,17 @@ export class PlatformRef {
350350
if (!exceptionHandler) {
351351
throw new Error('No ErrorHandler. Is platform module (BrowserModule) included?');
352352
}
353-
moduleRef.onDestroy(() => remove(this._modules, moduleRef));
354-
ngZone!.runOutsideAngular(() => ngZone!.onError.subscribe({
355-
next: (error: any) => {
356-
exceptionHandler.handleError(error);
357-
}
358-
}));
353+
ngZone!.runOutsideAngular(() => {
354+
const subscription = ngZone!.onError.subscribe({
355+
next: (error: any) => {
356+
exceptionHandler.handleError(error);
357+
}
358+
});
359+
moduleRef.onDestroy(() => {
360+
remove(this._modules, moduleRef);
361+
subscription.unsubscribe();
362+
});
363+
});
359364
return _callAndReportToErrorHandler(exceptionHandler, ngZone!, () => {
360365
const initStatus: ApplicationInitStatus = moduleRef.injector.get(ApplicationInitStatus);
361366
initStatus.runInitializers();

packages/core/test/acceptance/bootstrap_spec.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ApplicationRef, COMPILER_OPTIONS, Component, destroyPlatform, NgModule, TestabilityRegistry, ViewEncapsulation} from '@angular/core';
9+
import {ApplicationRef, COMPILER_OPTIONS, Component, destroyPlatform, NgModule, NgZone, TestabilityRegistry, ViewEncapsulation} from '@angular/core';
1010
import {expect} from '@angular/core/testing/src/testing_internal';
1111
import {BrowserModule} from '@angular/platform-browser';
1212
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
@@ -227,6 +227,22 @@ describe('bootstrap', () => {
227227
}));
228228
});
229229

230+
describe('PlatformRef cleanup', () => {
231+
it('should unsubscribe from `onError` when Injector is destroyed',
232+
withBody('<my-app></my-app>', async () => {
233+
const TestModule = createComponentAndModule();
234+
235+
const ngModuleRef = await platformBrowserDynamic().bootstrapModule(TestModule);
236+
const ngZone = ngModuleRef.injector.get(NgZone);
237+
238+
expect(ngZone.onError.observers.length).toBe(1);
239+
240+
ngModuleRef.destroy();
241+
242+
expect(ngZone.onError.observers.length).toBe(0);
243+
}));
244+
});
245+
230246
onlyInIvy('options cannot be changed in Ivy').describe('changing bootstrap options', () => {
231247
beforeEach(() => {
232248
spyOn(console, 'error');
@@ -365,4 +381,4 @@ export class MultipleSelectorsAppComponent {
365381
bootstrap: [MultipleSelectorsAppComponent],
366382
})
367383
export class MultipleSelectorsAppModule {
368-
}
384+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy