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

Support teeing readable byte streams #1114

Merged
merged 32 commits into from
Jul 8, 2021

Conversation

MattiasBuelens
Copy link
Collaborator

@MattiasBuelens MattiasBuelens commented Mar 20, 2021

With this PR, teeing a readable byte stream will now create two readable byte streams (instead of two "default" readable streams). See #1111 for context.

This implements the "hard" solution: when a branch is being read using a BYOB reader, the view of that read request is forwarded to the original stream, so it can read directly into that view. After the view is filled, an identical copy is created and enqueued on the other branch, so they both see the same data.

To do:

  • Write tests
  • Update specification text to match new reference implementation

(See WHATWG Working Mode: Changes for more details.)


Preview | Diff

@MattiasBuelens MattiasBuelens force-pushed the tee-byte-stream branch 5 times, most recently from 18544b8 to e3c2532 Compare March 24, 2021 01:20
index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
@MattiasBuelens MattiasBuelens force-pushed the tee-byte-stream branch 2 times, most recently from 158d02e to 7f3fdc9 Compare April 9, 2021 20:34
domenic pushed a commit that referenced this pull request Jun 2, 2021
Previously, if you cancel the stream while a BYOB read is pending, the stream has to wait for the underlying byte source to call respond(0) before it can return the BYOB request's buffer to the caller. This makes underlying byte sources difficult to write in a robust way. After this change, the contract changes so that canceling a stream while a BYOB read is pending will always lead to the stream not giving you back your buffer. This means that cancel() can immediately resolve all pending BYOB reads with the classic { done: true, value: undefined }, without having to wait for the underlying byte source. This solves the problem, and would make it easier to implement an underlying byte source.

To make this work, an additional change was required: when the stream is canceled, any pending BYOB request is now immediately invalidated, so the underlying byte source doesn't erroneously think that it still needs to provide a response. (This aligns with the behavior of controller.enqueue(), which throws if the stream is already closed.)

See #1114 (comment) and #1123 (comment) for some discussion and background.
yutakahirano pushed a commit to yutakahirano/streams that referenced this pull request Jun 3, 2021
Previously, if you cancel the stream while a BYOB read is pending, the stream has to wait for the underlying byte source to call respond(0) before it can return the BYOB request's buffer to the caller. This makes underlying byte sources difficult to write in a robust way. After this change, the contract changes so that canceling a stream while a BYOB read is pending will always lead to the stream not giving you back your buffer. This means that cancel() can immediately resolve all pending BYOB reads with the classic { done: true, value: undefined }, without having to wait for the underlying byte source. This solves the problem, and would make it easier to implement an underlying byte source.

To make this work, an additional change was required: when the stream is canceled, any pending BYOB request is now immediately invalidated, so the underlying byte source doesn't erroneously think that it still needs to provide a response. (This aligns with the behavior of controller.enqueue(), which throws if the stream is already closed.)

See whatwg#1114 (comment) and whatwg#1123 (comment) for some discussion and background.
Copy link
Member

@domenic domenic left a comment

Choose a reason for hiding this comment

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

LGTM with nits. Exciting...

index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
index.bs Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
Copy link
Collaborator

@ricea ricea left a comment

Choose a reason for hiding this comment

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

I've checked the algorithms and cross-referenced against the reference implementation and it all looks good.

I haven't analysed it for re-entrancy risk. I'm a bit worried about that.

index.bs Outdated Show resolved Hide resolved
index.bs Outdated Show resolved Hide resolved
Copy link
Collaborator Author

@MattiasBuelens MattiasBuelens left a comment

Choose a reason for hiding this comment

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

I've added handling for errors from cloning chunks.

Still gotta find some time to write tests for all these edge cases. 😛

index.bs Show resolved Hide resolved
index.bs Show resolved Hide resolved
Copy link
Member

@domenic domenic left a comment

Choose a reason for hiding this comment

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

Spec looks good to me at this point! I guess the new error cases, including for non-byte tee() are testable (although probably not in the reference implementation).

index.bs Show resolved Hide resolved
@MattiasBuelens MattiasBuelens marked this pull request as ready for review June 14, 2021 23:07
Copy link
Member

@domenic domenic left a comment

Choose a reason for hiding this comment

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

LGTM. Not sure why the bots are failing; try rebasing? That was the wrong PR.

@ricea feel free to take a look

@domenic domenic merged commit cada812 into whatwg:main Jul 8, 2021
@MattiasBuelens MattiasBuelens deleted the tee-byte-stream branch July 8, 2021 20:53
@MattiasBuelens
Copy link
Collaborator Author

Yay! 🥳

I assume this means Chrome is interested in implementing? I'll go ahead and file an implementation bug then. 🙂

@ricea
Copy link
Collaborator

ricea commented Jul 9, 2021

Yes, Chrome is interested.

moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this pull request Jul 18, 2021
…streams, a=testonly

Automatic update from web-platform-tests
Streams: tests for teeing readable byte streams

Follows whatwg/streams#1114.
--

wpt-commits: 87a4c80598aee5178c385628174f1832f5a28ad6
wpt-pr: 29190
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this pull request Jul 20, 2021
…streams, a=testonly

Automatic update from web-platform-tests
Streams: tests for teeing readable byte streams

Follows whatwg/streams#1114.
--

wpt-commits: 87a4c80598aee5178c385628174f1832f5a28ad6
wpt-pr: 29190
jamienicol pushed a commit to jamienicol/gecko that referenced this pull request Jul 20, 2021
…streams, a=testonly

Automatic update from web-platform-tests
Streams: tests for teeing readable byte streams

Follows whatwg/streams#1114.
--

wpt-commits: 87a4c80598aee5178c385628174f1832f5a28ad6
wpt-pr: 29190
jamienicol pushed a commit to jamienicol/gecko that referenced this pull request Jul 21, 2021
…streams, a=testonly

Automatic update from web-platform-tests
Streams: tests for teeing readable byte streams

Follows whatwg/streams#1114.
--

wpt-commits: 87a4c80598aee5178c385628174f1832f5a28ad6
wpt-pr: 29190
aarongable pushed a commit to chromium/chromium that referenced this pull request Apr 11, 2023
This CL implements teeing support for readable byte streams, so that
teeing a byte stream creates two readable byte streams instead of
two default readable streams.

The corresponding spec change was done in
whatwg/streams#1114.

Bug: 1227496
Change-Id: Ica29d939b7b3a3f7d6086b42ee1e8a538b611b25
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3757034
Commit-Queue: Nidhi Jaju <[email protected]>
Reviewed-by: Adam Rice <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1128460}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants