Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsNo conflicts as of last run. |
There was a problem hiding this comment.
concept ACK
makes sense to update m_keep_alive in the same critical section as the send
buffer, since both are read together under the same lock (m_send_mutex). this
keeps the related state changes in sync and does not allow the I/O thread to
view an inconsistent state while the next response is being queued
edit: the motivation is fine but since this scenario is not easy to reach pragmatically,
i'd rather delay review
|
concept ACK |
hodlinator
left a comment
There was a problem hiding this comment.
Concept ACK dc8fb12
Seems more correct than on master, thanks for finding this!
Verified that the test fails when the implementation changes are reverted.
| // The state is shared between the test, the request handler and the mock | ||
| // socket. It is static because the local class below cannot capture | ||
| // automatic variables. |
There was a problem hiding this comment.
nit: More to the point?
| // The state is shared between the test, the request handler and the mock | |
| // socket. It is static because the local class below cannot capture | |
| // automatic variables. | |
| // The state is shared between the test, the request handler and the mock | |
| // socket. It is static since SocketTestingSetup::ConnectClient<T>() doesn't | |
| // allow for additional socket ctor args. |
There was a problem hiding this comment.
Taken as suggested, thanks. Your wording is more accurate: the real constraint is ConnectClient<T>(), not the local class.
| static std::atomic_bool second_request_dispatched{false}; | ||
| static std::atomic_bool io_thread_in_send{false}; | ||
| static std::atomic_bool allow_second_reply{false}; |
There was a problem hiding this comment.
nit: Could skip explicit initialization since std::atomic-ctor implicitly initializes to T()1/false2 and we overwrite the values immediately below anyway.
| static std::atomic_bool second_request_dispatched{false}; | |
| static std::atomic_bool io_thread_in_send{false}; | |
| static std::atomic_bool allow_second_reply{false}; | |
| static std::atomic_bool second_request_dispatched; | |
| static std::atomic_bool io_thread_in_send; | |
| static std::atomic_bool allow_second_reply; |
Footnotes
| char buf[0x10000] = {}; | ||
| auto WaitForReply = [&](const std::string& reply) { |
There was a problem hiding this comment.
nit: Might as well decrease scope of buf and skip initialization:
| char buf[0x10000] = {}; | |
| auto WaitForReply = [&](const std::string& reply) { | |
| auto WaitForReply = [&](const std::string& reply) { | |
| char buf[0x10000]; |
HTTPRemoteClient::Send() wrote m_keep_alive before taking m_send_mutex
and appending the response to m_send_buffer. The I/O thread decides in
MaybeSendBytesFromBuffer() (under m_send_mutex) whether to disconnect
by checking "buffer drained to empty && !m_keep_alive". Because the flag
and the buffer were not updated together, a pipelined client could lose
a response:
1. Response 1 (keep-alive) is partially sent; the I/O thread is still
draining it.
2. Request 2 ("Connection: close") is parsed from the receive buffer
and dispatched to a worker (the send throttle only holds requests
back once the buffer exceeds MAX_BODY_SIZE).
3. The worker clears m_keep_alive, then the I/O thread finishes
sending response 1, observes an empty buffer with
m_keep_alive == false and sets m_disconnect.
4. The worker appends response 2, which is dropped when the client is
disconnected on the next I/O loop iteration.
Move the assignment into the same critical section that appends the
response, and change the field from std::atomic_bool to a plain bool
annotated GUARDED_BY(m_send_mutex) so that Clang's thread-safety
analysis enforces the ordering. The only reader already holds
m_send_mutex.
Add a unit test that reproduces the interleaving deterministically with a
mock socket: it holds the first reply in the send buffer until the second
(pipelined, "Connection: close") request has been dispatched, then lets
the I/O thread flush it while the worker is about to write the second
reply. Without the fix the client is disconnected with the second reply
still in the buffer.
dc8fb12 to
b71c028
Compare
|
Pushed b71c028: addresses all three nits from @hodlinator (comment wording, dropped redundant Thanks for verifying that the test fails with the implementation changes reverted. |

HTTPRemoteClient::Send()setsm_keep_alivebefore takingm_send_mutexand appending the response to the send buffer, while the I/O thread reads it underm_send_mutexinMaybeSendBytesFromBuffer()right after the buffer drains to empty. The two updates are therefore not ordered with respect to each other, and a pipelining client can lose a response:Connection: close) is parsed from the receive buffer and dispatched to a worker (the send throttle from http: throttle send buffer when client stops draining #36174 only holds a request back once the buffer exceedsMAX_BODY_SIZE).m_keep_alive; the I/O thread then finishes sending response 1, sees an empty buffer withm_keep_alive == false, and setsm_disconnect.This PR moves the assignment into the critical section that appends the response, and turns the field from
std::atomic_boolinto a plainbool GUARDED_BY(m_send_mutex)so Clang's thread-safety analysis enforces the ordering (the only reader already holds the mutex). With the flag and the buffer updated together, the I/O thread can no longer observe "empty && !keep_alive" between two pipelined responses.The ordering dates back to the initial HTTP server implementation (#35182); #35829/#36007 only moved the code. The window is small and
bitcoin-clidoes not pipeline, so this is a low-severity fix.Test
http_pipelined_keepalive_close_testsreproduces the interleaving deterministically with a mock socket:Send()fails with EAGAIN until the second request has been dispatched, then the I/O thread's nextSend()(made while holdingm_send_mutex) signals the worker to write its reply, waits for it to reachHTTPRemoteClient::Send(), and only then flushes the first reply. Sends are refused again until the test releases them, so the second reply is only delivered if the I/O loop kept the connection open. Without the first commit the test fails withserver.GetConnectionsCount() == 1 has failed [0 != 1]: the client was disconnected with the second reply still in its send buffer.