http: Log bytes received from client, use to replace receive-throttle regression test #36303

Manually merged
pinheadmz merged 2 commits from http-log-recv-test-throttle into master 2026-10-05 21:55:56 +00:00 AGit
pinheadmz commented 2026-09-20 16:18:10 +00:00 (Migrated from github.com)

Closes #36216

Improves and fixes the test introduced by #36123 to cover regressions in the server's receive throttle. The original test relied on client-side signals to assert server-side behavior which can easily be disrupted by a platform's TCP stack. Macos for example may suddenly re-open a TCP window size based on heuristics out of our control (SB_AUTOSIZE), despite the application not reading any data from the socket. Macos also may delay transmission of data between 5-60 seconds as the TCP window shrinks (TCPTV_PERSMIN / TCPT_PERSIST / "silly window syndrome" avoidance). All these behaviors were observed by running the current CI test on my fork hundreds of times, capturing TCP packets and processing them locally against the CI debug log.

The improved approach is similar to how we test the send-side throttle in #36174 using debug log messages. This way we test the thing we know we can control (bitcoind).

To introduce the regression patched by #36123 and fail the new test:

diff --git a/src/httpserver.cpp b/src/httpserver.cpp
--- a/src/httpserver.cpp
+++ b/src/httpserver.cpp
@@ -1040,13 +1040,7 @@ HTTPServer::IOReadiness HTTPServer::GenerateWaitSockets() const
         // before the next is taken and they stay separate critical sections.
         // Holding m_sock_mutex while acquiring m_send_mutex would invert that
         // order and risk a lock-order-inversion deadlock.
-        Sock::Event event{0};
-        if (http_client->ReadyToSend()) {
-            event = Sock::SendEvent;
-        } else if (http_client->GetRequest() != nullptr || http_client->ReceiveBufferEmpty()) {
-            // Mid-parse (need more bytes) or buffer empty.
-            event = Sock::RecvEvent;
-        }
+        Sock::Event event = (http_client->ReadyToSend() ? Sock::SendEvent : Sock::RecvEvent);
 
         io_readiness.events_per_sock.emplace(sock, Sock::Events{event});
         io_readiness.httpclients_per_sock.emplace(sock, http_client);

For usual bitcoind activity, httpo debug logs will only grow by one extra line per request. Huge requests like those generated by the test may produce a hundred or so lines of "Received data" messages. These are optional debug lines in a worst-case scenario, but reviewers can discuss rate-limiting that log activity if we're afraid it's too much.

Closes #36216 Improves and fixes the test introduced by #36123 to cover regressions in the server's receive throttle. The original test relied on client-side signals to assert server-side behavior which can easily be disrupted by a platform's TCP stack. Macos for example may suddenly re-open a TCP window size based on heuristics out of our control (`SB_AUTOSIZE`), despite the application not reading any data from the socket. Macos also may delay transmission of data between 5-60 seconds as the TCP window shrinks (`TCPTV_PERSMIN` / `TCPT_PERSIST` / "silly window syndrome" avoidance). All these behaviors were observed by running the current CI test on my fork hundreds of times, capturing TCP packets and processing them locally against the CI debug log. The improved approach is similar to how we test the send-side throttle in #36174 using debug log messages. This way we test the thing we know we can control (bitcoind). To introduce the regression patched by #36123 and fail the new test: ```diff diff --git a/src/httpserver.cpp b/src/httpserver.cpp --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -1040,13 +1040,7 @@ HTTPServer::IOReadiness HTTPServer::GenerateWaitSockets() const // before the next is taken and they stay separate critical sections. // Holding m_sock_mutex while acquiring m_send_mutex would invert that // order and risk a lock-order-inversion deadlock. - Sock::Event event{0}; - if (http_client->ReadyToSend()) { - event = Sock::SendEvent; - } else if (http_client->GetRequest() != nullptr || http_client->ReceiveBufferEmpty()) { - // Mid-parse (need more bytes) or buffer empty. - event = Sock::RecvEvent; - } + Sock::Event event = (http_client->ReadyToSend() ? Sock::SendEvent : Sock::RecvEvent); io_readiness.events_per_sock.emplace(sock, Sock::Events{event}); io_readiness.httpclients_per_sock.emplace(sock, http_client); ``` For usual bitcoind activity, httpo debug logs will only grow by one extra line per request. Huge requests like those generated by the test may produce a hundred or so lines of "Received data" messages. These are optional debug lines in a worst-case scenario, but reviewers can discuss rate-limiting that log activity if we're afraid it's too much.
DrahtBot commented 2026-09-20 16:18:17 +00:00 (Migrated from github.com)

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.

Type Reviewers
ACK winterrdog, davidgumberg, ismaelsadeeq, achow101
Stale ACK 151henry151

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36434 (http: support unix sockets by pinheadmz)

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.

LLM Linter (✨ experimental)

Possible typos and grammar issues:

  • macos -> macOS [misspelled proper name]

2026-09-25 23:37:54

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36303 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36303.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36303#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [winterrdog](https://github.com/bitcoin/bitcoin/pull/36303#pullrequestreview-5332121227), [davidgumberg](https://github.com/bitcoin/bitcoin/pull/36303#issuecomment-5896332122), [ismaelsadeeq](https://github.com/bitcoin/bitcoin/pull/36303#pullrequestreview-5410040396), [achow101](https://github.com/bitcoin/bitcoin/pull/36303#issuecomment-6003596522) | | Stale ACK | [151henry151](https://github.com/bitcoin/bitcoin/pull/36303#issuecomment-5752966526) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36434](https://github.com/bitcoin/bitcoin/pull/36434) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36434.svg"></sub> (http: support unix sockets by pinheadmz) 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. <!--5faf32d7da4f0f540f40219e4f7537a3--> ### LLM Linter (✨ experimental) Possible typos and grammar issues: - `macos` -> `macOS` [misspelled proper name] <sup>2026-09-25 23:37:54</sup>
151henry151 commented 2026-09-20 21:57:57 +00:00 (Migrated from github.com)

Tested ACK f86eb92d9a

tip: interface_http.py passes
with the PR unthrottle diff: fails (flood thread assert)

Tested ACK f86eb92d9a tip: `interface_http.py` passes with the PR unthrottle diff: fails (flood thread assert) <!--
hodlinator (Migrated from github.com) reviewed 2026-09-21 13:32:56 +00:00
hodlinator (Migrated from github.com) commented 2026-09-21 13:32:56 +00:00

This doesn't seem to be true unless we rename dl_start_size to dl_prev_size and update the value?

This doesn't seem to be true unless we rename `dl_start_size` to `dl_prev_size` and update the value?
pinheadmz (Migrated from github.com) reviewed 2026-09-21 13:46:34 +00:00
pinheadmz (Migrated from github.com) commented 2026-09-21 13:46:34 +00:00

hm, must have lost that in a rebase before pushing. thanks for catching, will fix.

hm, must have lost that in a rebase before pushing. thanks for catching, will fix.
pinheadmz (Migrated from github.com) reviewed 2026-09-21 14:00:49 +00:00
pinheadmz (Migrated from github.com) commented 2026-09-21 14:00:49 +00:00

Sorry, I think existing code is correct actually:
while True { dl.read() } will always start from where it left off in the previous loop (i.e. only read any new lines added to file since last loop).

I didn't have this in the next test though from #36174 though, but could add it here.

Sorry, I think existing code is correct actually: `while True { dl.read() }` will always start from where it left off in the previous loop (i.e. only read any new lines added to file since last loop). I didn't have this in the next test though from #36174 though, but could add it here.
hodlinator (Migrated from github.com) reviewed 2026-09-21 15:04:23 +00:00
hodlinator (Migrated from github.com) commented 2026-09-21 15:04:23 +00:00

Ah yes, I see now that dl.seek(dl_start_size) is outside the while-loop in this test, unlike the one from #36174. assert_debug_log both re-opens and re-seeks... to prev_size which is not updated as I was expecting. I guess that serves a paranoid guard against half-completed lines in the assert_debug_log case.

Ah yes, I see now that `dl.seek(dl_start_size)` is outside the while-loop in this test, unlike the one from #36174. `assert_debug_log` both re-opens and re-seeks... to `prev_size` which is not updated as I was expecting. I guess that serves a paranoid guard against half-completed lines in the `assert_debug_log` case.
davidgumberg (Migrated from github.com) reviewed 2026-09-22 21:42:31 +00:00
davidgumberg (Migrated from github.com) commented 2026-09-22 21:42:31 +00:00

Should this be time.sleep(0.05)?

Currently the test would take 5 minutes to time out

Should this be `time.sleep(0.05)`? Currently the test would take 5 minutes to time out
davidgumberg (Migrated from github.com) reviewed 2026-09-22 22:18:09 +00:00
davidgumberg (Migrated from github.com) commented 2026-09-22 22:18:09 +00:00

But I guess the way the test is currently set up, because it succeeds after the first time it sees a stall, you have to give the flood some time, maybe instead the number of tries should be greatly reduced?

But I guess the way the test is currently set up, because it succeeds after the first time it sees a stall, you have to give the flood some time, maybe instead the number of tries should be greatly reduced?
davidgumberg (Migrated from github.com) reviewed 2026-09-22 23:03:49 +00:00
davidgumberg (Migrated from github.com) commented 2026-09-22 23:03:49 +00:00

style nanonit, feel free to disregard and only if retouching:

wrong, see below

                matches = re.finditer(
                    rf"Received (?P<received>\d+) bytes from [^\s]*:{local_port} "
                    rf"\(id=\d+\): total=(?P<total>\d+) buffered=(?P<buffered>\d+)",
                    log,
                )
                total = max((int(m.group("total")) for m in matches), default=total)
                buffered = max((int(m.group("buffered")) for m in matches), default=buffered)
style nanonit, feel free to disregard and only if retouching: <details> <summary> ### ~wrong, see below~ </summary> ```suggestion matches = re.finditer( rf"Received (?P<received>\d+) bytes from [^\s]*:{local_port} " rf"\(id=\d+\): total=(?P<total>\d+) buffered=(?P<buffered>\d+)", log, ) total = max((int(m.group("total")) for m in matches), default=total) buffered = max((int(m.group("buffered")) for m in matches), default=buffered) ```
pinheadmz (Migrated from github.com) reviewed 2026-09-23 13:48:29 +00:00
pinheadmz (Migrated from github.com) commented 2026-09-23 13:48:29 +00:00

Yeah it's intentionally a 5 minute timeout (explicit in the comment above) but if you test the regression locally you'll probably find that the test exits much earlier based on the amount of data consumed by the socket:

assert sent_total < len(flood) * 10, f"Client sent too much data: {sent_total}"

Both of these boundaries (data sent, time) were chosen at somewhat excessive values to avoid false negatives like the original test.

Yeah it's intentionally a 5 minute timeout (explicit in the comment above) but if you test the regression locally you'll probably find that the test exits much earlier based on the amount of data consumed by the socket: `assert sent_total < len(flood) * 10, f"Client sent too much data: {sent_total}"` Both of these boundaries (data sent, time) were chosen at somewhat excessive values to avoid false negatives like the original test.
davidgumberg (Migrated from github.com) reviewed 2026-09-23 19:28:17 +00:00
davidgumberg (Migrated from github.com) commented 2026-09-23 19:28:17 +00:00

(speculative; non-blocking)

Both failure limits seem so big, and the test is so eager to pass on the first sign of stalling that this test seems like it could miss actual issues is my hunch, but I don't know what other values to suggest.

(_speculative; non-blocking_) Both failure limits seem so big, and the test is so eager to pass on the first sign of stalling that this test seems like it could miss actual issues is my hunch, but I don't know what other values to suggest.
davidgumberg commented 2026-09-23 21:39:33 +00:00 (Migrated from github.com)

crACK f86eb92d9a

It seems a bit odd to me that the throttle doesn't clamp on once there's a request being processed by the application, but only after that happens and a second message is sent, I think it would be good to also do something like #36276 mainly for code correctness reasons (with a fix for the 50ms select timeout that branch had), but I also recognize that the severity of this DoS vector is extremely low, and this testing approach is much better than the old one which seems inherently flaky for the reasons in PR description, and a good enough fix.

I also think 5 minutes to time out in the failure case is far too generous, intermittent failures are bad but so are tests that are too eager to not fail, and I can't see a good justification for why the http server would continue reading from the socket 60 times after the clamp should have started. IIUC the existing test failure that is observed is because of the ~bug-ish that we'll read one more message after we should have put the clamp on, and I don't see how it could plausibly happen more than once [maybe twice if unlucky] that more data is read, but maybe I have misunderstood something.

crACK f86eb92d9a5b2ef349c7002fb3bcb44f371870e0 It seems a bit odd to me that the throttle doesn't clamp on once there's a request being processed by the application, but only after that happens *and* a second message is sent, I think it would be good to also do something like #36276 mainly for code correctness reasons (with a fix for the 50ms select timeout that branch had), but I also recognize that the severity of this DoS vector is extremely low, and this testing approach is much better than the old one which seems inherently flaky for the reasons in PR description, and a good enough fix. I also think 5 minutes to time out in the failure case is far too generous, intermittent failures are bad but so are tests that are too eager to not fail, and I can't see a good justification for why the http server would continue reading from the socket 60 times after the clamp should have started. IIUC the existing test failure that is observed is because of the ~bug-ish that we'll read one more message after we should have put the clamp on, and I don't see how it could plausibly happen more than once [maybe twice if unlucky] that more data is read, but maybe I have misunderstood something.
pinheadmz commented 2026-09-24 19:44:58 +00:00 (Migrated from github.com)

IIUC the existing test failure that is observed is because of the ~bug-ish that we'll read one more message after we should have put the clamp on

The test failure was due to a chain of black-box socket behaviors between the server and client. Without log messages the only way to detect the server throttle was with socket errors on the client side. Problem is there's too many things that can cause those socket errors and even worse, sometimes they go away and data transmission continues. That's all due to behavior in the macos kernel.

I confess I was misled by LLM review that the "bug" was causing the test failures and it was your comment on that PR that really made me step through everything again with my meatball brain and I couldn't answer the fundamental question "how does this patch in the server fix the test?"

I used packet capture to try and nail down the issue and eventually came to the conclusion that there wasn't one issue but many, and all out of control of bitcoind.

And then yeah, its hard to pick constants for how long it should take for something not to happen. If the server code is broken and a ci machine is so slow it takes 6 seconds to read data from a socket then we'll have a false positive. I'd be happy bumping that to 10 or 5 * TIMEOUT_FACTOR or something? It just means safe/patched servers will sit in the test for that long before passing.

On the other end (5 minutes) I observed some CI runs that false-failed because the TCP was so slow it took a minute for the throttle to kick in at all. Again, could be a role for TIMEOUT_FACTOR.

Could borrow the approach @maflcko wrote in #36324

> IIUC the existing test failure that is observed is because of the ~bug-ish that we'll read one more message after we should have put the clamp on The test failure was due to a chain of black-box socket behaviors between the server and client. Without log messages the only way to detect the server throttle was with socket errors on the client side. Problem is there's too many things that can cause those socket errors and even worse, sometimes they go away and data transmission continues. That's all due to behavior in the macos kernel. I confess I was misled by LLM review that the "bug" was causing the test failures and it was your comment on that PR that really made me step through everything again with my meatball brain and I couldn't answer the fundamental question "how does this patch in the server fix the test?" I used packet capture to try and nail down the issue and eventually came to the conclusion that there wasn't one issue but many, and all out of control of bitcoind. And then yeah, its hard to pick constants for how long it should take for something _not_ to happen. If the server code is broken and a ci machine is so slow it takes 6 seconds to read data from a socket then we'll have a false positive. I'd be happy bumping that to 10 or `5 * TIMEOUT_FACTOR` or something? It just means safe/patched servers will sit in the test for that long before passing. On the other end (5 minutes) I observed some CI runs that false-failed because the TCP was so slow it took a minute for the throttle to kick in at all. Again, could be a role for TIMEOUT_FACTOR. Could borrow the approach @maflcko wrote in #36324
pinheadmz (Migrated from github.com) reviewed 2026-09-24 20:04:42 +00:00
pinheadmz (Migrated from github.com) commented 2026-09-24 20:04:42 +00:00

A little bot told me that since finditer returns a single-use iterator, matches will already be used up by the time you get to buffered = ... so it'll stay at 0 no matter what.

Observable in the log if you set the flood to never stop (assert sent_total < len(flood) * 1000000000):

TestFramework (DEBUG): Server read 6847787579 bytes, 0 buffered

A little bot told me that since `finditer` returns a single-use iterator, `matches` will already be used up by the time you get to `buffered = ...` so it'll stay at 0 no matter what. Observable in the log if you set the flood to never stop (`assert sent_total < len(flood) * 1000000000`): `TestFramework (DEBUG): Server read 6847787579 bytes, 0 buffered`
pinheadmz commented 2026-09-24 20:27:57 +00:00 (Migrated from github.com)

push to b481e99e30

Apply timeout_factor to the progress check, like #36324

push to b481e99e3018ce218570b4d8173b729c548a5ce3 Apply timeout_factor to the progress check, like #36324
davidgumberg (Migrated from github.com) reviewed 2026-09-24 20:28:00 +00:00
davidgumberg (Migrated from github.com) commented 2026-09-24 20:28:00 +00:00

oops sorry, I only meant to change the pattern:

                matches = re.findall(
                    rf"Received \d+ bytes from [^\s]*:{local_port} "
                    rf"\(id=\d+\): total=(?P<total>\d+) buffered=(?P<buffered>\d+)",
                    log,
                )
                total = max((int(m.group("total")) for m in matches), default=total)
                buffered = max((int(m.group("buffered")) for m in matches), default=buffered)

also should probably drop the unused capture group if you prefer the unnamed capture group approach but that's also not blocking for me:

                matches = re.findall(
                    rf"Received \d+ bytes from [^\s]*:{local_port} \(id=\d+\): total=(\d+) buffered=(\d+)",
                    log)
                total = max((int(m[0]) for m in matches), default=total)
                buffered = max((int(m[1]) for m in matches), default=buffered)
oops sorry, I only meant to change the pattern: ```suggestion matches = re.findall( rf"Received \d+ bytes from [^\s]*:{local_port} " rf"\(id=\d+\): total=(?P<total>\d+) buffered=(?P<buffered>\d+)", log, ) total = max((int(m.group("total")) for m in matches), default=total) buffered = max((int(m.group("buffered")) for m in matches), default=buffered) ``` also should probably drop the unused capture group if you prefer the unnamed capture group approach but that's also not blocking for me: ```suggestion matches = re.findall( rf"Received \d+ bytes from [^\s]*:{local_port} \(id=\d+\): total=(\d+) buffered=(\d+)", log) total = max((int(m[0]) for m in matches), default=total) buffered = max((int(m[1]) for m in matches), default=buffered) ```
winterrdog (Migrated from github.com) reviewed 2026-09-24 20:54:00 +00:00
winterrdog (Migrated from github.com) left a comment

approach ACK

approach ACK
winterrdog (Migrated from github.com) commented 2026-09-24 20:43:21 +00:00

non-blocking suggestion: do you think threading.Event()
would be a better option since it is an explicit synchronisation primitive
instead of relying on the python's GIL implicitly ?

it could also replace the time.sleep(0.05) in send_flood with sth like
flood_open.wait(0.05) which can stop blocking early the instant .set() is
called elsewhere, so teardown becomes up to 50ms snappier and in the end
not always paying the full sleep in the last loop iteration -- a tiny saving given the
small wait timeout

suggested diff
diff --git a/test/functional/interface_http.py b/test/functional/interface_http.py
index 906c2f891b..b9caa5a197 100755
--- a/test/functional/interface_http.py
+++ b/test/functional/interface_http.py
@@ -763,12 +763,12 @@ class HTTPBasicsTest (BitcoinTestFramework):
         # server behavior because different platforms (macos in particular)
         # like to open and close the TCP window size based on heuristics
         # out of our control, and delay transmission when windows get too small.
-        flood_open = True
+        flood_stop = threading.Event()

         def send_flood(self, conn):
             sent_total = 0
             blocked = False
-            while flood_open:
+            while not flood_stop.is_set():
                 try:
                     sent = conn.conn.sock.send(flood)
                     sent_total += sent
@@ -781,7 +781,7 @@ class HTTPBasicsTest (BitcoinTestFramework):
                 if sent_total > len(flood) * 10:
                     # Don't use AssertionError here or it'll get excepted below
                     raise Exception(f"Client sent too much data: {sent_total}")
-                time.sleep(0.05)
+                flood_stop.wait(0.05)

         executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
         flood_thread = executor.submit(
@@ -831,7 +831,7 @@ class HTTPBasicsTest (BitcoinTestFramework):
         # already-passed verdict into a spurious TimeoutError. If the flood
         # thread hit its own assertion (server never throttled), result()
         # re-raises it here.
-        flood_open = False
+        flood_stop.set()
         flood_thread.result()
         executor.shutdown(wait=True)
_non_-blocking suggestion: do you think [`threading.Event()`](https://docs.python.org/3/library/threading.html#threading.Event) would be a better option since it is an explicit synchronisation primitive instead of relying on the python's GIL implicitly ? it could also replace the `time.sleep(0.05)` in `send_flood` with sth like `flood_open.wait(0.05)` which can stop blocking early the instant `.set()` is called elsewhere, so teardown becomes _up to_ 50ms snappier and in the end not always paying the full sleep in the last loop iteration -- a tiny saving given the small wait timeout <details> <summary>suggested diff </summary> ```diff diff --git a/test/functional/interface_http.py b/test/functional/interface_http.py index 906c2f891b..b9caa5a197 100755 --- a/test/functional/interface_http.py +++ b/test/functional/interface_http.py @@ -763,12 +763,12 @@ class HTTPBasicsTest (BitcoinTestFramework): # server behavior because different platforms (macos in particular) # like to open and close the TCP window size based on heuristics # out of our control, and delay transmission when windows get too small. - flood_open = True + flood_stop = threading.Event() def send_flood(self, conn): sent_total = 0 blocked = False - while flood_open: + while not flood_stop.is_set(): try: sent = conn.conn.sock.send(flood) sent_total += sent @@ -781,7 +781,7 @@ class HTTPBasicsTest (BitcoinTestFramework): if sent_total > len(flood) * 10: # Don't use AssertionError here or it'll get excepted below raise Exception(f"Client sent too much data: {sent_total}") - time.sleep(0.05) + flood_stop.wait(0.05) executor = concurrent.futures.ThreadPoolExecutor(max_workers=1) flood_thread = executor.submit( @@ -831,7 +831,7 @@ class HTTPBasicsTest (BitcoinTestFramework): # already-passed verdict into a spurious TimeoutError. If the flood # thread hit its own assertion (server never throttled), result() # re-raises it here. - flood_open = False + flood_stop.set() flood_thread.result() executor.shutdown(wait=True) ``` </details>
winterrdog (Migrated from github.com) commented 2026-09-24 20:05:58 +00:00

nit: i think we can rip out the ambiguity like so:

                    self.log.debug(f"Client sent {sent} bytes (total {sent_total} bytes)")
**nit**: i think we can rip out the ambiguity like so: ```suggestion self.log.debug(f"Client sent {sent} bytes (total {sent_total} bytes)") ```
winterrdog (Migrated from github.com) commented 2026-09-24 20:07:59 +00:00

nit: same here. i think we can rip out the ambiguity:

                        self.log.debug(f"Client socket blocked after sending {sent_total} bytes")
**nit**: same here. i think we can rip out the ambiguity: ```suggestion self.log.debug(f"Client socket blocked after sending {sent_total} bytes") ```
winterrdog (Migrated from github.com) commented 2026-09-24 20:52:16 +00:00

nit: same here. i think we can remove the ambiguity:

                    raise Exception(f"Client sent too much data: {sent_total} bytes")
**nit**: same here. i think we can remove the ambiguity: ```suggestion raise Exception(f"Client sent too much data: {sent_total} bytes") ```
winterrdog commented 2026-09-24 23:06:04 +00:00 (Migrated from github.com)

reply-to: https://github.com/bitcoin/bitcoin/pull/36303#issuecomment-5821035077

And then yeah, its hard to pick constants for how long it should take for something not to happen. If the server code is broken and a ci machine is so slow it takes 6 seconds to read data from a socket then we'll have a false positive. I'd be happy bumping that to 10 or 5 * TIMEOUT_FACTOR or something? It just means safe/patched servers will sit in the test for that long before passing.

On the other end (5 minutes) I observed some CI runs that false-failed because the TCP was so slow it took a minute for the throttle to kick in at all. Again, could be a role for TIMEOUT_FACTOR.

Could borrow the approach @maflcko wrote in #36324

initially, i did think about polling faster at the beginning and only slowing
down once things start to pause, for instance, checking every 250ms while
total is still increasing, and only falling back to a longer wait once it
stops changing. so, we could catch the stall sooner instead of always waiting out the
entire fixed interval. it would save a few seconds in the fast case like in cases of
local testing

but i think wait_until is the better call here: it is the same pattern
check_slow_read_throttle() uses in the upcoming #36324, so this file would
not end up with 2 different ways of waiting on the debug log, and CI-speed
differences get handled by one shared setting (--timeout-factor)

reply-to: https://github.com/bitcoin/bitcoin/pull/36303#issuecomment-5821035077 > And then yeah, its hard to pick constants for how long it should take for something _not_ to happen. If the server code is broken and a ci machine is so slow it takes 6 seconds to read data from a socket then we'll have a false positive. I'd be happy bumping that to 10 or `5 * TIMEOUT_FACTOR` or something? It just means safe/patched servers will sit in the test for that long before passing. > > On the other end (5 minutes) I observed some CI runs that false-failed because the TCP was so slow it took a minute for the throttle to kick in at all. Again, could be a role for TIMEOUT_FACTOR. > > Could borrow the approach `@maflcko` wrote in #36324 initially, i did think about polling faster at the beginning and only slowing down once things start to pause, for instance, checking every `250ms` while `total` is still increasing, and only falling back to a longer wait once it stops changing. so, we could catch the stall sooner instead of always waiting out the entire fixed interval. it would save a few seconds in the fast case like in cases of local testing but i think `wait_until` is the better call here: it is the same pattern `check_slow_read_throttle()` uses in the upcoming #36324, so this file would not end up with 2 different ways of waiting on the debug log, and CI-speed differences get handled by one shared setting (`--timeout-factor`)
maflcko (Migrated from github.com) reviewed 2026-09-25 09:51:56 +00:00
maflcko (Migrated from github.com) commented 2026-09-25 09:51:56 +00:00

nit: Not too important, but I find the nested checks and different exception types, with catching a bit confusing. Also, "Client sent too much data" doesn't sound too helpful to indicate that a block did not occur.

Maybe just a plain:

assert_greater_than_or_equal(len(flood) * 10, sent_total);  # block should occur earlier

or (if you insist on hand-written msgs):

                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")

(to minimize the diff)

But not important, just a nit.

Also, the comment is wrong, because send_flood is in a background thread without an except?

nit: Not too important, but I find the nested checks and different exception types, with catching a bit confusing. Also, "Client sent too much data" doesn't sound too helpful to indicate that a block did not occur. Maybe just a plain: ``` assert_greater_than_or_equal(len(flood) * 10, sent_total); # block should occur earlier ``` or (if you insist on hand-written msgs): ``` 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") ``` (to minimize the diff) But not important, just a nit. Also, the comment is wrong, because `send_flood` is in a background thread without an `except`? <!--
pinheadmz (Migrated from github.com) reviewed 2026-09-25 11:19:15 +00:00
pinheadmz (Migrated from github.com) commented 2026-09-25 11:19:15 +00:00

ok, reverting this

ok, reverting this
pinheadmz (Migrated from github.com) reviewed 2026-09-25 11:26:38 +00:00
pinheadmz (Migrated from github.com) commented 2026-09-25 11:26:38 +00:00

👍

👍
pinheadmz (Migrated from github.com) reviewed 2026-09-25 11:32:00 +00:00
pinheadmz (Migrated from github.com) commented 2026-09-25 11:32:00 +00:00

Interesting suggestion! I'll try it out

Interesting suggestion! I'll try it out
pinheadmz (Migrated from github.com) reviewed 2026-09-25 23:24:20 +00:00
pinheadmz (Migrated from github.com) commented 2026-09-25 23:24:20 +00:00
  • need to keep a try/finally though to close the background thread
* need to keep a try/finally though to close the background thread
pinheadmz commented 2026-09-25 23:26:40 +00:00 (Migrated from github.com)

push to 5b4c945d6f933d857f8dcf479bd09c7d583c5fce

include suggestions from @maflcko and @winterrdog, unwrapping wait_until, using threading.Event() and clearing up some log lines.

push to 5b4c945d6f933d857f8dcf479bd09c7d583c5fce include suggestions from @maflcko and @winterrdog, unwrapping `wait_until`, using `threading.Event()` and clearing up some log lines.
davidgumberg (Migrated from github.com) reviewed 2026-09-25 23:27:43 +00:00
davidgumberg (Migrated from github.com) commented 2026-09-25 23:27:43 +00:00

@pinheadmz if leaving as-is can you drop the first match group since it's unused?

@pinheadmz if leaving as-is can you drop the first match group since it's unused?
davidgumberg (Migrated from github.com) reviewed 2026-09-25 23:28:21 +00:00
davidgumberg (Migrated from github.com) commented 2026-09-25 23:28:21 +00:00

or say no and mark this resolved, also fine

or say no and mark this resolved, also fine
pinheadmz (Migrated from github.com) reviewed 2026-09-25 23:33:36 +00:00
pinheadmz (Migrated from github.com) commented 2026-09-25 23:33:36 +00:00

yeah sorry, cleaning up...

yeah sorry, cleaning up...
pinheadmz commented 2026-09-25 23:38:00 +00:00 (Migrated from github.com)

push to 493850c4c4

  • remove unused capture group in the regex matcher
push to 493850c4c4734acce0f75d1efcffb3868d4235fc - remove unused capture group in the regex matcher
winterrdog (Migrated from github.com) reviewed 2026-09-27 21:12:06 +00:00
winterrdog (Migrated from github.com) left a comment

tACK 493850c4c4

successfully built and tested on: FreeBSD 15.0/clang++-19/x86_64


reply-to: https://github.com/bitcoin/bitcoin/pull/36303#issue-5518981709

These are optional debug lines in a worst-case scenario, but reviewers can discuss rate-limiting that log activity if we're afraid it's too much.

for now, i think we can skip rate-limiting this. normal traffic only produces
one log line per request, and the flood test's worst case (a single ~32MB
request) tops out at around 600-700 lines in well under half a second (tried it
out with this script).
that does not seem too noisy for an opt-in debug category :)

if, indeed, we get empirical evidence that this has become a problem, a simple
fix i can think of right now would be to place the logging behind a tiny
per-client interval, for instance, only emit the debug log if at least 100ms
have passed since the last one for that client

tACK 493850c4c4734acce0f75d1efcffb3868d4235fc successfully built and tested on: FreeBSD `15.0`/`clang++-19`/`x86_64` --- reply-to: https://github.com/bitcoin/bitcoin/pull/36303#issue-5518981709 > These are optional debug lines in a worst-case scenario, but reviewers can discuss rate-limiting that log activity if we're afraid it's too much. for now, i think we can skip rate-limiting this. normal traffic only produces one log line per request, and the flood test's worst case (a single ~32MB request) tops out at around 600-700 lines in well under half a second (tried it out with this [script](https://gist.github.com/winterrdog/5b571212df549bbd6ff3363da44db595)). that does not seem too noisy for an opt-in debug category :) if, indeed, we get empirical evidence that this has become a problem, a simple fix i can think of right now would be to place the logging behind a tiny per-client interval, for instance, only emit the debug log if at least 100ms have passed since the last one for that client
First-time contributor

Base: 66776840beb558f7e84451c2c55457f0e06242f0
Head: 493850c4c4734acce0f75d1efcffb3868d4235fc

Design and approach

Log per-read counters at trace level (src/httpserver.cpp:971, head)

Receive() reads at most 64 KiB per call, so a 32 MiB request can produce at least 512 unrate-limited LogDebug entries. That is substantially noisier than the usual one additional line per request for users enabling -debug=http. The functional test framework enables trace logging, so using LogTrace would preserve its log-based assertion while making these per-read details opt-in at trace level, consistent with the logging guidance for noisy output.

🟠 Major

A read plateau does not prove the receive throttle engaged (test/functional/interface_http.py:815, head)

total is cumulative for the connection, and the checkpoint follows the client's blocking RPC without confirming that the server read it. After observing any positive total, the test passes if no further recv() succeeds for five seconds. That does not establish that flood bytes reached the server or that more bytes were available to read. The PR description reports platform TCP delivery pauses lasting 5–60 seconds, which can satisfy this predicate even if the server would read more data when bytes arrive. Assert server-side evidence that unread bytes remain available, or test the read-interest decision deterministically. No false pass of this replacement test is reported; the concern follows from the predicate and the documented delivery pauses.

💡 Suggestion

Resume the flood at the unsent offset (test/functional/interface_http.py:773, head)

If this nonblocking send returns a short positive count, sent_total advances, but the next call sends flood again from byte zero. The stream therefore repeats prefixes instead of completing each request before starting the next, unlike the base test, and may leave malformed request content for post-unblock parsing. The raw bytes still exercise receive throttling, but preserving the constructed pipeline is straightforward: send from flood[sent_total % len(flood):].

Review coverage was incomplete. The archaeology review had incomplete evidence. The alternatives experiment had incomplete evidence. The adversarial_glm review had incomplete evidence.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `66776840beb558f7e84451c2c55457f0e06242f0` Head: `493850c4c4734acce0f75d1efcffb3868d4235fc` ##### Design and approach **Log per-read counters at trace level** (src/httpserver.cpp:971, head) `Receive()` reads at most 64 KiB per call, so a 32 MiB request can produce at least 512 unrate-limited `LogDebug` entries. That is substantially noisier than the usual one additional line per request for users enabling `-debug=http`. The functional test framework enables trace logging, so using `LogTrace` would preserve its log-based assertion while making these per-read details opt-in at trace level, consistent with the logging guidance for noisy output. ##### 🟠 Major **A read plateau does not prove the receive throttle engaged** (test/functional/interface_http.py:815, head) `total` is cumulative for the connection, and the checkpoint follows the client's blocking RPC without confirming that the server read it. After observing any positive total, the test passes if no further `recv()` succeeds for five seconds. That does not establish that flood bytes reached the server or that more bytes were available to read. The PR description reports platform TCP delivery pauses lasting 5–60 seconds, which can satisfy this predicate even if the server would read more data when bytes arrive. Assert server-side evidence that unread bytes remain available, or test the read-interest decision deterministically. No false pass of this replacement test is reported; the concern follows from the predicate and the documented delivery pauses. ##### 💡 Suggestion **Resume the flood at the unsent offset** (test/functional/interface_http.py:773, head) If this nonblocking send returns a short positive count, `sent_total` advances, but the next call sends `flood` again from byte zero. The stream therefore repeats prefixes instead of completing each request before starting the next, unlike the base test, and may leave malformed request content for post-unblock parsing. The raw bytes still exercise receive throttling, but preserving the constructed pipeline is straightforward: send from `flood[sent_total % len(flood):]`. Review coverage was incomplete. The archaeology review had incomplete evidence. The alternatives experiment had incomplete evidence. The adversarial_glm review had incomplete evidence. [Full review report](<https://review.fish.foo/traces/36303-493850c4c4734acce0f75d1efcffb3868d4235fc-398-1.html>)
hebasto commented 2026-09-29 16:23:14 +00:00 (Migrated from github.com)
FWIW, https://github.com/hebasto/bitcoin-core-nightly/pull/369.
davidgumberg commented 2026-09-29 18:36:24 +00:00 (Migrated from github.com)
crtACK https://github.com/bitcoin/bitcoin/commit/493850c4c4734acce0f75d1efcffb3868d4235fc
ismaelsadeeq (Migrated from github.com) reviewed 2026-10-05 04:54:12 +00:00
ismaelsadeeq (Migrated from github.com) left a comment

Code review ACK 493850c4c4
Ran interface_http.py ×200 on: macOS 26.6.2 (25G83), arm64 (Apple M2 Pro, Mac14, - Xcode 26.1 (17B55), Apple clang 17.0.0, SDK 26.1

check_pipelined_data_is_throttled passed every time, couldn't reproduce the intermittent failure. My config differs from the failing nightly runner (run 34438869701 (https://github.com/hebasto/bitcoin-core-nightly/actions/runs/34438869701/job/102749511737)), which was macOS 26.5.2 / Xcode 27, I'm on a newer macOS but an older Xcode, so I can't say whether it's toolchain or OS-specific .

A few non-blocking notes: -

The revert diff disables the throttle, so it confirms the test catches unthrottling. IIUC the original intermittent failure was the small one-read window × macOS TCP timing, and a ~64 KB leak would plateau well under the fail threshold.

  • The OP description is mostly context (the macOS TCP behavior). It'd aid review to also state the change itself: i.e we add a per-read debug log in HTTPRemoteClient::Receive() and some context on why their etc, and that we rewrites the test to flood the connection and detect the stall from that log.

  • The approach relies on the log being emitted per socket read before request parsing, so the counter tracks bytes drained off the socket even when no request completes. If it moved to request-completion/dispatch, a never-completing flood would emit nothing and the counter couldn't observe an unbounded read. Worth a comment noting this placement is intentional load-bearing.
    (It seems in the test prevent that, by the total > 0 check though).

Code review ACK 493850c4c4734acce0f75d1efcffb3868d4235fc Ran interface_http.py ×200 on: macOS 26.6.2 (25G83), arm64 (Apple M2 Pro, Mac14, - Xcode 26.1 (17B55), Apple clang 17.0.0, SDK 26.1 `check_pipelined_data_is_throttled` passed every time, couldn't reproduce the intermittent failure. My config differs from the failing nightly runner (run 34438869701 (https://github.com/hebasto/bitcoin-core-nightly/actions/runs/34438869701/job/102749511737)), which was macOS 26.5.2 / Xcode 27, I'm on a newer macOS but an older Xcode, so I can't say whether it's toolchain or OS-specific . A few non-blocking notes: - The revert diff disables the throttle, so it confirms the test catches unthrottling. IIUC the original intermittent failure was the small one-read window × macOS TCP timing, and a ~64 KB leak would plateau well under the fail threshold. - The OP description is mostly context (the macOS TCP behavior). It'd aid review to also state the change itself: i.e we add a per-read debug log in `HTTPRemoteClient::Receive()` and some context on why their etc, and that we rewrites the test to flood the connection and detect the stall from that log. - The approach relies on the log being emitted per socket read before request parsing, so the counter tracks bytes drained off the socket even when no request completes. If it moved to request-completion/dispatch, a never-completing flood would emit nothing and the counter couldn't observe an unbounded read. Worth a comment noting this placement is intentional load-bearing. (It seems in the test prevent that, by the total > 0 check though).
ismaelsadeeq (Migrated from github.com) commented 2026-10-05 04:32:55 +00:00

In "test: assert HTTP receive throttle from server-side log"

non-blocking: We could make the verdict entirely bitcoind-based by letting the client be a dumb flooder and, after wait_until(progress_stalled), asserting the server's received-byte counter stayed reasonable (total < len(flood)*10).

In "test: assert HTTP receive throttle from server-side log" non-blocking: We could make the verdict entirely bitcoind-based by letting the client be a dumb flooder and, after `wait_until(progress_stalled)`, asserting the server's received-byte counter stayed reasonable (`total < len(flood)*10)`.
achow101 commented 2026-10-05 21:43:09 +00:00 (Migrated from github.com)
ACK 493850c4c4734acce0f75d1efcffb3868d4235fc
fanquake commented 2026-10-06 14:01:58 +00:00 (Migrated from github.com)

Backported to 32.x in #36427.

Backported to `32.x` in #36427.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36303
No description provided.