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 6de1156
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 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,12 @@ export class Ky {
throw error;
}

// Now, it can be determined that a retry is not needed,
// close the ReadableStream of the ky.request.
if (!ky.request.bodyUsed) {
await ky.request.arrayBuffer();
}

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

0 comments on commit 6de1156

Please sign in to comment.