Repository navigation
Conversation
Add a functional test showing that, at -rpcmaxconnections, idle keep-alive clients keep their slots: with -rpcservertimeout disabled, a new connection over the limit is never accepted while all slots are held by clients that have finished their requests.
When the server is at -rpcmaxconnections, new connections wait in the kernel's accept queue. A keep-alive connection keeps its slot after its request has been answered, until the client closes it or it reaches -rpcservertimeout (default 30 s). A client holding the maximum number of keep-alive connections therefore blocks all other clients (including bitcoin-cli) for up to 30 s, without any error. While the server is at the limit and connections are waiting, free slots for them in two ways: - Close connections after their reply: reply with "Connection: close" instead of keeping the connection alive, and close it once the reply has been sent. The client learns from the reply that the connection won't be reused, so this does not race with the client, and a slot is freed as soon as a request has been answered. The I/O thread checks the listening sockets for waiting connections, without waiting, once per loop iteration while at the limit, and publishes the result in an atomic flag that replies read. - Evict the keep-alive client that has been idle the longest. This is needed for connections that were already idle when the other connections started waiting, as they won't get another reply. Only clients whose requests have all been answered, that have not started a new request, and that have been idle for at least HTTP_MIN_IDLE_FOR_EVICTION (500 ms) are considered, and at most one client is evicted per I/O loop iteration. At the limit, the listening sockets are only polled if a client could be evicted, so that a pending connection that can't be accepted does not turn the I/O loop into a busy loop. An eviction can still race with a client that reuses the connection at that moment; clients are expected to retry in that case (RFC 9112), but not all do, which is why replies closing their connections are the main mechanism. Tests: - Add unit tests for HTTPRemoteClient::IsEvictable() and for closing the connection after the reply while connections are waiting. - check_connection_limit_idle_clients(): the waiting connection is now served, and the client that has been idle the longest is evicted. - Add check_connection_limit_close_on_reply(): with all slots held by busy clients, a waiting connection is served once one of them gets its reply, which closes its connection. - check_connection_limit(): its clients were idle keep-alive clients, which are now evicted. They now keep an unfinished request open, so they are busy and can't be evicted. This keeps checking that a connection over the limit waits when no client can be evicted, and that closing a connection frees its slot.
Contributor
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process. ConflictsReviewers, this pull request conflicts with the following ones:
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. |
This was referenced Dec 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #36960
Problem
Since the libevent replacement (#35182), the HTTP server limits the number of connected clients to
-rpcmaxconnections(default 16, #35730). At the limit, new connections are not accepted and wait in the kernel's accept queue.A keep-alive connection keeps its slot after its request has been answered. The slot is only freed when the client closes the connection, or after
-rpcservertimeout(default 30 s) of inactivity. So a client that holds the maximum number of keep-alive connections blocks every other RPC and REST client, includingbitcoin-cli, for up to 30 s, without any error or log message.This happens in practice with a client that keeps a pool of parallel keep-alive connections. The popular indexer
electrs, for example, fetches blocks over REST with one connection per CPU thread. On a 32-core machine, every batch of blocks it fetches while catching up stalls for about 30 s. Measured on master with 100 clients that each make one request and then keep their connection open (-rpcmaxconnections=64): 62 were answered at once, and the remaining 38 only after 31.1 s, when the idle connections hit-rpcservertimeout. With-rpcservertimeout=10the stall shrinks to 10–11 s. The libevent server had no connection limit, so this didn't happen before v32.Solution
While the server is at the limit and connections are waiting to be accepted, free slots for them in two ways:
Close connections after their reply. A reply that would keep the connection alive gets
Connection: closeinstead, and the connection is closed once the reply has been sent. The client learns from the reply that the connection won't be reused, so there is no race with the client, and a slot is freed as soon as a request has been answered. The I/O thread checks the listening sockets for waiting connections without waiting, once per loop iteration and only while at the limit, and publishes the result in an atomic flag that replies read.Evict the keep-alive client that has been idle the longest. Connections that were already idle when the others started waiting won't get another reply, so they would keep their slots. Such a client is disconnected if it is fully idle: all its requests have been answered and sent, it has not started a new request, and it has been idle for at least
HTTP_MIN_IDLE_FOR_EVICTION(500 ms). At most one client is evicted per I/O loop iteration. At the limit, the listening sockets are only polled if a client could be evicted, so that a waiting connection that can't be accepted doesn't make the I/O loop spin.Closing after the reply is the main mechanism, and eviction is only the fallback. An eviction can race with the client: if the client reuses the connection at that moment, its request fails with a connection reset. HTTP allows servers to close idle connections and expects clients to retry (RFC 9112, section 9.3.1), but not all clients do. An earlier version of this change only evicted idle clients. Tested with electrs under constant connection pressure, it hit this race quickly: electrs's HTTP client (ureq) reuses the connection that has been idle the longest, the same one that was evicted, and doesn't retry, so electrs failed with "Connection reset by peer". With replies closing their connections, slots are freed without that race during sustained load, and eviction is rarely needed.
New connections that have not sent a request yet, and clients with a request in progress, are never closed. Behavior below the limit is unchanged. Both mechanisms are logged in the
httpdebug category, and the help text of-rpcmaxconnectionsdescribes them.Alternatives considered
-rpcmaxconnections: this only moves the threshold, and costs file descriptors, which is why the default was lowered from 128 to 16 in http: limit connected HTTPRemoteClients #35730.-rpcservertimeout.503: the client gets an error instead of a delay, while idle slots stay blocked.Tests
-rpcservertimeout=0, a new connection is never accepted. The second commit changes it to the new expectation: the client that has been idle the longest is evicted, and the new connection is served.Connection: closeand closes the connection.HTTPRemoteClient::IsEvictable(), including the minimum idle time (with a mocked clock), and for closing the connection after the reply while connections are waiting.check_connection_limit()filled all slots with idle keep-alive clients, which would now be evicted. Its clients now keep an unfinished request open, so they are busy, and the test still checks that a connection over the limit waits when no client can be evicted.-rpcmaxconnections.Related: #36204 addresses a different way of holding a slot: clients that never finish sending a request.