Skip to content

test: interface_http: Fix for case when a response doesn't arrive in one recv - #28

Closed
optout21 wants to merge 2 commits into
masterfrom
2610-interface-http-test-fix
Closed

optout21 wants to merge 2 commits into
masterfrom
2610-interface-http-test-fix

Conversation

@optout21

@optout21 optout21 commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

The interface_http.py test can fail in the -- very improbable -- case when network reads happen to arrive in smaller chunks (when reading HTTP response from the socket).

All networking code using sockets must not make assumption about the chunking of the incoming/outgoing data, and should be resilient against any chunking. Normally, the chunking of the sends of the writer dictate the chunking of the receives at the reader, but this is not guaranteed by IP/TCP, intermediate networking elements may change the packet structure. In test using sockets on the same host, this is not very likely, but still possible.

This tests reads complete HTTP responses, by checking for the HTTP response code texts ("HTTP/1.1 200"). In doing so, it implicitly assumes that one HTTP response arrives in one recv. There is no guarantee for this.

This was found and can be reproduced by using a reproducer patch, that forces all socket sends to use smaller chunking.
The reproducer patch for forced chunked send is here:

ssize_t Sock::Send(const void* data, size_t len, int flags) const

TODO add link to reproducer run

The issue is test-code only, the non-test code is correct.

The fix is to follow standard HTTP parsing: read the header until the new line, extract the content length from the header, and read the body of that size. Use repeated receives if needed, until the full response is read.

This fix makes the test more robust, though a failure due to this issue in CI is very unlikely, but not impossible.

…one recv

Several checks read a response with one recv() call (recv_raw(), up to
1024 bytes) and assumed it held the whole HTTP response. TCP does not
guarantee this: on a slow or loaded machine a response can arrive split
across several reads, so the check sees only the status line or headers
and fails.

The loops in check_pipelining and check_slow_read_throttle had a related
problem. They counted "HTTP/1.1 200" markers in each read separately, so
a marker split across two reads was never counted, and the loop waited
until the socket timed out.

Add BitcoinHTTPConnection.recv_responses(count), which keeps reading
until `count` complete responses (headers plus Content-Length body) have
arrived, or the server closes the connection. Use it everywhere a full
response is expected, and replace the counting loops with explicit
assertions on the result.

recv_raw() is kept for the timeout checks, where a single blocking read
is the intended behaviour.
@optout21
optout21 force-pushed the 2610-interface-http-test-fix branch from 0a2e86b to 5a88082 Compare November 2, 2026 11:15
@optout21 optout21 changed the title test: interface_http: Fix failure in case of smaller chunked reads test: interface_http: Fix for case when a response doesn't arrive in one recv Nov 2, 2026
@optout21

optout21 commented Nov 2, 2026

Copy link
Copy Markdown
Owner Author

--> bitcoin#36729

@optout21 optout21 closed this Nov 2, 2026
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.

1 participant