Repository navigation
Conversation
We use TCP backpressure to throttle incoming data from the client. We release the throttle when there is an empty receive buffer and no request in the progress of being parsed, but we should continue to throttle if there is a "busy" request being handled by a worker thread. On some platforms this would allow up to 65kB of additional data to trickle in after the throttle was supposedly engaged.
|
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/36276. ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste LLM Linter (✨ experimental)Possible places where comparison-specific test macros should replace generic comparisons:
2026-09-16 19:22:28 |
hodlinator
left a comment
There was a problem hiding this comment.
Concept ACK 0066120
Will have a closer look soon.
| const HTTPRequest* GetRequest() const { return m_req.get(); } | ||
|
|
||
| //! True while a request from this client is being processed by a worker thread. | ||
| bool IsRequestBusy() const { return m_req_busy.load(); } |
There was a problem hiding this comment.
nanonit: Wish we had a more descriptive name... something closer to:
| bool IsRequestBusy() const { return m_req_busy.load(); } | |
| bool WorkerHasRequest() const { return m_req_busy.load(); } |
The flood loop uses a non-blocking socket to detect when the server stops reading from the connection. When the server stops reading, the TCP receive buffer fills up and send() is expected to fail immediately with "try again" (EAGAIN/BlockingIOError). On macOS 15, when the buffer fills completely to zero, the kernel can block inside send() waiting for the remote side to free up space, instead of returning an error like it should. This causes the test to hang when run alongside other tests that put the system under load. Fix: call select() with a short timeout before each send(). select() asks the kernel whether there is space to write, and always returns within the timeout. If there is no space, we never call send() at all, so the kernel never gets the chance to block.
|
Added a second commit to handle this CI failure where macos-15 just hangs forever. My agents and I think what is happening is that macos is blocking inside of |
|
Converting to draft, i think i have this issue again where 50ms is being added to every RPC call... |
|
Sorry for the dump, if you understand the issue in the PR description then this is not helpful so I'll put this inside of a details block: I had a bit of trouble following what the issue is here, so I just want to check my understanding:Lines 1034 to 1039 in b821585
Lines 171 to 173 in b821585 Lines 188 to 190 in b821585
This is the bug since it means that the client can send the server more data (up to the server's TCP receive window limit which is usually ~64KiB or so). I think my understanding up to here is right, but I'm a little bit confused about why the functional test was failing, and how it relates to the zero-window probing, but here's what I gather:
On MacOS, the zero-window probe timer happens to be ~5 seconds, so about ~5 seconds into the test, the zero-window probe is performed by the client and it learns that it can send another window of bytes, resetting the bitcoin/test/functional/interface_http.py Lines 767 to 777 in b821585 which we were waiting for to hit 5 seconds: bitcoin/test/functional/interface_http.py Lines 756 to 758 in b821585 But the test times out after 10 seconds: bitcoin/test/functional/interface_http.py Lines 759 to 761 in b821585 So in some cases there won't be enough time left for the stall to hit 5 seconds, and that's why the test is failing. And the fix here is to just check if |
winterrdog
left a comment
There was a problem hiding this comment.
approach ACK
in order to keep request processing strictly serial, as intended in #35735 (comment), it makes sense to stop pipelining requests or reading requests ahead while one is still in flight
reply-to: #36276 (comment)
i think i have this issue again where 50ms is being added to every RPC call...
i might be missing something here, but where do you think the 50ms would be coming from in the RPC path ? i am trying to understand how it could end up affecting each RPC call
| conn.conn.sock.setblocking(False) | ||
|
|
||
| # Kernel socket buffer sizes vary widely across platforms, | ||
| # so we can't rely on counting sent() bytes to determine if the | ||
| # server is actually draining its end of the socket. | ||
| # When the server is busy, a continuous flood from the client SHOULD, | ||
| # at some point, stall indefinitely. An unpatched server will continue | ||
| # to accept data from the socket, at some rate, indefinitely. | ||
|
|
||
| # If send() is blocked for this many seconds, we assume the server | ||
| # is behaving correctly. | ||
| STALL_TIMEOUT = 5 | ||
| # If send() continues to progress for this many seconds, we assume | ||
| # the server is vulnerable to memory exhaustion. | ||
| PROGRESS_TIMEOUT = 10 | ||
|
|
||
| sent = 0 | ||
| stuck_since = None | ||
| start = time.monotonic() | ||
| while True: | ||
| try: | ||
| sent += conn.conn.sock.send(flood[sent % len(flood):]) | ||
| # Progress: the server is still reading | ||
| stuck_since = None | ||
| self.log.debug(f"sent: {sent}") | ||
| assert sent <= len(flood) * 10, ( | ||
| f"Server accepted {sent} bytes of pipelined data while a " | ||
| "request was still in flight: the receive buffer is not throttled") | ||
| except BlockingIOError: | ||
| # The kernel send buffer is full (EAGAIN). | ||
| # That's good, but we still need to determine if we are | ||
| # feeling backpressure from the server or the client-side buffer. | ||
| # Use select() to test writability before calling send(). On macOS, | ||
| # when the TCP receive window reaches zero the kernel can block inside | ||
| # send() doing zero-window probing even on a non-blocking socket, | ||
| # preventing BlockingIOError from ever being raised. select() always | ||
| # honors its timeout regardless of TCP window state. | ||
| _, writable, _ = select.select([], [conn.conn.sock], [], 0.05) | ||
| if not writable: | ||
| # Socket not writable within timeout: kernel send buffer is full. | ||
| if stuck_since is None: | ||
| stuck_since = time.monotonic() | ||
| elif time.monotonic() - stuck_since > STALL_TIMEOUT: | ||
| # No progress: the server has stopped reading. | ||
| break | ||
| else: | ||
| try: | ||
| sent += conn.conn.sock.send(flood[sent % len(flood):]) | ||
| # Progress: the server is still reading. | ||
| stuck_since = None | ||
| self.log.debug(f"sent: {sent}") | ||
| assert sent <= len(flood) * 10, ( | ||
| f"Server accepted {sent} bytes of pipelined data while a " | ||
| "request was still in flight: the receive buffer is not throttled") | ||
| except BlockingIOError: | ||
| # Rare race between select() and send(); treat as not writable. | ||
| if stuck_since is None: | ||
| stuck_since = time.monotonic() | ||
| elif time.monotonic() - stuck_since > STALL_TIMEOUT: | ||
| break | ||
| if stuck_since is None and time.monotonic() - start > PROGRESS_TIMEOUT: | ||
| # Continuous progress: the server is still draining the | ||
| # receive buffer while a request is in flight. | ||
| raise AssertionError( | ||
| f"Server kept reading pipelined data ({sent} bytes) while a " | ||
| f"request was still in flight for {PROGRESS_TIMEOUT}s.") | ||
| time.sleep(0.05) | ||
|
|
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
|
@winterrdog The 50ms is the select timeout which pauses the IO loop when there are no events. Since this patch is reducing events it forces many operations to wait a loop before being executed. I have a second commit locally that calls TryReadRequest from the worker threads (with a lock) to keep the pipeline moving, but after banging on this a few more hours today (in order to respond properly to @davidgumberg) I'm starting to think the issue really is just a flaky test. The bug I'm chasing here is pretty minimal and I think the approach in this PR so far is going to cost more than it's worth. I've been running the test through wireshark and watching the actual packets. The zero window probe is happening but the real macOS behavior is much more sinister: as the TCP window gets smaller, the kernel delays transport. It'll even raise a socket blocking error, wait five seconds, then send more data. |
|
Benchmarked keep-alive RPC latency on Linux (regtest, Python Compared master Single connection, n=200 after 20 warmup:
One full The cost is on the next request, not the response. I think what this needs is a wakeup so clearing For the test: @winterrdog's socket timeout stops the hang, but if the kernel later lets @davidgumberg re 4b: the stack can advertise a reopened window with an update ACK, not only a probe. @pinheadmz's captures show the client delaying as the window shrinks. Sender-side SWS avoidance (RFC 1122 §4.2.3.4) fits. ~5s progress vs 5s |
|
moving to #36303 |
Closes #36216
Follow up to #36123, which implemented a read-side throttle so the server won't drain the socket buffer into application memory without bound. There was a tiny window left unaddressed which allowed one additional read operation of up to 65kB from the socket buffer in rare cases, correctly failing the test.
The original throttle works by only registering socket
recvevents when EITHER:m_req != nullptr)m_recv_bufferis empty.The idea is "don't read from the socket unless we need to, in order to complete an incoming request".
The new rule applied in this PR is "if a request is already busy in a worker thread, don't read at all".
The test sends a blocking HTTP request followed by a flood of data, expecting that flood to be blocked at some point within a few seconds by TCP backpressure: the application stops reading from the socket and then the kernel stops reading from the client (
BlockingIOErrorin the python test).The TCP spec requires clients to periodically probe the server to learn when the window has been reopened:
https://www.rfc-editor.org/rfc/rfc9293.html#name-zero-window-probing
Because of the bug in the throttle logic from #36123, this probe would discover a non-zero window and send more data, resetting the stall timer in the test. This may repeat until the socket buffer is full again (the kernel replaced the 65kB the application foolishly drained) at which point the stream is truly blocked.
On linux platforms this probe is sent around 200ms, so even after a few cycles the server throttle would still register as solid to the test.
On macos however, given the small size of the open window, the probe is delayed 5 seconds. The cycle repeating every 5 seconds breaks the assumptions in the test and is interpreted as no throttle at all.
Note that the worst-case scenario for this bug is only 65kB so this is not a critical OOM fix.
I spent a few days with GPT, kimi, Sonnet and Opus trying to improve the test, or make it fail more reliably on master with no satisfying outcome. Tweaking the
STALL_TIMEOUTconstant, for example, either makes the test less sensitive to regression or more likely to fail on macos. I think we are at the limit of what we can test from the client side. We could add more DebugLog messages to the server and test server-side behavior a bit more like the other throttle testcheck_slow_read_throttle()but I chose to leave that alone for now.