Skip to content

http: do not process requests pipelined behind "Connection: close" - #36355

Closed
azuchi wants to merge 1 commit into
bitcoin:masterfrom
azuchi:http-no-dispatch-after-close
Closed

azuchi wants to merge 1 commit into
bitcoin:masterfrom
azuchi:http-no-dispatch-after-close

Conversation

@azuchi

@azuchi azuchi commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

After the reply to a Connection: close request has been queued, TryReadRequest() still parses whatever is left in the receive buffer and hands it to a worker (it only checks m_req_busy), so a request pipelined behind the Connection: close request is executed. Once the reply has been flushed, MaybeSendBytesFromBuffer() flags the client for disconnection and the client is removed at the end of the I/O loop iteration, so the reply to the pipelined request is dropped (m_client.lock() fails in WriteReply()). If the closing reply is not flushed right away (large reply, slow client), the pipelined request is even answered and, since the last reply decides m_keep_alive, a keep-alive request pipelined behind the close request keeps the connection open.

RFC 9112 section 9.6 requires a server that sends a close response not to process any further requests on that connection. A client that relies on this and resends the request executes it twice, which matters for RPCs with side effects. The libevent based server freed the connection right after writing the close reply (evhttp_send_done() → evhttp_connection_free()), discarding any pipelined bytes, so this is a regression from #35182. It requires an authenticated client that pipelines behind a Connection: close request, so it is a robustness issue rather than a security one.

The fix records in the client that a closing reply has been queued (m_closing, set in Send() when the reply is not keep-alive) and makes TryReadRequest() stop reading from such a client, as well as from a client already flagged for disconnection (m_disconnect, which also covers permanent send and receive errors). The state machine unit test relied on reading a request pipelined behind an HTTP/1.0 reply without keep-alive; that request is now keep-alive, and two cases are added: a closing reply that is flushed by the optimistic send, and one that stays queued because the socket does not accept data.

The functional test pipelines a setnetworkactive false request behind a blocking waitforblockheight request that carries Connection: close, generates a block, and checks that the server closed the connection right after the first reply, that only one request from that connection was handed to a worker (the I/O thread logs every request it hands over, with the client's address and port, before it closes the connection), and that the network is still active.

TryReadRequest() only checked m_req_busy, so after the reply to a
"Connection: close" request had been queued it still parsed what was
left in the receive buffer and handed it to a worker. The pipelined
request was executed, and its reply was dropped once the closing reply
had been flushed and the client disconnected. A client that resends it,
as RFC 9112 section 9.6 allows, executes it twice. The libevent based
server freed the connection right after writing the close reply, so
this is a regression from the HTTP server rewrite.

Record that a closing reply has been queued and stop reading requests
from such a client, as well as from one flagged for disconnection.
Adjust the state machine unit test, which relied on reading a request
behind an HTTP/1.0 reply, and add unit and functional tests.
@DrahtBot

DrahtBot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/36355.

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK pinheadmz

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36259 (http: update m_keep_alive under m_send_mutex by azuchi)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@azuchi

azuchi commented Sep 27, 2026

Copy link
Copy Markdown
Contributor Author

The CI failure in "Windows, test cross-built" looks unrelated to this PR: https://github.com/bitcoin/bitcoin/actions/runs/36297704387/job/108561658571

The only failing test is wallet_send.py, in test_maxfeerate (added in #29278):

File "wallet_send.py", line 225, in test_maxfeerate
    self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), amount=1, fee_rate=Decimal("1.009"))
test_framework.util.JSONRPCException: Fee rate exceeds maximum configured by user (maxfeerate) (-6)

This PR only touches the HTTP server and its tests, and all other jobs passed. I could not reproduce the failure locally (12 runs of wallet_send.py on the same commit passed), so it seems to be intermittent.

My guess at the cause, not verified: the test sends with a fee rate exactly equal to -maxfeerate. The fee is computed from the estimated maximum signed size, while the limit is checked against the vsize of the signed transaction, so a signature that is one byte shorter than estimated can push the fee just above the limit.

@pinheadmz

Copy link
Copy Markdown
Member

concept ACK, have not reviewed the code but this behavior has been reported by red team as well. Seems reasonable to fix but not a high priority. I don't think there is a security issue here, if anything the current server might just be more accommodating / flexible to misbehaving clients.

@pinheadmz

Copy link
Copy Markdown
Member

Closing this for now. We only need to implement a subset of the HTTP protocol for our purposes and unless a server behavior is a security vulnerability or seriously disrupts a legitimate user workflow, we are going to kindly pass on the submission.

@pinheadmz pinheadmz closed this Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants