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

Fix multiple read attempts in fetch client #876

Merged
merged 2 commits into from
Oct 11, 2023
Merged

Conversation

srikrsna-buf
Copy link
Member

@srikrsna-buf srikrsna-buf commented Oct 7, 2023

Fix multiple read attempts in fetch client. I caught this while working on connectrpc/examples-es#1075. The fix similar to how we read the iterable:

const it = iterable[Symbol.asyncIterator]();
return new ReadableStream<Uint8Array>(<UnderlyingSource<Uint8Array>>{

This also fixes the underlying issue of pipe calling the iterator twice.

Copy link
Member

@timostamm timostamm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From connectrpc/examples-es#1075:

Using the fetch client from connect, the client for server steaming throws an error at the very end of the steam with: Invalid state: ReadableStream is locked.

Seems like some code is calling [Symbol.asyncIterator]() twice. If this is in our code base, it would be great to understand where this happens, and avoid it. Did you take a look at how the duplicate call occurs?

@srikrsna-buf
Copy link
Member Author

Seems like some code is calling [Symbol.asyncIterator]() twice. If this is in our code base, it would be great to understand where this happens, and avoid it. Did you take a look at how the duplicate call occurs?

It seems to be this line:

// cleanup any allocated resources.
source[Symbol.asyncIterator]()
.return?.()
.catch(() => {
in pipe.

@timostamm
Copy link
Member

Yeah, looks like that line in async-iterable.ts would do it. I believe we should avoid that.

If you iterate over an Array or Map, you would expect a second iteration to restart from the first entry. Under the hood, [Symbol.iterator] is called once per iteration. I would expect [Symbol.asyncIterator] to have the same semantics. By calling it twice for what is effectively a single iteration, we dip out toes into undefined behavior.

@srikrsna-buf srikrsna-buf merged commit 05603ab into main Oct 11, 2023
5 checks passed
@srikrsna-buf srikrsna-buf deleted the sk/fix-fetch-lock branch October 11, 2023 13:29
@timostamm timostamm mentioned this pull request Oct 27, 2023
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

Successfully merging this pull request may close these issues.

None yet

2 participants