Visitar URL original
fix(common): remove abort listener on the abort path too in httpResource · angular/angular@850db9d · GitHub
Skip to content

Commit 850db9d

Browse files
arturovtthePunderWoman
authored andcommitted
fix(common): remove abort listener on the abort path too in httpResource
httpResource registers an 'abort' listener on the resource's AbortSignal to unsubscribe the underlying request when it's cancelled. The listener was only removed from the subscription's own error/complete callbacks, but calling sub.unsubscribe() directly - which is exactly what happens when the listener itself fires - doesn't trigger those callbacks. So on the one path where the signal actually aborts, the listener was never removed. Passing { once: true } lets the removal happen automatically once the listener fires, covering that path without touching the existing error/complete cleanup, which is still needed for the paths where the request finishes without ever aborting. This also happens to route around a real leak on affected engines: an AbortSignal that's had any listener attached isn't garbage collected until it fires. https://bugs.webkit.org/show_bug.cgi?id=298499 (cherry picked from commit 5128dec)
1 parent cfa8967 commit 850db9d

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

‎packages/common/http/src/resource.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,7 @@ function normalizeRequest(
299299
? unwrappedRequest.headers
300300
: new HttpHeaders(
301301
unwrappedRequest.headers as
302-
| Record<string, string | number | Array<string | number>>
303-
| undefined,
302+
Record<string, string | number | Array<string | number>> | undefined,
304303
);
305304

306305
const params =
@@ -376,13 +375,13 @@ class HttpResourceImpl<T>
376375
// is subscribe even if it isn't initialized yet.
377376
let aborted = false;
378377

379-
// Track the abort listener so it can be removed if the Observable completes (as a memory
380-
// optimization).
378+
// `once: true`: calling unsubscribe() here (on abort) doesn't trigger the error/complete
379+
// callbacks below, so without it this listener wouldn't get removed on the abort path.
381380
const onAbort = () => {
382381
aborted = true;
383382
sub?.unsubscribe();
384383
};
385-
abortSignal.addEventListener('abort', onAbort);
384+
abortSignal.addEventListener('abort', onAbort, {once: true});
386385

387386
// Start off stream as undefined.
388387
const stream = signal<ResourceStreamItem<T>>({value: undefined as T});

0 commit comments

Comments
 (0)