Skip to content

Commit 8768894

Browse files
arturovtthePunderWoman
authored andcommitted
fix(common): prevent reading chunks if app is destroyed (#61354)
Prevents processing response chunks after the application has been destroyed. PR Close #61354
1 parent 3440338 commit 8768894

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

packages/common/http/src/fetch.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {inject, Injectable, InjectionToken, NgZone} from '@angular/core';
9+
import {ApplicationRef, inject, Injectable, InjectionToken, NgZone} from '@angular/core';
1010
import {Observable, Observer} from 'rxjs';
1111

1212
import {HttpBackend} from './backend';
@@ -73,6 +73,7 @@ export class FetchBackend implements HttpBackend {
7373
private readonly fetchImpl =
7474
inject(FetchFactory, {optional: true})?.fetch ?? ((...args) => globalThis.fetch(...args));
7575
private readonly ngZone = inject(NgZone);
76+
private readonly appRef = inject(ApplicationRef);
7677

7778
handle(request: HttpRequest<any>): Observable<HttpEvent<any>> {
7879
return new Observable((observer) => {
@@ -151,6 +152,14 @@ export class FetchBackend implements HttpBackend {
151152
// Here calling the async ReadableStreamDefaultReader.read() is responsible for triggering CD
152153
await this.ngZone.runOutsideAngular(async () => {
153154
while (true) {
155+
// Prevent reading chunks if the app is destroyed. Otherwise, we risk doing
156+
// unnecessary work or triggering side effects after teardown.
157+
// This may happen if the app was explicitly destroyed before
158+
// the response returned entirely.
159+
if (this.appRef.destroyed) {
160+
break;
161+
}
162+
154163
const {done, value} = await reader.read();
155164

156165
if (done) {

0 commit comments

Comments
 (0)