Skip to content

http: free idle connection slots for waiting connections at the limit - #37019

Draft
optout21 wants to merge 2 commits into
bitcoin:masterfrom
optout21:2611-http-close-if-full2
Draft

optout21 wants to merge 2 commits into
bitcoin:masterfrom
optout21:2611-http-close-if-full2

Conversation

@optout21

Copy link
Copy Markdown
Contributor

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, 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

While the server is at the limit and connections are waiting to be accepted, free slots for them in two ways:

  1. Close connections after their reply. A reply that would keep the connection alive gets Connection: close instead, 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.

  2. 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 http debug category, and the help text of -rpcmaxconnections describes them.

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 #35730.
  • Only evict idle clients: simpler, but races with clients that reuse their connections, as described above.
  • Only close connections after their reply: race-free, but connections that were idle before the others started waiting would still block their slots until -rpcservertimeout.
  • 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.
  • New functional test for closing after the reply: with all slots held by busy clients (which can't be evicted), a waiting connection is served once one of them gets its reply, which carries Connection: close and closes the connection.
  • Unit tests for 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.
  • Manually tested with electrs catching up against a node with a low -rpcmaxconnections.

Related: #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 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.
@DrahtBot

DrahtBot commented Nov 30, 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.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #37055 (refactor: drop format calls on strings without arguments by fanquake)
  • #36434 (http: support unix sockets by pinheadmz)
  • #36204 (http: disconnect clients that never finish a request by janb84)

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.

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.

http: idle keep-alive connections block new clients when max connections is reached

2 participants