mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:47:36 +00:00
http: Make HTTPRequest update state internally #36124
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!36124
Loading…
Reference in a new issue
No description provided.
Delete branch "2026/08/http_refactors"
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?
Issues in order of importance
#35829 made the fields of
HTTPRequest&HTTPRemoteClientprivate, but still allows anyone with a reference to a request to update the internal stateenum. https://github.com/bitcoin/bitcoin/pull/35829#discussion_r3860447518HTTPRemoteClient::TryReadRequest()only needs to be astaticmethod in order to forward ashared_ptrof a client to a new request.Commits solving these issues in same order
HTTPRequest::SetState()and introduceHTTPRequest::Load()- Removes the ability for other classes to directly modify the request state.HTTPRemoteClient::TryReadRequestInternal()from a combination ofHTTPRemoteClient::TryReadRequest()andHTTPRemoteClient::ReadRequest()(the latter is removed)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
I ran the flattened
ShouldDisconnect()against the version in master over every combination of the flags it reads, with the client inside and outside the idle window and the timeout on and off. Same return value in all 64. The only difference is that master logs the idle timeout even for a client that is already flagged for disconnect.The unit tests run
ShouldDisconnect()but assert nothing about its first two branches. I broke each one and the suite stayed green both times.interface_http.pydoes reach both, but it blocks on the socket instead of failing. The test below covers both branches, and the new log line with them.test for both branches and the new log line
Concept ACK
do you think some runtime safety like
assert(false)(in debug builds) would be useful here ?e.g. similar to how it's done:
github.com/bitcoin/bitcoin@dc0395c585/src/index/blockfilterindex.cpp (L64-L71)or here:
github.com/bitcoin/bitcoin@dc0395c585/src/bitcoin-cli.cpp (L430-L445)Thanks! Added the checks in slightly extended form.
concept ACK 907d7342b60dfa00ad81e235eccb5808b9c13da6
The removal of the
SetState()makes it a better design (imho), makes the convention based "rule" not to arbitrary set the state, to a a compiler-enforced factFound a few possible NITS.
NIT: would it not help the readability to move this function closer to the other load* helpers ? ~ L 378
NIT. On error it does not return
true. Why not make it void? non-test code already checksgetstate()and doing the same in the tests makes the test slightly more strict. The bool loosy communicates 5 states in a boolnit: this comment still refers to
LoadControlData/LoadHeaders/LoadBodyas the call sequence above, but those were replaced by a singleLoad()call. since the helpers are private now too, it is not immediately clear to a reader where this logic comes fromThanks for the feedback so far! Pushes after 907d7342b60dfa00ad81e235eccb5808b9c13da6 address some nits and expands
http_should_disconnect_teststo more completely verify optimistic send behavior.re https://github.com/bitcoin/bitcoin/pull/36124#discussion_r3915112397:
The order of methods in httpserver.cpp is wildly out of sync with the header. I prefer to punt on this for now, to keep the number of commits down.
re https://github.com/bitcoin/bitcoin/pull/36124#discussion_r3915573840:
Removed docstring for now as I switched to returning state which seems self-explanatory.
Decided to split out two PRs from this one as they are quite independent. See #36159 and #36160.
Added co-authorship in #36159.
(re) ACK c43d544b16b61e3182467dba7355cdaa372204c6
returning the state and not a bool at the
load()function, is the better way to go imho :) Have re-reviewed the PR because of the split-off; build tested etc.In the commit message, is an API change a behaviour change? super micro nit.
The
load()documentation block has no @return anymore, is state super clear to everyone ?had not noticed this. i think we can add the
@returnwith some lightdocumentation.
Stateis self-explanatory as a type, but it does not tell a caller thatLoad()never returnsError(it throws instead), or that the other valuesmean "keep calling with more data" vs "done", sth that is specific to
Load()'s contract. secondly, it keeps consistency with the@throwsbelowand the Doxygen style used elsewhere
Latest push modifies the initial
Load()commit only.re https://github.com/bitcoin/bitcoin/pull/36124#pullrequestreview-5102409506:
Good point, fixed.
re https://github.com/bitcoin/bitcoin/pull/36124#pullrequestreview-5102409506 + https://github.com/bitcoin/bitcoin/pull/36124#issuecomment-5554537521:
Added back
@returns. It is true thatLoad()(re-)throws the first time it setsError, but subsequent calls will returnError. The return value is only read in unit tests, so not sure if it's worth elaborating further in the doc-string.tACK
62796e659fsuccessfully built and tested on this toolchain: Debian/
clang++-18/x86_64🐙 This pull request conflicts with the target branch and needs rebase.
🐙 This pull request conflicts with the target branch and needs rebase.
🐙 This pull request conflicts with the target branch and needs rebase.
I found no actionable issues in this static review.
Previous findings
Resolved findings
[Minor · resolved] A complete request can remain undispatched after the receive buffer empties
The early return at line 1101 skips the completion and dispatch check, not just parsing. In the base version,
ReadRequest()returned early on an empty receive buffer, butTryReadRequest()still checked whetherm_reqwas complete and dispatched it once the send buffer fell toMAX_BODY_SIZEor below.This occurs when a pipelined request finishes parsing while earlier responses leave more than
MAX_BODY_SIZE(32 MiB) in the send buffer, and parsing consumes the entire receive buffer. Line 1153 then retains the complete request inm_req. Once the send buffer drains, later calls returnnullptrat line 1101.GenerateWaitSocketspolls only for receives for this client, so the request remains unanswered until the client sends more bytes or the idle timeout closes the connection (m_req_busyis false).Skip only the load/erase step when the buffer is empty, for example by initializing
req_statefromm_req->GetState(), so the throttle check still runs on every iteration. Add a unit test that holds a complete request under send pressure, drains the send buffer, and expects dispatch without new input.No checked-in client that pipelines a request behind a response larger than 32 MiB was identified, so production impact is unconfirmed.
Location:
src/httpserver.cpp:1101(head).Previous reports of the same concern:
c84da5e53aa1661a89d522667e7f05c6f6f88a3b. Last checked at6a5212fb97c2c332fc3cb901f8e9a110622ef841. This is the same mechanism and consequence as 890:2:finding:1. An empty-buffer early return skipped dispatch of a complete request held back by send backpressure, so the request stayed unanswered. At head, the empty-buffer guard covers only the Load/erase block (cpp:1102-1116), and the Complete-state check and throttle run on every call (cpp:1143-1152). Both records are fixed by the same change.656ddb44ba0db001d503cce52db5ac6e6db897f1. Last checked at6a5212fb97c2c332fc3cb901f8e9a110622ef841. The reported regression was an earlyreturn nullptron an empty receive buffer, placed before the Complete/throttle dispatch check. At head, the!m_recv_buffer.empty()guard wraps only the Load/erase step (cpp:1102-1116). Control always reachesm_req->GetState() == HTTPRequest::State::Completeand theMAX_BODY_SIZEthrottle (cpp:1143-1152).SocketHandlerConnectedcallsTryReadRequest()for every client on every iteration (cpp:919-923). A complete request held back by send backpressure is therefore dispatched on a later iteration once the send buffer drains, with no new input needed. This matches base, whereReadRequest()'s empty-buffer return did not skip the caller's state check.Reviewed:
6a5212fb97c2· Full review report · Previous reportsmostly a concern..
on a 2nd look, i have started to get some reservations about folding
ReadRequestintoTryReadRequestInternal. was it mainly removed to simplify things, or was there a problem with keeping it separate ?ReadRequestgave us one place to clean upm_recv_bufferafter a failed parse. now, eachcatchblock has to remember to do that itself, so it is easy for someone adding a new error case later to forget. keeping the cleanup in one place felt like a nice and easy safety netunless there is a reason i am missing, i would lean towards keeping a thin
ReadRequestjust for the cleanup. it gives us one less thing to worry about when adding new error cases. open to be convinced otherwise. i am mostly trying to understand the direction you want to goCould potentially add an inner
try/catch-block:Would prefer doing that over bringing back
HTTPRemoteClient::ReadRequest(). WDYT?i think that will do. let's run with it
i had 2 alternatives, but both introduced a helper, which i understand you would rather avoid here
also, i think the
readercan be scoped to the outertry-catchinstead of function scope, since that is the only scope it is used in, thus ending up with sth like this:Nice! Narrowed the scope of
readerin latest push.reACK
656ddb44baLGTM!
concept ACK
"Ralph" caught something that I think needs to be addressed: https://review.fish.foo/traces/36124-656ddb44ba0db001d503cce52db5ac6e6db897f1-890-2.html
Edge case:
m_send_bufferis full (for some reason), som_reqgets held back (throttled) and is not dispatched to a workerm_recv_bufferis empty because client sent one request and its now inm_reqm_reqnever(*) gets dispatched, even after the send buffer drains:(*) = if the client sends more data it will unclog the jam because then
m_recv_bufferwon't be empty anymore.Suggested fix from Claude: skip only the parsing when the buffer is empty, and start req_state from the request's current state. This way a
Completerequest still gets dispatched to a worker.b6deeac911nit: why remove the doxygen param?
Thank you @pinheadmz & Ralph for catching the accidental behavior change/bug. Applied suggestion, making the last commit more of a pure refactor.
Was keeping it for the now-private
Load*()methods. In the latest push I've instead moved it to the public method and rephrased it to be more informative instead of describing theLineReadertype.doxygen nit, totally ignorable
Turns out I had the grouping syntax wrong. Groups need
@nameto work, otherwise the description is only attached to the first item.fixed:
ACK
148210fe99Clean refactor, technically a little granular unit test coverage lost from private-izing internal methods but it makes for cleaner classes.
Show Signature
pinheadmz's public key is on openpgp.org
inline comment thread in response to @pinheadmz https://github.com/bitcoin/bitcoin/pull/36124#issuecomment-6474351472:
Found a couple of more groupings:
Not entirely happy with how these fields are separated from the others, see screenshot:

But if you agree I can push that commit on top.
Too picky for me ;-) I think future devs are more likely to read the .cpp file than the doxygen html anyway so as long as it looks ok in the code, i'm good with it. Happy to re-ack if you decide to change it though.
Since you offered to re-ACK, I pushed the change. People are bound to consume the comments more through code as you say, rather than through this specific doxygen layout, so it feels more complete for it to be correct.
ACK
6a5212fb97Only doxygen changes since last review, thanks for cleaning that all up <3
Show Signature
pinheadmz's public key is on openpgp.org
Post-merge ACK
6a5212fb97