http/client: handle early server response during streaming body write#3410
Open
levenon wants to merge 1 commit into
Open
http/client: handle early server response during streaming body write#3410levenon wants to merge 1 commit into
levenon wants to merge 1 commit into
Conversation
When using body_writer for streaming uploads (e.g., chunked transfer), the server may send an error response and close the connection before the client finishes writing the body. Previously, do_make_request() serialized write_body and recv_reply, meaning the client could not detect the server's early response until write_body completed — which may never happen for long-lived streams, as writes continue to succeed into kernel/userspace buffers even after the remote end has closed. Fix this by running write_body and recv_reply concurrently via when_all(). If recv_reply completes first (server responded early), shutdown_output() is called to immediately abort the in-progress write. If write_body completes first (normal flow), recv_reply proceeds as before without shutdown. This resolves a hang where streaming HTTP clients would remain stuck in write_body indefinitely after the server had already rejected the request.
xemul
reviewed
May 29, 2026
| return _write_buf.flush().then([this] { | ||
| return recv_reply(); | ||
|
|
||
| return do_with(false, [this, &req](bool& write_done) { |
Contributor
There was a problem hiding this comment.
For short bodies (those without body writer function set) this can be avoided
xemul
reviewed
May 29, 2026
| } | ||
| return make_ready_future<reply_ptr>(std::move(rep)); | ||
| }) | ||
| ).then([](auto joined) { |
Contributor
There was a problem hiding this comment.
Good fit for .then_unpack() sugar
Contributor
|
Test that reproduces the issue is needed badly |
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 using body_writer for streaming uploads (e.g., chunked transfer), the server may send an error response and close the connection before the client finishes writing the body. Previously, do_make_request() serialized write_body and recv_reply, meaning the client could not detect the server's early response until write_body completed — which may never happen for long-lived streams, as writes continue to succeed into kernel/userspace buffers even after the remote end has closed.
Fix this by running write_body and recv_reply concurrently via when_all(). If recv_reply completes first (server responded early), shutdown_output() is called to immediately abort the in-progress write. If write_body completes first (normal flow), recv_reply proceeds as before without shutdown.
This resolves a hang where streaming HTTP clients would remain stuck in write_body indefinitely after the server had already rejected the request.