Skip to content

fix: reset Http1Parser state in InitResponse to prevent false HTTP 200 on keep-alive reconnect#846

Merged
ithewei merged 2 commits into
masterfrom
copilot/fix-async-http-client-response
Jul 9, 2026
Merged

fix: reset Http1Parser state in InitResponse to prevent false HTTP 200 on keep-alive reconnect#846
ithewei merged 2 commits into
masterfrom
copilot/fix-async-http-client-response

Conversation

Copilot AI commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

On a reused keep-alive connection, if the peer closes without sending any bytes, AsyncHttpClient incorrectly delivers a default-constructed HTTP 200 empty response instead of triggering a retry or error. The built-in retry mechanism is bypassed entirely.

Root cause

Http1Parser::InitResponse() reset the underlying C parser and string buffers but not the state member. The previous response had set state = HP_MESSAGE_COMPLETE. On reconnect, IsEof()IsComplete() sees that stale value and returns true with zero bytes received, so onclose takes successCallback() with a freshly Reset() response — whose default status_code is HTTP_STATUS_OK.

InitRequest() (server-side) already had the correct reset; InitResponse() lacked symmetry.

Fix

 virtual int InitResponse(HttpResponse* res) {
     res->Reset();
     parsed = res;
     http_parser_init(&parser, HTTP_RESPONSE);
+    state = HP_START_REQ_OR_RES;
     url.clear();

This makes a zero-byte peer close correctly take the error/retry path.

Copilot AI changed the title [WIP] Fix misreported successful response on reused keep-alive connection fix: reset Http1Parser state in InitResponse to prevent false HTTP 200 on keep-alive reconnect Jul 9, 2026
Copilot AI requested a review from ithewei July 9, 2026 14:21
@ithewei ithewei marked this pull request as ready for review July 9, 2026 14:29
Copilot AI review requested due to automatic review settings July 9, 2026 14:29

Copilot AI 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.

Pull request overview

Fixes a client-side HTTP/1 parsing edge case on reused keep-alive connections where a zero-byte peer close could be misinterpreted as a completed response (defaulting to HTTP 200), bypassing the retry/error path. The change aligns InitResponse() (client-side) with InitRequest() (server-side) by fully resetting the parser state.

Changes:

  • Reset Http1Parser::state to HP_START_REQ_OR_RES inside InitResponse() after reinitializing the underlying C parser.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ithewei ithewei merged commit 872a312 into master Jul 9, 2026
11 of 13 checks passed
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.

AsyncHttpClient: connection closed with zero bytes on reused keep-alive connection is misreported as a successful HTTP 200 empty response

3 participants