Skip to content

http: evict idle keep-alive client when at the connection limit - #30

Closed
optout21 wants to merge 2 commits into
masterfrom
2611-http-close-idle-if-full
Closed

optout21 wants to merge 2 commits into
masterfrom
2611-http-close-idle-if-full

Conversation

@optout21

@optout21 optout21 commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Fixes TODO

Problem

Since the libevent replacement (bitcoin#35182), the HTTP server limits the number of connected clients to -rpcmaxconnections (default 16, bitcoin#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 idle keep-alive connections blocks every other RPC and REST client, including bitcoin-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=10 the stall shrinks to 10–11 s. The libevent server had no connection limit, so this didn't happen before v32.

Solution

If a connection is waiting while the server is at the limit, disconnect the keep-alive client that has been idle the longest, so that the waiting connection can be accepted:

  • Only fully idle clients are evicted: all their requests have been answered and sent, they have not started a new request, and they have been idle for at least HTTP_MIN_IDLE_FOR_EVICTION (250 ms). New connections that have not sent a request yet, and clients with a request in progress, are never evicted.
  • The minimum idle time avoids cutting off a client that is about to reuse its connection right away.
  • At most one client is evicted per I/O loop iteration, since the number of waiting connections is unknown.
  • At the limit, the listening sockets are only polled if a client could be evicted. Otherwise a waiting connection that can't be accepted would keep the listening socket readable, and the I/O loop would spin.
  • Each eviction is logged in the http debug category.

Behavior below the limit is unchanged, and so is the behavior at the limit when no client is idle.

An evicted client may be just about to send its next request, and then sees the connection closed. That is the same race keep-alive clients already face with the idle timeout. The minimum idle time makes it unlikely.

Alternatives considered

  • Raise the default of -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 bitcoin/bitcoin#35730.
  • Send Connection: close in replies while connections are waiting: this avoids the race above, but doesn't help with clients that went idle before the other connections arrived, so idle connections would still need to be evicted. The trigger is the waiting connection, so the eviction is done there.
  • Reject waiting connections with 503: the client gets an error instead of a delay, while idle slots stay blocked.

Tests

  • The first commit adds a functional test for the current behavior: with all slots held by idle keep-alive clients and -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.
  • Unit test for HTTPRemoteClient::IsEvictable(), including the minimum idle time (with a mocked clock).
  • 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.

Related: bitcoin#36204 addresses a different way of holding a slot: clients that never finish sending a request.

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 idle keep-alive connections therefore blocks all other clients
(including bitcoin-cli) for up to 30 s, without any error.

If a connection is waiting while the server is at the limit, disconnect
the keep-alive client that has been idle the longest, so that the
waiting connection can be accepted. 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 (250 ms) are
considered. 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.

Tests:
- Add a unit test for HTTPRemoteClient::IsEvictable().
- check_connection_limit_idle_clients(): the waiting connection is now
  served, and the client that has been idle the longest is evicted.
- 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.
@optout21

Copy link
Copy Markdown
Owner Author

Closing in favor of #31

@optout21 optout21 closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant