{{ message }}
Repository navigation
[flutter_tools] Stream web dev server proxy responses instead of buffering them - #194934
Open
ishaquehassan wants to merge 2 commits into
Open
ishaquehassan wants to merge 2 commits into
ishaquehassan wants to merge 2 commits into
Conversation
…ering them Responses built by shelf_proxy carry no context, so shelf_io leaves HttpResponse.bufferOutput at its default and small streamed chunks such as SSE events or NDJSON lines wait in dart:io's 8 KB output buffer until the backend closes the response. Set shelf.io.buffer_output to false on proxied responses so each chunk is written out as soon as it arrives. Fixes flutter#194354
Contributor
There was a problem hiding this comment.
Code Review
This pull request configures the proxy middleware to disable output buffering for proxied responses by setting the shelf.io.buffer_output context option to false. This allows streamed response bodies to reach the client immediately. A corresponding unit test has been added to verify this streaming behavior. There are no review comments, and I have no feedback to provide.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

When a request matches a
web_dev_config.yamlproxy rule,_applyProxyRulesreturns theResponsethatshelf_proxybuilds around the backend stream. That response has no context, andshelf_ioonly touchesHttpResponse.bufferOutputwhenshelf.io.buffer_outputis in the context, so proxied responses go through dart:io's default 8 KB output buffer. A single SSE event or NDJSON line just sits in that buffer until it fills up or the backend closes the response. In the issue's repro the first line shows up after 33 ms when hitting the backend directly, and doesn't arrive through the proxy within the 3 second timeout.The fix sets
shelf.io.buffer_output: falseon the proxied response viaResponse.change. Status, headers and the streamed body carry over unchanged, only the context gets the extra key, soshelf_iowrites each chunk out as soon as it comes in from the backend. I didn't scope it totext/event-streamor NDJSON: dart:io already writes chunks over 8 KB straight through even with buffering on, so for regular responses the only difference is that small chunks no longer get coalesced. Both handlers inweb_asset_server.dart(release/Wasm and the DWDS debug one) go throughproxyMiddleware, so both get this.The new test in
proxy_test.darthas the backend send one line and only finish the response after that line has made it through the proxy. Without the fix the first line never reaches the client and the test times out. With it, all 40 tests inproxy_test.dartpass, and so does the rest oftest/general.shard/web/. I also ran a local check (not part of this PR) with an SSE backend behindWebAssetServer.start: the first event came through after 24 ms in release and 141 ms in Wasm debug. Without the change it took about 8 s, which is when that backend closed the response.dart-lang/shelf#503 asks for a
bufferOutputoption onproxyHandlerfor the same SSE case. Even with that option the tool would still have to opt in here, andshelf.io.buffer_outputis the keyshelf_ioalready documents for this, so there's no need to wait for ashelf_proxyrelease. If that option lands later, this call site can switch over to it.Fixes #194354
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.