Skip to content

Commit

Permalink
Clean up orphan ReadableStream remaining after retry (#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
HaidongPang committed Nov 7, 2024
1 parent 3ba40cc commit 3efef4a
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions source/core/Ky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export class Ky {

// Delay the fetch so that body method shortcuts can set the Accept header
await Promise.resolve();
// Before using ky.request, _fetch clones a new Request for retries preparation.
// If retry is not needed, close the original Request ReadableStream for memory safety.
let response = await ky._fetch();

for (const hook of ky._options.hooks.afterResponse) {
Expand Down Expand Up @@ -63,6 +65,10 @@ export class Ky {
throw error;
}

// Now, it can be determined that a retry is not needed,
// close the ReadableStream of the ky.request.
await ky.request.body?.cancel();

// If `onDownloadProgress` is passed, it uses the stream API internally
/* istanbul ignore next */
if (ky._options.onDownloadProgress) {
Expand Down

0 comments on commit 3efef4a

Please sign in to comment.