Skip to content

Orphan ReadableStream remaining after retry #650

Closed
@HaidongPang

Description

@HaidongPang

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions