Skip to content
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

Clean up orphan ReadableStream remaining after retry (#650) #651

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 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 it and saves the clone for future retries to use.
// If retry is not needed, close the cloned request's ReadableStream for memory safety.
let response = await ky._fetch();

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

// Now that we know a retry is not needed, close the ReadableStream of the cloned request.
if (!ky.request.bodyUsed) {
await ky.request.body?.cancel();
}

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