8000 fix(common): prevent reading chunks if app is destroyed by arturovt · Pull Request #61354 · angular/angular · GitHub
[go: up one dir, main page]

Skip to content

fix(common): prevent reading chunks if app is destroyed #61354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/common/http/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.dev/license
*/

import {inject, Injectable, InjectionToken, NgZone} from '@angular/core';
import {ApplicationRef, inject, Injectable, InjectionToken, NgZone} from '@angular/core';
import {Observable, Observer} from 'rxjs';

import {HttpBackend} from './backend';
Expand Down Expand Up @@ -70,6 +70,7 @@ export class FetchBackend implements HttpBackend {
private readonly fetchImpl =
inject(FetchFactory, {optional: true})?.fetch ?? ((...args) => globalThis.fetch(...args));
private readonly ngZone = inject(NgZone);
private readonly appRef = inject(ApplicationRef);

handle(request: HttpRequest<any>): Observable<HttpEvent<any>> {
return new Observable((observer) => {
Expand Down Expand Up @@ -148,6 +149,14 @@ export class FetchBackend implements HttpBackend {
// Here calling the async ReadableStreamDefaultReader.read() is responsible for triggering CD
await this.ngZone.runOutsideAngular(async () => {
while (true) {
// Prevent reading chunks if the app is destroyed. Otherwise, we risk doing
// unnecessary work or triggering side effects after teardown.
// This may happen if the app was explicitly destroyed before
// the response returned entirely.
if (this.appRef.destroyed) {
break;
}

const {done, value} = await reader.read();

if (done) {
Expand Down
0