Skip to content

http: disconnect clients that never finish a request - #36204

Open
janb84 wants to merge 2 commits into
bitcoin:masterfrom
janb84:http-request-deadline
Open

janb84 wants to merge 2 commits into
bitcoin:masterfrom
janb84:http-request-deadline

Conversation

@janb84

@janb84 janb84 commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

The -rpcservertimeout timer only measures inactivity. Every read from a client resets it, so a client that keeps sending bytes without ever completing a request can hold the connection for ever.

Connection slots are capped by default 16 or by -rpcmaxconnections. Once those slots are taken, the server stops accepting new connections. Because no request ever completes the authentication functions are never hit.
The impact is low because RPC port binds to localhost by default, so this is a robustness fix rather than a remotely exploitable security issue.

How to reproduce

To reproduce: start a node with -rpcmaxconnections=2, open two sockets, and write "GET / HTTP/1.1\r\n" one byte at a time with ten seconds between bytes. bitcoin-cli getblockcount then hangs until one of the (trickling) sockets is closed.

The fix adds a second deadline next to the idle timer. It is armed on the first byte of a request and expires after -rpcservertimeout (the same value as the idle timer). This timer also does not resets or is extended by later reads. Completing request parsing clears the deadline, so a long-running RPC keeps its connection as before. The deadline is deferred while a worker holds the request, for the same reason the idle timer is. It is restarted while response bytes
are written, because the client cannot start a new request during that window. -rpcservertimeout=0 disables both timers. No new option is added.

Disclaimer this bug is found by utilizing ASTRA. Verified using KIMI 3 and Claude.

@DrahtBot

DrahtBot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

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
Approach ACK winterrdog
Stale ACK pinheadmz, hodlinator

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:

  • #37019 (http: free idle connection slots for waiting connections at the limit by optout21)
  • #36434 (http: support unix sockets by pinheadmz)
  • #36187 (http: Restore requirement of IPv4 bind success by hodlinator)

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.

@hodlinator hodlinator left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK 537944c

Comment thread src/httpserver.cpp Resolved
Comment thread src/httpserver.h Resolved
Comment thread src/test/httpserver_tests.cpp Resolved
Comment thread src/httpserver.cpp Resolved
Comment thread test/functional/interface_http.py Resolved
Comment thread test/functional/interface_http.py Resolved
Comment thread test/functional/interface_http.py Resolved
@janb84
janb84 force-pushed the http-request-deadline branch from 537944c to 87dab48 Compare September 14, 2026 17:51
@janb84

janb84 commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

Rebased to latests master
Made change for the changes made by pr #36174
Incoporated suggestions made by @hodlinator
Reworked functional tests

@janb84

janb84 commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

Rebased and adjusted the function naming.

@hodlinator hodlinator left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 99e8803

Nice how you cleaned up the test, makes it easy to grasp.

Comment thread test/functional/interface_http.py Resolved
Comment thread test/functional/interface_http.py Resolved
@pinheadmz

Copy link
Copy Markdown
Member

concept ACK, this mitigates slow-loris and perhaps most importantly, mitigates the LLM "findings" reports we keep getting 👍

@janb84
janb84 force-pushed the http-request-deadline branch from 99e8803 to bfbe9a6 Compare November 2, 2026 15:57

@hodlinator hodlinator left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed bfbe9a6

Comment thread src/httpserver.h Resolved
Comment thread src/test/httpserver_tests.cpp Resolved
Comment thread src/test/httpserver_tests.cpp Resolved
Comment thread src/test/httpserver_tests.cpp Resolved
Comment thread src/test/httpserver_tests.cpp Resolved
@janb84
janb84 force-pushed the http-request-deadline branch from bfbe9a6 to c504205 Compare November 4, 2026 14:26
@janb84

janb84 commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

Incorporated the suggested changes by the review from hodlinator.
Changed a line in httpserver.cpp (L 1242) !m_req_busy => Assume(!m_req_busy) because that state (armed and busy) cannot happen on production, reduces test code.

@pinheadmz pinheadmz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 2f699f9

Built and tested on macos/arm64. Played with the new timeout using socat to manually make very slow (and then fast) HTTP requests. Mutated the patch a few places to observe test failure, and watched the packets in wireshark throughout to see the trickle.

Reviewed the code and left a few aesthetic nits, no blockers. I found the unit test with the token counter a bit hard to follow but after trying to simplify it myself a few different ways I ended up understanding it and agreeing with the approach.

kimi-k3 gave up trying to write a slow loris attack against this patch ;-)

Show Signature
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

ACK 2f699f91f12e163229edc979769af58457a5f6d0
-----BEGIN PGP SIGNATURE-----

iQJPBAEBCAA5FiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmsF95QbFIAAAAAABAAO
bWFudTIsMi41KzEuMTIsMCwzAAoJEOfimEtiick6mF4QANnw9RpPRMezeATOiSHO
q/wt1wVotMJKGJsy5xRwgW+tuegFhKNB73JNT7uC+KNq4KE8InT/Qr4+QqN0mOAN
rQ65V/PzMhVNs7QXT/waw2iPDPMKYY3QooPkvBaviWSrsKuk/DhmbMZw57iJdWqH
IMF1crTEUzcSUmS3S+EawwTyPBTv9vVaYiki1eZBdzAn14mHkKkNALTu33fbvhWd
DcOPrFyrM+9V+OYwHgiKhHv9UOv3tMGpGXGZU2Fs1xBBeq3snslaMhQv7FTzQCs9
YDKdeoehv3/IRRckzoe1OlvtyQ5grkvSFcAHHurDdq2qtV0okhG5zasFkS5K4Aiw
6M4dHtZvmdWn63Vg7NyB/FznGUtl27/yEOiQxmvyOM95dbec5Mbzq3epgoxfvFHZ
jzzwuhR9RIUIlv8Ng2M4kgAcYx6duQpxfzgmbi+py/XWNpk9kIbMi6yNGp2MLupQ
BpriQK890kx4jdY/s0959OeQ5VM0Q31S861duuY4S5rFPHkFsvGKpwg/prZIPsBu
qYcgCrdpLbo6snbD19GxsfLoTcO0BZ2z2BbuAExzWRRdrcZxV+HvrObc32GpGQik
bXVKqsK6JZrHQQmJ45d9pr3OV4HNflsy79qjOseGU+oMvmSh44irHkFO5FKbwBz1
FFtnnO838TnxgW03FAkCFgLr
=IX/c
-----END PGP SIGNATURE-----

pinheadmz's public key is on openpgp.org

Comment thread src/test/httpserver_tests.cpp Resolved
Comment thread src/test/httpserver_tests.cpp Resolved
Comment thread src/test/httpserver_tests.cpp Resolved
@DrahtBot
DrahtBot requested a review from hodlinator November 24, 2026 20:50
@janb84
janb84 force-pushed the http-request-deadline branch 2 times, most recently from 21fac44 to b6dfbd6 Compare November 27, 2026 12:44
@janb84

janb84 commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Pushed update to fix the lint issue & suggestions of @pinheadmz taken.

Comment on lines +1018 to +1019
constexpr std::string_view data{"GET /first HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"
"GET /second HTTP/1.1\r\n"};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you touch again:

Suggested change
constexpr std::string_view data{"GET /first HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"
"GET /second HTTP/1.1\r\n"};
constexpr std::string_view data{"GET /first HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"
"GET /second HTTP/1.1\r\n"};

Comment thread src/test/httpserver_tests.cpp Resolved

@hodlinator hodlinator left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK b6dfbd6

Nice to avert slow-loris attacks even though RPC is a slightly trusted interface.

I think one could tighten up the functional test a little bit more, but not blocking.

Comment thread src/test/httpserver_tests.cpp Resolved
Comment thread src/test/httpserver_tests.cpp Resolved
Comment thread src/test/httpserver_tests.cpp Resolved
Comment thread test/functional/interface_http.py Resolved
@DrahtBot
DrahtBot requested a review from pinheadmz November 30, 2026 14:35
Every read resets the idle timer, so a client that trickles in a
request one byte at a time and never finishes it keeps its connection
slot for as long as it keeps sending.

Add unit and functional tests that pin down this behaviour. The
functional test limits the server to one connection and occupies it
with such a client. A second client queued behind it gets no response
within the timeout.

The next commit adds a completion deadline and updates these tests to
expect the client to be disconnected.
Every read resets the idle timer, so a client that keeps sending bytes
without ever completing a request holds on to its connection slot
indefinitely. A handful of them fill -rpcmaxconnections and no other
client gets served.

Add a second deadline timer that starts at the first byte of a request
and is not extended by subsequent reads. It is cleared once the request
has been parsed, so a slow RPC keeps its connection. The deadline is
restarted while response data goes out.

Update the tests from the previous commit: the trickling client is now
disconnected once its deadline passes, and the client queued behind it
gets a response about one timeout after the first byte. A late response
would mean each byte pushed the deadline back, an early one that the
slot was freed before the deadline. The debug log check confirms the
disconnect was due to the deadline and not the idle timeout.
@janb84
janb84 force-pushed the http-request-deadline branch from b6dfbd6 to 1dfe11b Compare December 1, 2026 13:38
@janb84

janb84 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Reviewers, sorry. I have changed the form now to {characterization-test , fix + flipped assertions + new deadline tests). (where new is not new from a reviewer viewpoint but from master / code viewpoint)
So mainly the order is changed not the tests & code.

Pushed new updates that addresses suggestions by @hodlinator and drahtbot

@winterrdog winterrdog left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approach ACK on adding a 2nd timeout specifically for request completion

waiting_conn.set_timeout(RPCSERVERTIMEOUT + 2)
with self.node.assert_debug_log(["took too long to send a complete request"]):
waiting_conn.post_raw('/', '{"method": "getblockcount"}')
response = waiting_conn.recv_raw()

@winterrdog winterrdog Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on NetBSD 11.0, check_trickled_request_timeout() fails. waiting_conn.recv_raw() raises TimeoutError because the client-side timeout (RPCSERVERTIMEOUT + 2) expires before a response shows up

on NetBSD, increasing the client-side timeout to any value >= RPCSERVERTIMEOUT * 3 makes the test pass. RPCSERVERTIMEOUT * 2 is still not enough. the test also passes if i remove the client-side timeout altogether. Linux passes without any of these changes, using the original RPCSERVERTIMEOUT + 2

what i cannot tell yet is why it breaks. in the failing run's debug.log, i never see HTTP client took too long to send a complete request


some logs:

test output log
...
2026-11-17T00:23:21.273664Z TestFramework (INFO): Client did not finish sending request before connection was terminated
2026-11-17T00:23:21.279598Z TestFramework (INFO): Check -rpcservertimeout
2026-11-17T00:23:23.747429Z TestFramework (INFO): Check that -rpcservertimeout disconnects clients that never finish a request
2026-11-17T00:23:26.149963Z TestFramework (ERROR): Unexpected exception:
Traceback (most recent call last):
  File "/home/user404/bitcoin/test/functional/test_framework/test_framework.py", line 149, in main
    self.run_test()
    ~~~~~~~~~~~~~^^
  File "/home/user404/bitcoin/build/test/functional/interface_http.py", line 155, in run_test
    self.check_trickled_request_timeout()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/home/user404/bitcoin/build/test/functional/interface_http.py", line 495, in check_trickled_request_timeout
    response = waiting_conn.recv_raw()
  File "/home/user404/bitcoin/build/test/functional/interface_http.py", line 91, in recv_raw
    return self.conn.sock.recv(1024)
           ~~~~~~~~~~~~~~~~~~~^^^^^^
TimeoutError: timed out
2026-11-17T00:23:26.153951Z TestFramework (INFO): Not stopping nodes as test failed. The dangling processes will be cleaned up later.
2026-11-17T00:23:26.154074Z TestFramework (WARNING): Not cleaning up dir /tmp/bitcoin_func_test_s9etyj6d
2026-11-17T00:23:26.154171Z TestFramework (ERROR): Test failed. Test logging available at /tmp/bitcoin_func_test_s9etyj6d/test_framework.log
...
debug.log excerpt
...
2026-11-17T04:15:04.537793Z [http] [httpserver.cpp:977] [Receive] [http] Received 1 bytes from 127.0.0.1:62906 (id=5): total=240 buffered=1
2026-11-17T04:15:04.786570Z [http] [httpserver.cpp:977] [Receive] [http] Received 1 bytes from 127.0.0.1:62906 (id=5): total=241 buffered=2
2026-11-17T04:15:05.035110Z [http] [httpserver.cpp:977] [Receive] [http] Received 1 bytes from 127.0.0.1:62906 (id=5): total=242 buffered=3
2026-11-17T04:15:05.283603Z [http] [httpserver.cpp:977] [Receive] [http] Received 1 bytes from 127.0.0.1:62906 (id=5): total=243 buffered=4
2026-11-17T04:15:05.532013Z [http] [httpserver.cpp:977] [Receive] [http] Received 1 bytes from 127.0.0.1:62906 (id=5): total=244 buffered=5
2026-11-17T04:15:05.781073Z [http] [httpserver.cpp:977] [Receive] [http] Received 1 bytes from 127.0.0.1:62906 (id=5): total=245 buffered=6
2026-11-17T04:15:06.029565Z [http] [httpserver.cpp:977] [Receive] [http] Received 1 bytes from 127.0.0.1:62906 (id=5): total=246 buffered=7
2026-11-17T04:15:06.278318Z [http] [httpserver.cpp:977] [Receive] [http] Received 1 bytes from 127.0.0.1:62906 (id=5): total=247 buffered=8
2026-11-17T04:15:06.504703Z [http] [httpserver.cpp:957] [Receive] [http] Received EOF from 127.0.0.1:62906 (id=5)
2026-11-17T04:15:06.504776Z [http] [httpserver.cpp:1197] [operator()] [http] Disconnecting HTTP client 127.0.0.1:62906 (id=5)
2026-11-17T04:15:06.504887Z [http] [httpserver.cpp:884] [NewSockAccepted] [http] HTTP Connection accepted from 127.0.0.1:62905 (id=6)
2026-11-17T04:15:06.504943Z [http] [httpserver.cpp:977] [Receive] [http] Received 189 bytes from 127.0.0.1:62905 (id=6): total=189 buffered=189
2026-11-17T04:15:06.505030Z [http] [httpserver.cpp:1174] [TryReadRequestInternal] [http] Received a POST request for / from 127.0.0.1:62905 (id=6)
2026-11-17T04:15:06.505124Z [http] [httpserver.cpp:957] [Receive] [http] Received EOF from 127.0.0.1:62905 (id=6)
2026-11-17T04:15:06.505192Z [http] [httpserver.cpp:1197] [operator()] [http] Disconnecting HTTP client 127.0.0.1:62905 (id=6)
  • node's full debug.log here -- the tail has the lines of interest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants