[flutter_tools] Stream web dev server proxy responses instead of buffering them by ishaquehassan · Pull Request #194934 · flutter/flutter · GitHub
Skip to content

[flutter_tools] Stream web dev server proxy responses instead of buffering them - #194934

Open
ishaquehassan wants to merge 2 commits into
flutter:masterfrom
ishaquehassan:fix/194354-web-proxy-streaming
Open

ishaquehassan wants to merge 2 commits into
flutter:masterfrom
ishaquehassan:fix/194354-web-proxy-streaming

Conversation

@ishaquehassan

Copy link
Copy Markdown
Contributor

When a request matches a web_dev_config.yaml proxy rule, _applyProxyRules returns the Response that shelf_proxy builds around the backend stream. That response has no context, and shelf_io only touches HttpResponse.bufferOutput when shelf.io.buffer_output is 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: false on the proxied response via Response.change. Status, headers and the streamed body carry over unchanged, only the context gets the extra key, so shelf_io writes each chunk out as soon as it comes in from the backend. I didn't scope it to text/event-stream or 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 in web_asset_server.dart (release/Wasm and the DWDS debug one) go through proxyMiddleware, so both get this.

The new test in proxy_test.dart has 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 in proxy_test.dart pass, and so does the rest of test/general.shard/web/. I also ran a local check (not part of this PR) with an SSE backend behind WebAssetServer.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 bufferOutput option on proxyHandler for the same SSE case. Even with that option the tool would still have to opt in here, and shelf.io.buffer_output is the key shelf_io already documents for this, so there's no need to wait for a shelf_proxy release. 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-assist bot 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.

…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
@github-actions github-actions Bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Oct 21, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@flutter-zl
flutter-zl requested a review from mdebbar October 21, 2026 17:27
@flutter-zl flutter-zl added the CICD Run CI/CD label Oct 21, 2026
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Oct 21, 2026

@mdebbar mdebbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for contributing this fix! LGTM!

The Linux customer_testing failure is unrelated (fixed on master by #194886); merging master should clear it.

@mdebbar mdebbar added the CICD Run CI/CD label Oct 21, 2026
@mdebbar
mdebbar requested a review from bkonyi October 21, 2026 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[web] Development server proxy buffers SSE and NDJSON responses

3 participants