Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/35780. ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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. |
1b34df5 to
06ae35c
Compare
06ae35c to
2f14e21
Compare
|
Is there any way to cover the new behavior with functional tests? For example, maybe in |
| } | ||
|
|
||
| void HTTPRequest::WriteReply(HTTPStatusCode status, std::span<const std::byte> reply_body) | ||
| void HTTPRequest::WriteReply(HTTPStatusCode status, std::span<const std::byte> reply_body, bool force_close) |
There was a problem hiding this comment.
I'm not sure why we need a flag force_close in addition to HTTPRemoteClient.m_disconnect?
There was a problem hiding this comment.
the force_close is about HTTP response, send connection:close and dont keep-alive
then for m_disconnect is for tear down this socket now .. so we need force_close first then m_disconnect later in order to tell client we are done flush the error reply drain a little bit then close so if we set m_disconnect immediately will close too early that will hit RST and hide the reply
| // Copy data from socket buffer to client receive buffer | ||
| client->m_recv_buffer.insert( | ||
| client->m_recv_buffer.end(), | ||
| buf, | ||
| buf + nrecv); |
There was a problem hiding this comment.
If we're just draining the socket and ignoring the data anyway, do we still need to copy it into memory at all? (And then clear it immediately?)
There was a problem hiding this comment.
i agree i will clean it up
| void HTTPServer::DisconnectClients() | ||
| { | ||
| const auto now{Now<SteadySeconds>()}; | ||
| const auto now_ms{Now<SteadyMilliseconds>()}; |
There was a problem hiding this comment.
I think if we need this level of accuracy here, we can just replace the seconds counter with ms and only use one. See #35182 (comment)
| * timeout and half-closes after flushing the reply; the receive path then | ||
| * drains to EOF. | ||
| */ | ||
| static void SendErrorReplyAndLingerClose(HTTPRequest& req, HTTPRemoteClient& client, HTTPStatusCode status) |
There was a problem hiding this comment.
I don't totally understand why we need a specific function for this, like a linger close is a special type of close? Why don't we just make it so every client close (for any reason) drains the incoming socket for up to 1 second first?
There was a problem hiding this comment.
well linger isnt needed for every close its useful when we reject a request while client is still sending huge body or bad request so if we close with unread data still sitting there then windows can RST and client never sees 400 or 413
| // took place because it also (might) parse a request we have already | ||
| // received and pass it to a worker thread. | ||
| MaybeDispatchRequestsFromClient(client); | ||
| if (!lingering_close) { |
There was a problem hiding this comment.
I don't think we need to guard this, since a lingering close happens in the middle of a request being processed already. This will be even better handled after #35735 when we won't even read from the socket if one request is being handled.
There was a problem hiding this comment.
i thinkk we still want the guard because once we are lingering that connection should only drain and go away not keep parsing or dispatching more work
in #35735 helps with one request at time less queue weirdnes but doesnt remove the need for if we are shutting this connection via linger doesnt treat it as a normal live client
i will wait for your PR to be merged first |
|
|
||
| // Disconnect this client due to error, end of communication, or idle timeout. | ||
| // Disconnect on error, EOF, idle timeout, or a stalled lingering close. | ||
| // May drop unsent data if we are closing due to error. |
There was a problem hiding this comment.
I think the motivation behind this PR is to be able to honestly remove this comment? Even in case of an error, we shouldn't drop the response for the client.
frankomosh
left a comment
There was a problem hiding this comment.
Concept ACK. Agree the goal here is worth solving
The try/excepts in check_excessive_request_size and check_chunked_transfer are the 413 cases, where the client is still uploading when we reject. Drain-until-EOF is meant for that, and on loopback it probably does make those reliable. I still wouldn't turn them into hard asserts — if the upload hasn't finished by the fallback timeout, that's a flake that only shows up on slow runners. The #35632 flake is the other way around: we reject at the headers and close with the body still unread, so Windows RST's and the 400 never arrives. check_whitespace_in_headers already asserts 400. |
There was a problem hiding this comment.
concept ACK
from #35780 (review) :
However I am not convinced about the approach. I also think this is a feature that would be easier to implement after #35735
fully agree with this comment
my rough idea of how i think it can be approached:
if rebased on #35735: once m_req->m_state transitions to an HTTPRequest::State::Error state, we know HTTP processing for that connection is all over. so, queue the 400/413, stop buffering or parsing further input, let the existing send path flush out the response, then half-close the send side and simply discard incoming data from the kernel buffer until EOF or an error or a timeout, giving well-behaved clients room to disconnect. in other words, the request state machine handles the HTTP side, and only minimal connection-level state is needed for the final TCP drain (kernel-side)
Wrap shutdown(2) (SHUT_WR, or SD_SEND on Windows) behind the Sock interface so it can be overridden by the mock sockets used in the unit and fuzz tests.
2f14e21 to
1e0e718
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
When the HTTP server rejects a request during parsing a malformed header or an oversized body, it replied and then immediately closed the connection. On Windows, closing a socket that still has unread inbound data can trigger a TCP RST, which discards the queued reply before the client reads it, so the client sees a connection reset instead of the 400/413 response (see bitcoin#35632). Instead of closing abruptly, flush the error reply, half-close the send side, and drain any remaining inbound data until the peer sends EOF. A 1s LINGERING_CLOSE_TIMEOUT bounds clients that never close their side.
1e0e718 to
b14649f
Compare
|
🐙 This pull request conflicts with the target branch and needs rebase. |
hodlinator
left a comment
There was a problem hiding this comment.
Concept ACK b14649f
Thanks for working on this! Since I'm to blame for the merge conflict, here's a suggestion for how to rebase: master...hodlinator:bitcoin:pr/35780_rebased
| err_msg = "unknown error"; | ||
| } | ||
| // Reply so the client doesn't hang waiting for the response. | ||
| req->WriteHeader("Connection", "close"); |
There was a problem hiding this comment.
Why not write this, seems fine to have the other side close on us?
Fixes bitcoin#35632 by allowing both outcomes of a race condition. The server behavior is unchanged: in response to a malformed request we send an error code and disconnect. The issue is that sometimes on Windows the RST is caught by the platform and the receive buffer is discarded before the Python client can process it with recv(). We can also be much more polite to misbehaving clients by implementing SO_LINGER as suggested in bitcoin#35780 but that will require more review. Reviewers can force the race condition to lose by withholding the HTTP response entirely: ```diff diff --git a/src/httpserver.cpp b/src/httpserver.cpp index 9bb8986..62324d3fea 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -1072,7 +1072,7 @@ std::unique_ptr<HTTPRequest> HTTPRemoteClient::TryReadRequest(const std::shared_ e.what()); // We failed to read a complete request from the buffer - WriteNoStoreErrorReply(*client->m_req, HTTP_BAD_REQUEST); + // WriteNoStoreErrorReply(*client->m_req, HTTP_BAD_REQUEST); client->m_disconnect = true; return nullptr; } ```
Fixes bitcoin#35632 by allowing both outcomes of a race condition. The server behavior is unchanged: in response to a malformed request we send an error code and disconnect. The issue is that sometimes on Windows the RST is caught by the platform and the receive buffer is discarded before the Python client can process it with recv(). We can also be much more polite to misbehaving clients by implementing SO_LINGER as suggested in bitcoin#35780 but that will require more review.
Fixes bitcoin#35632 by allowing both outcomes of a race condition. The server behavior is unchanged: in response to a malformed request we send an error code and disconnect. The issue is that sometimes on Windows the RST is caught by the platform and the receive buffer is discarded before the Python client can process it with recv(). We can also be much more polite to misbehaving clients by implementing SO_LINGER as suggested in bitcoin#35780 but that will require more review.
a51df9b test: tolerate race condition in interface_http.py (Matthew Zipkin) Pull request description: Fixes #35632 by allowing both outcomes of a race condition. The server behavior is unchanged: in response to a malformed request we send an error code and disconnect. The issue is that sometimes on Windows the RST is caught by the platform and the receive buffer is discarded before the Python client can process it with recv(). We can also be much more polite to misbehaving clients by implementing a lingering close using SO_LINGER as suggested in #35780 but that will require more review. The exact error in #35632 is hard to produce reliably but there are a few close options for reviewers. I tested this on windows native building with MSVC. In both of these cases the patch from this PR caught the error and passed the test. **RemoteDisconnected: Remote end closed connection without response** ```diff diff --git a/src/httpserver.cpp b/src/httpserver.cpp index 9bb8986..62324d3fea 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -1072,7 +1072,7 @@ std::unique_ptr<HTTPRequest> HTTPRemoteClient::TryReadRequest(const std::shared_ e.what()); // We failed to read a complete request from the buffer - WriteNoStoreErrorReply(*client->m_req, HTTP_BAD_REQUEST); + // WriteNoStoreErrorReply(*client->m_req, HTTP_BAD_REQUEST); client->m_disconnect = true; return nullptr; } ``` **ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host** ```diff diff --git a/src/httpserver.cpp b/src/httpserver.cpp index 9bb8986..be52acb874 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -1154,6 +1154,11 @@ bool HTTPRemoteClient::MaybeDisconnect(std::chrono::time_point<SteadyClock> now, "Disconnecting HTTP client %s (id=%llu)", m_origin, m_id); + auto sock{GetSock()}; + linger opt{}; + opt.l_onoff = 1; // enable SO_LINGER + opt.l_linger = 0; // zero timeout + sock->SetSockOpt(SOL_SOCKET, SO_LINGER, &opt, sizeof(opt)); return true; } ``` ACKs for top commit: jeanpablojp: re-ACK a51df9b winterrdog: tACK a51df9b janb84: re ACK a51df9b hodlinator: re-ACK a51df9b sedited: ACK a51df9b Tree-SHA512: a6244581b2b51af647452e0dc8cd09cdc8d975dee6a0dc8b8064cad136023dad68b4af987303bced91a662bf5fae22871ea718a6a8e68024158a9aef6c5855ef
|
We are going to close this for now. I don't think the extra review work required here is worth the feature. The flaky test has already been fixed with a different approach. Unless we are exposing a security vulnerability or seriously disrupting a legitimate user workflow we should probably pass. |
Fixes bitcoin#35632 by allowing both outcomes of a race condition. The server behavior is unchanged: in response to a malformed request we send an error code and disconnect. The issue is that sometimes on Windows the RST is caught by the platform and the receive buffer is discarded before the Python client can process it with recv(). We can also be much more polite to misbehaving clients by implementing SO_LINGER as suggested in bitcoin#35780 but that will require more review.
Based on this issue #35632 raised i believe the issue is not with whitespace header validation since the logic works fine coz the server detects the malformed request and queues a 400 reply as expected.
The problem is how the server closes the connection afterwards. It queues the reply and closes the socket right away sometimes before it has finished reading everything the client had sent.
So when socket is closed while there's still unread data sitting in the kernel's receive buffer, Windows responds with a TCP RST. That reset just throws away the reply that was already on its way out, so the client never gets to read the 400 and instead sees the connection aborted.
So my LLM Buddie and I realized that we should change how the server closes a connection after a parse/size error so the client always gets a chance to read the response first by queuing the
400/413 responsewithConnection: close.Then, waits until the response has actually left the send buffer. Then, Half-close only the write side (shutdown(SHUT_WR/SD_SEND)) so the client can still finish reading while the server stops sending. Afterwards, Keep draining whatever the client is still sending until it hits EOF or a socket error and only force the connection closed after a 1s fallback timeout, for a client that never finishes.