Skip to content

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

Backlog

Description

@optout21

Is there an existing issue for this?

  • I have searched the existing issues

Current behaviour

Since v32.1 (custom HTTP server, #35182), the server stops calling accept() once -rpcmaxconnections clients are connected (default 16, #35730). Additional clients wait in the kernel's accept queue.

HTTP/1.1 connections are kept alive by default, so a connection keeps its slot after its request has been answered. It's only released when the client closes it, or after -rpcservertimeout (default 30 s) of inactivity. As a result, a client that holds 16 idle keep-alive connections blocks every other RPC and REST client for up to 30 s. That includes bitcoin-cli. Those clients get no error, and nothing is logged; their requests just don't make progress.

In v31 (libevent), the number of concurrent connections was not limited, so this didn't happen.

Real-world example: electrs

electrs (an Electrum server) fetches blocks over REST through its indexing library bindex-rs, which uses the HTTP client ureq 3:

  • Blocks are fetched in parallel batches, one request at a time per thread. The number of threads is the number of CPU threads, so on a 32-core machine electrs uses up to 32 REST connections at once, plus one JSON-RPC connection.
  • The connections are HTTP/1.1 keep-alive, and up to 32 of them are kept open in a connection pool between requests.
  • No request timeout is configured: a request on a connection that bitcoind hasn't accepted waits indefinitely.

When electrs catches up with more than 16 blocks to index (for example after being stopped for a while), each batch of 32 blocks goes like this:

  1. 16 connections are accepted and served. Their threads finish their blocks, and the connections go back to the client's pool, idle.
  2. The other 16 threads have sent their requests on new connections, which wait in the accept queue. They can't use the idle pooled connections, because each request is already bound to its own (not yet accepted) connection.
  3. The batch only completes once the idle connections reach -rpcservertimeout, bitcoind closes them, and the waiting connections are accepted.

Observed effects:

  • Catching up stalls for about 30 s per batch of 32 blocks.
  • bitcoin-cli getblockcount hangs for the same time, since it waits in the same accept queue.
  • electrs doesn't react to Ctrl+C or a plain kill during the stall, because its threads are blocked in requests without a timeout. Only kill -9 stops it right away.
  • While it's stuck, ss -tn state established '( sport = :38332 )' lists 29 connections, and ss -ltn '( sport = :38332 )' shows Recv-Q 13 on the listening socket: 16 accepted connections, and 13 waiting to be accepted.

Setting -rpcmaxconnections=64 makes the problem go away, but there's no hint (error, log message) pointing to it.

Relevant settings

Setting Default Role here
-rpcmaxconnections 16 Accepted client connections, busy or idle. At the limit, new connections wait unaccepted in the kernel's accept queue. This is the limit being hit.
-rpcservertimeout 30 s Idle timeout of a connection. In practice it's what frees the slots, so it sets the length of the stall.
-rpcthreads 16 Worker threads; a thread is only busy while a request executes. They're idle during the stall, so this isn't the bottleneck.
-rpcworkqueue 64 Parsed requests waiting for a worker; a full queue gets 503 and a log warning. Not reached, because the waiting requests are never read.

The two defaults of 16 differ in practice: a thread is held for the milliseconds a request takes, while a connection is held for its whole keep-alive lifetime, including the time spent idle.

Expected behaviour

When the server is at -rpcmaxconnections and new connections are waiting, slots held by keep-alive connections should be freed for them, instead of making every other client wait for -rpcservertimeout. Many HTTP servers handle idle keep-alive connections more aggressively under connection pressure.

Possible improvements:

  1. While connections are waiting at the limit, answer with Connection: close and close the connection after the reply, so that a slot is freed as soon as a request has been answered. This doesn't race with the client, since the client learns from the reply that the connection won't be reused.
  2. For connections that were already idle when the others started waiting (they won't get another reply), disconnect the longest-idle keep-alive client without an in-flight request.
  3. Log a (rate-limited) warning when the connection limit is reached, pointing to -rpcmaxconnections. Currently nothing is logged, which makes this hard to diagnose.
  4. Mention in the release notes that clients using more than 16 parallel keep-alive connections need to raise -rpcmaxconnections.

Steps to reproduce

The following script reproduces it without electrs. It starts 100 concurrent clients. Each one sends a single GET /rest/chaininfo.json and then keeps its connection open, like a client-side keep-alive pool, and prints when it was answered.

  1. Start bitcoind with -rest=1 (signet RPC port 38332 below). The results below were measured with -rpcmaxconnections=64 and the default -rpcservertimeout=30; the same happens with the default of 16.
  2. Run:
    import socket, threading, time
    N = 100  # number of concurrent clients
    def client(i, t0):
        s = socket.create_connection(("127.0.0.1", 38332))  # signet RPC port
        s.sendall(b"GET /rest/chaininfo.json HTTP/1.1\r\nHost: localhost\r\n\r\n")
        s.recv(65536)
        print(f"client {i:2} answered after {time.time() - t0:5.1f}s")
        time.sleep(200)  # keep the connection open
    t0 = time.time()
    for i in range(N):
        threading.Thread(target=client, args=(i, t0), daemon=True).start()
    time.sleep(150)

Results:

  • 100 clients, -rpcmaxconnections=64: 62 clients were answered immediately (0.0 s; 2 slots were in use by another client). The remaining 38 were all answered after 31.1 s, i.e. only once the idle connections reached -rpcservertimeout.
  • 150 clients: the answers came in three bursts. Each group had to wait for the previous group's idle connections to time out.
  • -rpcservertimeout=10: the waiting clients were answered after 10–11 s instead of 31 s, so the stall follows the idle timeout exactly.
  • With the proposed fix: all 100 clients are answered immediately.

Any other client is affected in the same way: while the slots are held by idle connections, bitcoin-cli getblockcount also hangs until the next timeout. While it's blocked, ss -ltn '( sport = :38332 )' shows a non-zero Recv-Q on the listening socket, meaning connections are waiting to be accepted.

Relevant log output

With -debug=http, the waiting clients are only accepted once the idle connections hit the idle timeout. Excerpt from a run of the script above with -rpcmaxconnections=10 -rpcservertimeout=60 (repetitive lines elided):

2026-11-24T11:15:31Z [http] HTTP Connection accepted from 127.0.0.1:54530 (id=0)
2026-11-24T11:15:31Z [http] Received a GET request for /rest/chaininfo.json from 127.0.0.1:54530 (id=0)
...                         (clients id=1 to id=9 accepted and served the same way)
2026-11-24T11:15:31Z [http] HTTPResponse (status code: 200 size: 970) added to send buffer for client 127.0.0.1:54530 (id=0)
...                         (all 10 slots now held by idle keep-alive connections; nothing for 61 s)
2026-11-24T11:16:32Z [http] HTTP client idle timeout 127.0.0.1:54530 (id=0)
2026-11-24T11:16:32Z [http] Disconnecting HTTP client 127.0.0.1:54530 (id=0)
...                         (same for id=1 to id=9)
2026-11-24T11:16:32Z [http] HTTP Connection accepted from 127.0.0.1:54610 (id=10)
2026-11-24T11:16:32Z [http] Received a GET request for /rest/chaininfo.json from 127.0.0.1:54610 (id=10)
...                         (id=11 to id=19 accepted and served the same way)

How did you obtain Bitcoin Core

Compiled from source

What version of Bitcoin Core are you using?

master @ acdb326. By code inspection, the same applies to v32.0 and v32.1 (httpserver.cpp: the listen sockets aren't polled at the limit, and idle connections are only released by -rpcservertimeout).

Operating system and version

Ubuntu Linux 26.04

Machine specifications

Intel Xeon CPU, 32 cores

Activity

  1. optout21 commented on Nov 24, 2026

    @optout21
    ContributorAuthor

    Note: proposed fix is being worked on, currently here: optout21#31

  2. pinheadmz commented on Nov 24, 2026

    @pinheadmz
    Member

    Relevant IRC conversation: https://bitcoin-irc.chaincode.com/bitcoin-core-dev/2026-10-29#1270525;

    But it was determined that ElectrumX only used 10 connections and they had two instances running. Configuring a higher limit worked. I'm not so sure I'm on board with the eviction of keep alive connections, thats how P2P churns connections but I dont think thats right for HTTP -- would nginx or apache do that?

  3. optout21 commented on Nov 24, 2026

    @optout21
    ContributorAuthor

    @pinheadmz , I've seen the mentioned IRC chat, and also comments in #35730, including:

    "The default value would only need to be adjusted by a service that uses more than 16 persistent connections"

    "A 16-connection cap is not a problem for any of these projects under normal operation"

    It seems that I have accidentally bumped into such usage: electrs on a host with 16+ cores makes 16+ persistent connections. I know that the easy solution is to sync the configs -- increase bitcoind's maxrpconnections or decrease in electrs, yet I decided this may need some followup, because:

    • If I've bumped into it, other may as well
    • It doesn't seem optimal that if more than 16 keepalive connections are made, 16 of them are served right away, but the others wait, and for 30 seconds nothing happens
    • The logs don't warn about connections that are pending but don't get a free slot.
    • It can be reproduced with a fewliner python script (above)

    Regarding your nginx/apache question, here's what my agent found (I haven't double checked this part):

    nginx does this: when a worker is about to run out of connections (free connections ≤ 1/16 of worker_connections), ngx_get_connection() calls ngx_drain_connections(), which closes up to 32 "reusable" connections, longest-idle first: idle keep-alive connections and connections that haven't sent a request yet. There's no minimum idle time, the close is silent, and it logs "worker_connections are not enough, reusing connections". (src/core/ngx_connection.c)
    Apache's event MPM: "If all workers are busy, it will close connections in keep-alive state even if the keep-alive timeout has not expired." (AsyncRequestWorkerFactor)

    My proposed fix is actually less aggressive (than nginx's).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions