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 bugs in handling multiple incoming bodies piped through a TransFormStream to an outgoing body #171

Merged
merged 4 commits into from
Nov 6, 2024

Conversation

tschneidereit
Copy link
Member

This was pretty gnarly to work through, but I think came out reasonably clean. The two key pieces are:

  • delaying the first read from an incoming stream that's been piped to a TransformStream until the latter is actually read from, and hence doesn't have backpressure applied anymore. This part guarantees that when the read would happen, we have the full pipeline in place and can hand things off to the host API instead of handling anything in the JS fetch API implementation.
  • properly integrating these two pieces, and only closing the incoming body's ReadableStream once the host API reports that it's been dealt with to completion.

This PR additionally contains some fixes to how writes to bodies are handled, and some cleanup. These are both in separate patches which can be reviewed separately, if that seems easier.

…ect handles

It was pretty hard to follow when a caller would expect to have a valid handle, or when a `nullptr` would be acceptable.

Signed-off-by: Till Schneidereit <[email protected]>
…ormStream` to an outgoing body

This was pretty gnarly to work through, but I think came out reasonably clean. The two key pieces are:
- delaying the first read from an incoming stream that's been piped to a `TransformStream` until the latter is actually read from, and hence doesn't have backpressure applied anymore. This part guarantees that when the read would happen, we have the full pipeline in place and can hand things off to the host API instead of handling anything in the JS `fetch` API implementation.
- properly integrating these two pieces, and only closing the incoming body's `ReadableStream` once the host API reports that it's been dealt with to completion.

Signed-off-by: Till Schneidereit <[email protected]>
Before, any chunk would be written synchronously, with write operations to the outgoing body in a loop. That both meant that large chunks would block all script execution until they're done, and that things wouldn't necessarily always work.

To wit, the test added here doesn't pass without this change. It streams a sentence in a very convoluted way, sending nested requests for each word, which then respond by sending the word back, letter for letter. Without this patch, that part breaks after the second letter in a word has been sent back.

Signed-off-by: Till Schneidereit <[email protected]>
Copy link
Contributor

@guybedford guybedford left a comment

Choose a reason for hiding this comment

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

Looks great thanks for working on this. It's just a shame Fastly can't share the implementation of these improvements.

I was a little surprised the WPT had no coverage here to change, and tested the most recent WPT against this, but it seems that's just the way it goes.

host-apis/wasi-0.2.0/host_api.cpp Outdated Show resolved Hide resolved
@tschneidereit
Copy link
Member Author

Looks great thanks for working on this. It's just a shame Fastly can't share the implementation of these improvements.

Fastly doesn't need this, as C@E has a hostcall for doing this kind of append natively and much more efficiently, which the JS runtime makes use of. I'm working on adding an equivalent thing to wasi:http.

I was a little surprised the WPT had no coverage here to change, and tested the most recent WPT against this, but it seems that's just the way it goes.

Same: I would've expected this to be covered somewhere and was surprised it's not.

@tschneidereit tschneidereit merged commit aa18177 into main Nov 6, 2024
5 checks passed
@tschneidereit tschneidereit deleted the fix-body-append branch November 6, 2024 14:23
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.

2 participants