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

Orphan ReadableStream remaining after retry #650

Open
HaidongPang opened this issue Nov 7, 2024 · 1 comment
Open

Orphan ReadableStream remaining after retry #650

HaidongPang opened this issue Nov 7, 2024 · 1 comment

Comments

@HaidongPang
Copy link

Cloudflare workers raise the following warning:

A ReadableStream branch was created but never consumed. Such branches can be created, for instance, by calling the tee() method on a ReadableStream, or by calling the clone() method on a Request or Response object. If a branch is created but never consumed, it can force the runtime to buffer the entire body of the stream in memory, which may cause the Worker to exceed its memory limit and be terminated. To avoid this, ensure that all branches created are consumed.

 * Unused stream created:
    at Ky._fetch (file:///....../node_modules/ky/source/core/Ky.ts:303:30)
    at function_ (file:///....../node_modules/ky/source/core/Ky.ts:38:28)
    at async Ky._retry (file:///....../node_modules/ky/source/core/Ky.ts:256:11)

The request object that was cloned in advance for retry preparation still remains after returning a response that does not require a retry.

export class Ky {
	protected async _fetch(): Promise<Response> {
		......

		// Cloning is done here to prepare in advance for retries
		const mainRequest = this.request;
		this.request = mainRequest.clone();

		if (this._options.timeout === false) {
			return this._options.fetch(mainRequest, nonRequestOptions);
		}

		return timeout(mainRequest, nonRequestOptions, this.abortController, this._options as TimeoutOptions);
	}
}

Related to :#648

@HaidongPang
Copy link
Author

Canceling the Request ReadableStream prepared for retry after determining that the current request does not need to be retried can ensure memory safety.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant