Skip to content

http: support unix sockets - #36434

Open
pinheadmz wants to merge 10 commits into
bitcoin:masterfrom
pinheadmz:http-unix-sockets
Open

pinheadmz wants to merge 10 commits into
bitcoin:masterfrom
pinheadmz:http-unix-sockets

Conversation

@pinheadmz

@pinheadmz pinheadmz commented Oct 4, 2026 •

Copy link
Copy Markdown
Member

Closes #5029

Accept unix socket paths as options for -rpcbind= (server) and -rpcconnect= (bitcoin-cli). Options follow the same pattern as ipcbind:

  • =unix is a magic keyword that means <datadir>/http.sock
  • =unix:relative/path means <datadir>/relative/path
  • =unix:/absolute/path means /absolute/path

We delete any existing http.sock before calling bind() and do not clean up at shutdown.

Because access is handled by the filesystem, -rpcallowip is ignored for unix sockets and not required at all if we are only binding to unix sockets.

Authentication by username/password/cookie is still required.

A new option --httpunix is added to the test framework and added to test_runner for rpc_bind.py and interface_http.py for CI, but (like --usecli) most other tests should also pass with that option. When set, bitcoind is started with -rpcbind=unix...which overrides tcp://localhost entirely. AuthProxy has been extended to connect directly to the unix socket.

Try it out!

bitcoind -regtest -rpcbind=unix
bitcoin-cli -regtest -rpcconnect=unix getbestblockhash

Refactored code:

Back in #27375 we added unix socket support but it was isolated to the Proxy class. That created a bit of tech debt which we pay off in this PR. Two new classes are implemented:

  • UnixSocketAddr reflects the CService API for generic unix sockets, without bitcoin-p2p-specific attributes
  • SocketAddr holds a variant of UnixSocketAddr and Cservice so classes like Proxy and HTTPServer can operate on an abstract level, with either socket type.
  • CNetAddr and CService are left untouched -- they are somewhat dirty combinations of generic socket classes and bitcoin p2p code, adding unix sockets to those classes wouldn't make sense for bitcoin p2p operations and that's why this design was chosen.

Follow-up suggestions:

  • Deduplicate some of the unix socket path resolution code between IPC and HTTP
  • On macos the default unix socket buffer size is only ~8k. In the functional tests we have to bump the python client socket buffer up to ~256k or the "throttle" tests will stall on macos. We could apply this socket option on the server side as well, only on macos... requires some bike shedding.
  • This option replaces TCP but it does not replace HTTP, there may be a more efficient transport protocol.

Production code and test framework changes designed and authored by my own cranial meatball. Unit tests added by Opus. C++ cleaned up by Fable, security tightened by Kimi-k3 🧠 🤖 🤖 🤖

@DrahtBot

DrahtBot commented Oct 4, 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
Concept ACK b-l-u-e, vasild
Approach ACK winterrdog

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)
  • #36994 (net: reduce ASMap lookup and address grouping costs by l0rinc)
  • #36969 (net: Warn only once when a proxy or the I2P proxy is unreachable by fjahr)
  • #36963 (net, http: handle socket errors separately from reads by Sjors)
  • #36904 (log: Remove trailing and embedded newlines from log messages by maflcko)
  • #36204 (http: disconnect clients that never finish a request by janb84)
  • #36187 (http: Restore requirement of IPv4 bind success by hodlinator)
  • #36033 ([wip,nomerge,rfc] build: Require C++23 compiler by maflcko)
  • #35713 (Remove boost as a unit test runner by rustaceanrob)
  • #35355 (Use atomics for determining whether trace logging is enabled by ajtowns)
  • #33954 (test: add functional test for outbound connection management by mzumsande)

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.

LLM Linter (✨ experimental)

Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):

  • src/bitcoin-cli.cpp: Lookup(host, port, /*fAllowLookup=*/true, 256)
  • src/httpserver.cpp: Lookup("::1", http_port, false)
  • src/httpserver.cpp: Lookup("127.0.0.1", http_port, false)
  • src/httpserver.cpp: Lookup(host, port, false)

2026-12-01 18:36:07

@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.

concept ACK


users of other projects that rely on Core have always wanted sth similar to this and implemented workarounds in the meantime:

there was also an attempt to add this back in 2017:

unfortunately, that attempt ran into a limitation in libevent's HTTP client. supporting Unix Domain sockets in bitcoin-cli required a libevent patch to pass an already-open Unix socket connection to the HTTP client. this was eventually addressed in libevent/libevent#1722, but by then Core had decided to moved away from libevent. anyway, with libevent dropped, this is no longer an issue

@pinheadmz

pinheadmz commented Oct 4, 2026 •

Copy link
Copy Markdown
Member Author

Don't forget libevent/libevent#1615 ! Its been a long time coming... now that that pesky little refactor is out of the way we can finally get to it ;-)

@b-l-u-e

b-l-u-e commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

concept ACK...i found an issue by running two nodes and noticed that second node can take over a live rpc unix socket path, if two nodes use different datadirs but the same -rpcbind=unix: the second node starts with no error..since it removes the first node socket file and creates its own then after that bitcoin cli talks to the second node.. the first node keeps running but becomes unreachable so with TCP the second bind fails with address already in use

i reproduced this on regtest

B=build/bin
SOCK=/tmp/btc-hijack.sock
mkdir -p /tmp/nodeA /tmp/nodeB
ARGS="-regtest -daemon -listen=0 -rpcbind=unix:$SOCK -rpcuser=u -rpcpassword=p"
CLI="$B/bitcoin-cli -regtest -rpcconnect=unix:$SOCK -rpcuser=u -rpcpassword=p -rpcwait"

1: for node A

$B/bitcoind -datadir=/tmp/nodeA $ARGS
$CLI getrpcinfo | grep logpath && stat -c 'inode=%i' $SOCK

output:
Bitcoin Core starting
"logpath": "/tmp/nodeA/regtest/debug.log"
inode=2229678

2: for node B

$B/bitcoind -datadir=/tmp/nodeB $ARGS; sleep 2
$CLI getrpcinfo | grep logpath && stat -c 'inode=%i' $SOCK

output:
Bitcoin Core starting                                  (as you can see node B starts without error)
"logpath": "/tmp/nodeB/regtest/debug.log"              (cli now reaches B)
inode=2229647   (and this replaces node A socket file)

3: ss -xlp | grep btc-hijack

output:
u_str LISTEN ... /tmp/btc-hijack.sock ... pid=1684462  (this is for B)
u_str LISTEN ... /tmp/btc-hijack.sock ... pid=1684363  (here A still listening but unreachable)

4: $CLI stop; sleep 2

output:
Bitcoin Core stopping                                  (`stop` reached B)

5: pgrep -a bitcoind

output:
1684363 build/bin/bitcoind -datadir=/tmp/nodeA ...     (A still running)

6: $CLI -rpcwait=0 getblockcount

output:
error: Error while attempting to communicate with server unix:/tmp/btc-hijack.sock (Could not connect to the server)

Make sure the bitcoind server is running and that you are connecting to the correct unix socket path.
Use "bitcoin-cli -help" for more info.

@b-l-u-e

b-l-u-e commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

i noticed a change in bitcoin-cli the host header contains the resolved IP instead of the hostname given in -rpcconnect=localhost the build from master sends Host: localhost but on this PR sends Host: 127.0.0.1

i reproduced the result by running this py file on terminal 1

import socket

srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
srv.bind(("127.0.0.1", 18555))
srv.listen()
print("listening on 127.0.0.1:18555")
while True:
    conn, _ = srv.accept()
    request = conn.recv(4096).decode(errors="replace")
    for line in request.split("\r\n"):
        if line.lower().startswith("host:"):
            print(line)
    conn.close()

on terminal 2 i ran this command

build-master/bin/bitcoin-cli -rpcconnect=localhost -rpcport=18555 -rpcuser=u -rpcpassword=p -rpcclienttimeout=3 getblockcount
build-pr36434/bin/bitcoin-cli       -rpcconnect=localhost -rpcport=18555 -rpcuser=u -rpcpassword=p -rpcclienttimeout=3 getblockcount

and the output was

python3 /tmp/show_host.py 
listening on 127.0.0.1:18555
Host: localhost
Host: 127.0.0.1

@pinheadmz

pinheadmz commented Oct 8, 2026 •

Copy link
Copy Markdown
Member Author

push to 022a6aa:

Major change is copying the parameter format from ipcbind, so default is to create http.sock in the appropriate network data directory. This should also adderss @b-l-u-e concern about multiple processes messing with each others sockets. Or at least, makes it harder for a user to footgun. Will update the PR description as well.

@pinheadmz

Copy link
Copy Markdown
Member Author

push to 81b8369

@pinheadmz

Copy link
Copy Markdown
Member Author

push to c01d87c

  • Get the functional test to pass on NetBsd by using a smaller socket buffer size (thanks to @winterrdog)

That issue was also caught by running @hebasto nightly BSD tests on this branch:

NetBSD (failed)
FreeBSD (passed)
OpenBSD (passed)

I'll re-run the extra CI tests with this new fix in place

@pinheadmz

Copy link
Copy Markdown
Member Author

hebasto-nightly CI passed on all 3 BSDs after lowering the buffer size in the test:

@pinheadmz

Copy link
Copy Markdown
Member Author

push to 31a19bf

rebase on master, fix conflicts from #36916

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.

Unix Domain Sockets

6 participants