Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 35309bb

Browse filesBrowse 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 1676d22 commit 35309bb
Copy full SHA for 35309bb

File tree

2 files changed

+29
-8
lines changed
Filter options

2 files changed

+29
-8
lines changed

‎packages/core/src/application_ref.ts

Copy file name to clipboardExpand all lines: packages/core/src/application_ref.ts
+11-6Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,17 @@ export class PlatformRef {
328328
if (!exceptionHandler) {
329329
throw new Error('No ErrorHandler. Is platform module (BrowserModule) included?');
330330
}
331-
moduleRef.onDestroy(() => remove(this._modules, moduleRef));
332-
ngZone!.runOutsideAngular(() => ngZone!.onError.subscribe({
333-
next: (error: any) => {
334-
exceptionHandler.handleError(error);
335-
}
336-
}));
331+
ngZone!.runOutsideAngular(() => {
332+
const subscription = ngZone!.onError.subscribe({
333+
next: (error: any) => {
334+
exceptionHandler.handleError(error);
335+
}
336+
});
337+
moduleRef.onDestroy(() => {
338+
remove(this._modules, moduleRef);
339+
subscription.unsubscribe();
340+
});
341+
});
337342
return _callAndReportToErrorHandler(exceptionHandler, ngZone!, () => {
338343
const initStatus: ApplicationInitStatus = moduleRef.injector.get(ApplicationInitStatus);
339344
initStatus.runInitializers();

‎packages/core/test/acceptance/bootstrap_spec.ts

Copy file name to clipboardExpand all lines: packages/core/test/acceptance/bootstrap_spec.ts
+18-2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.