mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-05 06:23:39 +00:00
http: update m_keep_alive under m_send_mutex #36259
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36259
Loading…
Reference in a new issue
No description provided.
Delete branch "http-keepalive-under-send-mutex"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 #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.The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.Conflicts
No conflicts as of last run.
concept ACK
makes sense to update
m_keep_alivein the same critical section as the sendbuffer, since both are read together under the same lock (
m_send_mutex). thiskeeps 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
🔵 Suggestion · The race regression test can miss its triggering interleaving
If the second worker is delayed beyond the 200 ms window, the test can pass with the original bug restored despite describing the reproduction as deterministic.
Location:
src/test/httpserver_tests.cpp:1120(head).🔵 Suggestion · Ensure server teardown runs after fatal test assertions
httpserver_tests.cpp:1185: a failed BOOST_REQUIRE after StartSocketsThreads() unwinds past the tail teardown. ~HTTPServer only Assume()s the thread was joined, so the joinable std::thread member calls std::terminate. The failure is logged first; the existing test at line 1050 has the same pattern.
Location:
src/test/httpserver_tests.cpp:1184(head).Review coverage was incomplete.
Reviewed:
b71c0288f237· Full review report · Previous reportspermalink
concept ACK
seems reasonable, but low priority for review
Concept ACK
dc8fb12509Seems more correct than on master, thanks for finding this!
Verified that the test fails when the implementation changes are reverted.
nit: More to the point?
nit: Could skip explicit initialization since
std::atomic-ctor implicitly initializes toT()1/false2 and we overwrite the values immediately below anyway.https://en.cppreference.com/cpp/atomic/atomic/atomic ↩︎
https://stackoverflow.com/questions/3308028/default-constructor-value-for-bool-type ↩︎
nit: Might as well decrease scope of
bufand skip initialization:Taken as suggested, thanks. Your wording is more accurate: the real constraint is
ConnectClient<T>(), not the local class.Done, thanks.
Done, thanks.
Pushed
b71c0288f2: addresses all three nits from @hodlinator (comment wording, dropped redundant{false}initializers, movedbufinto the lambda). Test-only changes, no change to the fix itself.Thanks for verifying that the test fails with the implementation changes reverted.