Content-Length: 318041 | pFad | http://github.com/angular/angular/commit/17fe731e8ee52388b9d87ec99e604da47a87d63b

7F fix(common): cancel reader when app is destroyed (#61528) · angular/angular@17fe731 · GitHub
Skip to content

Commit 17fe731

Browse files
arturovtthePunderWoman
authored andcommitted
fix(common): cancel reader when app is destroyed (#61528)
Streams left in a pending state (due to `break` without cancel) may continue consuming or holding onto data behind the scenes. Calling `reader.cancel()` allows the browser or the underlying system to release any network or memory resources associated with the stream. PR Close #61528
1 parent 971981e commit 17fe731

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

packages/common/http/src/fetch.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ export class FetchBackend implements HttpBackend {
147147
// when the zone is nooped.
148148
const reqZone = typeof Zone !== 'undefined' && Zone.current;
149149

150+
let canceled = false;
151+
150152
// Perform response processing outside of Angular zone to
151153
// ensure no excessive change detection runs are executed
152154
// Here calling the async ReadableStreamDefaultReader.read() is responsible for triggering CD
@@ -157,6 +159,12 @@ export class FetchBackend implements HttpBackend {
157159
// This may happen if the app was explicitly destroyed before
158160
// the response returned entirely.
159161
if (this.appRef.destroyed) {
162+
// Streams left in a pending state (due to `break` without cancel) may
163+
// continue consuming or holding onto data behind the scenes.
164+
// Calling `reader.cancel()` allows the browser or the underlying
165+
// system to release any network or memory resources associated with the stream.
166+
await reader.cancel();
167+
canceled = true;
160168
break;
161169
}
162170

@@ -188,6 +196,15 @@ export class FetchBackend implements HttpBackend {
188196
}
189197
});
190198

199+
// We need to manage the canceled state — because the Streams API does not
200+
// expose a direct `.state` property on the reader.
201+
// We need to `return` because `parseBody` may not be able to parse chunks
202+
// that were only partially read (due to cancellation caused by app destruction).
203+
if (canceled) {
204+
observer.complete();
205+
return;
206+
}
207+
191208
// Combine all chunks.
192209
const chunksAll = this.concatChunks(chunks, receivedLength);
193210
try {

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/angular/angular/commit/17fe731e8ee52388b9d87ec99e604da47a87d63b

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy