Skip to content

Commit 7562e2a

Browse files
committed
Squashed 'src/ipc/libmultiprocess/' changes from a4f92969649..1fc65008f7d
1fc65008f7d Merge bitcoin-core/libmultiprocess#237: Made SpawnProcess() behavior safe post fork() 5205a87cd90 test: check SpawnProcess post-fork safety 69652f0edfa Precompute argv before fork in SpawnProcess 30a8681de62 SpawnProcess: avoid fd leak on close failure d0fc1081d09 Merge bitcoin-core/libmultiprocess#196: ci: Add NetBSD job 7b171f45bfc Merge bitcoin-core/libmultiprocess#234: doc: Fix typos and grammar in documentation and comments 861da39cae9 ci: Add NetBSD job 458745e3940 Fix various typos, spelling mistakes, and grammatical errors in design.md and source code comments. 585decc8561 Merge bitcoin-core/libmultiprocess#236: ci: Install binary package `capnproto` on OpenBSD instead of building it 14e926a3ff3 refactor: extract MakeArgv helper 1ee909393f4 ci: Install binary package `capnproto` on OpenBSD instead of building it 470fc518d4b Merge bitcoin-core/libmultiprocess#230: cmake: add ONLY_CAPNP target_capnp_sources option 2d8886f26c4 Merge bitcoin-core/libmultiprocess#228: Add versions.md and version.h files describing version branches and tags c1838be565d Merge bitcoin-core/libmultiprocess#225: Improve and document act support a173f1704ce Merge bitcoin-core/libmultiprocess#223: ci: Replace nix-shell with equivalent nix develop command 625eaca42fb Merge bitcoin-core/libmultiprocess#229: Design Documentation Update cc234be73a6 Design doc update 81c652687b8 cmake: add ONLY_CAPNP target_capnp_sources option 6e01d2d766e Add versions.md and version.h files describing version branches and tags 4e3f8fa0d2c doc: add instructions for using act 81712ff6bbf ci: disable KVM and sandbox inside act containers 18a2237a8ef ci: Replace nix-shell with equivalent nix develop command git-subtree-dir: src/ipc/libmultiprocess git-subtree-split: 1fc65008f7d64161e84c08cbd93109a23dd6a1e9
1 parent 0f01e15 commit 7562e2a

17 files changed

Lines changed: 562 additions & 47 deletions

File tree

.github/workflows/ci.yml

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,66 @@ on:
55
pull_request:
66

77
jobs:
8+
build-netbsd:
9+
runs-on: ubuntu-latest
10+
name: build • netbsd ${{ matrix.release }}
11+
defaults:
12+
run:
13+
shell: netbsd {0}
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
# Test all supported releases.
18+
# See https://www.netbsd.org/releases/.
19+
include:
20+
- release: 9.4
21+
capnproto-cppflags: 'CPPFLAGS="-DKJ_NO_EXCEPTIONS=0 -DKJ_USE_KQUEUE=0"'
22+
- release: 10.1
23+
capnproto-cppflags: 'CPPFLAGS="-DKJ_NO_EXCEPTIONS=0"'
24+
steps:
25+
- uses: actions/checkout@v6
26+
27+
- name: Start NetBSD VM
28+
uses: vmactions/netbsd-vm@v1
29+
with:
30+
release: ${{ matrix.release }}
31+
# The installed compiler version must match the CXX variable
32+
# defined in `ci/configs/netbsd.bash`.
33+
prepare: |
34+
pkg_add cmake ninja-build gcc14
35+
# capnproto prerequisites.
36+
# See the following "Install capnproto" step.
37+
run: |
38+
set -e
39+
pkg_add digest libtool-base mktools pkgconf cwrappers
40+
pkg_admin -K /usr/pkg/pkgdb fetch-pkg-vulnerabilities
41+
cd /usr
42+
cvs -danoncvs@anoncvs.NetBSD.org:/cvsroot checkout -P \
43+
pkgsrc/devel/capnproto \
44+
pkgsrc/devel/libtool-base \
45+
pkgsrc/devel/pkgconf \
46+
pkgsrc/devel/zlib \
47+
`# gcc15 is referenced here because the pkgsrc framework requires lang/gcc15/version.mk to exist` \
48+
`# during the "make install" step below, even though we compile our project with gcc14.` \
49+
pkgsrc/lang/gcc15 \
50+
pkgsrc/mk \
51+
pkgsrc/pkgtools \
52+
pkgsrc/security/openssl \
53+
pkgsrc/sysutils/install-sh/files
54+
sync: 'rsync'
55+
copyback: false
56+
57+
- name: Install capnproto
58+
run: |
59+
cd /usr/pkgsrc/devel/capnproto/
60+
unset PKG_PATH
61+
make ${{ matrix.capnproto-cppflags }} install
62+
63+
- name: Run CI script
64+
run: |
65+
cd ${{ github.workspace }}
66+
CI_CONFIG="ci/configs/netbsd.bash" bash ci/scripts/ci.sh
67+
868
build-openbsd:
969
runs-on: ubuntu-latest
1070
name: build • openbsd
@@ -18,17 +78,10 @@ jobs:
1878
uses: vmactions/openbsd-vm@v1
1979
with:
2080
prepare: |
21-
pkg_add -v cmake ninja git bash
22-
run: |
23-
git clone --depth=1 https://codeberg.org/OpenBSD/ports.git /usr/ports
81+
pkg_add -v cmake ninja bash capnproto
2482
sync: 'rsync'
2583
copyback: false
2684

27-
- name: Install capnproto
28-
run: |
29-
cd /usr/ports/devel/capnproto/
30-
make install
31-
3285
- name: Run CI script
3386
run: |
3487
cd ${{ github.workspace }}
@@ -76,6 +129,11 @@ jobs:
76129
build:
77130
runs-on: ubuntu-latest
78131

132+
env:
133+
NIX_EXTRA_CONFIG_ACT: |
134+
sandbox = false
135+
filter-syscalls = false
136+
79137
strategy:
80138
fail-fast: false
81139
matrix:
@@ -90,6 +148,10 @@ jobs:
90148
uses: cachix/install-nix-action@v31 # 2025-05-27, from https://github.com/cachix/install-nix-action/tags
91149
with:
92150
nix_path: nixpkgs=channel:nixos-25.05 # latest release
151+
# Act executes inside an unprivileged container (Docker or Podman),
152+
# so KVM support isn't available.
153+
enable_kvm: "${{ github.actor != 'nektos/act' }}"
154+
extra_nix_config: ${{ github.actor == 'nektos/act' && env.NIX_EXTRA_CONFIG_ACT || '' }}
93155

94156
- name: Run CI script
95157
env:

ci/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,29 @@ CI_CONFIG=ci/configs/olddeps.bash ci/scripts/run.sh
2424
```
2525

2626
By default CI jobs will reuse their build directories. `CI_CLEAN=1` can be specified to delete them before running instead.
27+
28+
### Running workflows with `act`
29+
30+
You can run either the entire workflow or a single matrix entry locally. On
31+
macOS or Linux:
32+
33+
1. Install [`act`](https://github.com/nektos/act) and either Docker or
34+
Podman.
35+
2. Inside the Podman VM, create a named volume for the Nix store (ext4,
36+
case-sensitive) so builds persist across runs. Recreate it any time you want
37+
a clean cache:
38+
```bash
39+
podman volume create libmultiprocess-nix
40+
```
41+
3. From the repo root, launch the workflow. The example below targets the
42+
sanitize matrix entry; drop the `--matrix` flag to run every configuration.
43+
```bash
44+
act \
45+
--reuse \
46+
-P ubuntu-latest=ghcr.io/catthehacker/ubuntu:act-24.04 \
47+
--container-options "-v libmultiprocess-nix:/nix" \
48+
-j build \
49+
--matrix config:sanitize
50+
```
51+
52+

ci/configs/netbsd.bash

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CI_DESC="CI config for NetBSD"
2+
CI_DIR=build-netbsd
3+
export CXXFLAGS="-Werror -Wall -Wextra -Wpedantic -Wno-unused-parameter"
4+
# Hardcode GCC 14, since default GCC versions installed by NetBSD are older
5+
# and may not be compatible with libmultiprocess. GCC 14 was chosen because
6+
# it's the latest compiler available on all versions of NetBSD that we test.
7+
# Note that the GCC version specified here must match the version specified
8+
# in pkg_add in ci.yml.
9+
export CXX="/usr/pkg/gcc14/bin/g++"
10+
CMAKE_ARGS=(-G Ninja)
11+
BUILD_ARGS=(-k 0)

ci/scripts/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ set -o errexit -o nounset -o pipefail -o xtrace
1010

1111
[ "${CI_CONFIG+x}" ] && source "$CI_CONFIG"
1212

13-
nix-shell --pure --keep CI_CONFIG --keep CI_CLEAN "${NIX_ARGS[@]+"${NIX_ARGS[@]}"}" --run ci/scripts/ci.sh shell.nix
13+
nix develop --ignore-environment --keep CI_CONFIG --keep CI_CLEAN "${NIX_ARGS[@]+"${NIX_ARGS[@]}"}" -f shell.nix --command ci/scripts/ci.sh

cmake/TargetCapnpSources.cmake

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Example:
5555
function(target_capnp_sources target include_prefix)
5656
cmake_parse_arguments(PARSE_ARGV 2
5757
"TCS" # prefix
58-
"" # options
58+
"ONLY_CAPNP" # options
5959
"" # one_value_keywords
6060
"IMPORT_PATHS" # multi_value_keywords
6161
)
@@ -85,11 +85,14 @@ function(target_capnp_sources target include_prefix)
8585
set_source_files_properties(${capnp_file}.c++ PROPERTIES SKIP_LINTING TRUE) # Ignored before cmake 3.27
8686
target_sources(${target} PRIVATE
8787
${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.c++
88-
${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-client.c++
89-
${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-server.c++
90-
${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-types.c++
9188
)
92-
89+
if(NOT TCS_ONLY_CAPNP)
90+
target_sources(${target} PRIVATE
91+
${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-client.c++
92+
${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-server.c++
93+
${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-types.c++
94+
)
95+
endif()
9396
list(APPEND generated_headers ${capnp_file}.h)
9497
endforeach()
9598

@@ -111,5 +114,7 @@ function(target_capnp_sources target include_prefix)
111114
# dependencies explicitly because while cmake detect dependencies of non
112115
# generated files on generated headers, it does not reliably detect
113116
# dependencies of generated headers on other generated headers.
114-
add_custom_target("${target}_headers" DEPENDS ${generated_headers})
117+
if(NOT TARGET "${target}_headers")
118+
add_custom_target("${target}_headers" DEPENDS ${generated_headers})
119+
endif()
115120
endfunction()

doc/design.md

Lines changed: 185 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ There is also optional support for thread mapping, so each thread making interpr
1313

1414
Libmultiprocess acts as a pure wrapper or layer over the underlying protocol. Clients and servers written in other languages, but using a shared capnproto schema can communicate with interprocess counterparties using libmultiprocess without having to use libmultiprocess themselves or having to know about the implementation details of libmultiprocess.
1515

16-
### Internals
16+
## Core Architecture
1717

1818
The `ProxyClient` and `ProxyServer` generated classes are not directly exposed to the user, as described in [usage.md](usage.md). Instead, they wrap C++ interfaces and appear to the user as pointers to an interface. They are first instantiated when calling `ConnectStream` and `ServeStream` respectively for creating the `InitInterface`. These methods establish connections through sockets, internally creating `Connection` objects wrapping a `capnp::RpcSystem` configured for client and server mode respectively.
1919

@@ -25,7 +25,190 @@ When a generated method on the `ProxyClient` is called, it calls `clientInvoke`
2525

2626
On the server side, the `capnp::RpcSystem` receives the capnp request and invokes the corresponding C++ method through the corresponding `ProxyServer` and the heavily templated `serverInvoke` triggering a `ServerCall`. The return values from the actual C++ methods are copied into capnp responses by `ServerRet` and exceptions are caught and copied by `ServerExcept`. The two are connected through `ServerField`. The main method driving execution of a request is `PassField`, which is invoked through `ServerField`. Instantiated interfaces, or capabilities in capnp speak, are tracked and owned by the server's `capnp::RpcSystem`.
2727

28-
## Interface descriptions
28+
## Request and Response Flow
29+
30+
Method parameters and return values are serialized using Cap'n Proto's Builder objects (for sending) and Reader objects (for receiving). Input parameters flow from the client to the server, while output parameters (return values) flow back from the server to the client.
31+
32+
```mermaid
33+
sequenceDiagram
34+
participant clientInvoke
35+
participant BuildField as BuildField<br/>(Client)
36+
participant ReadField_C as ReadField<br/>(Client)
37+
participant Request as Request<br/>message
38+
participant serverInvoke
39+
participant ReadField as ReadField<br/>(Server)
40+
participant BuildField_S as BuildField<br/>(Server)
41+
participant Response as Response<br/>message
42+
43+
Note over clientInvoke,ReadField: Input Parameter Flow
44+
clientInvoke->>BuildField: BuildField(input_arg)
45+
BuildField->>Request: Serialize input
46+
Request->>serverInvoke: Cap'n Proto message
47+
serverInvoke->>ReadField: Deserialize input
48+
49+
Note over clientInvoke,Response: Output Parameter Flow
50+
serverInvoke-->>BuildField_S: BuildField(output)
51+
BuildField_S-->Response: Serialize output
52+
Response-->>ReadField_C: Cap'n Proto message
53+
ReadField_C-->>clientInvoke: Deserialize output
54+
```
55+
56+
### Detailed Serialization Mechanism
57+
58+
Parameters are represented as Fields that must be set on Cap'n Proto Builder objects (for sending) and read from Reader objects (for receiving).
59+
60+
#### Building Fields
61+
62+
`BuildField` uses a generated parameter `Accessor` to set the appropriate field in the Cap'n Proto Builder object.
63+
64+
```mermaid
65+
sequenceDiagram
66+
participant clientInvoke as clientInvoke or<br/>serverInvoke
67+
participant BuildField
68+
participant Accessor
69+
participant Builder as Params::Builder
70+
71+
Note over clientInvoke,Builder: Serializing Parameters
72+
clientInvoke->>BuildField: BuildField(param1)
73+
BuildField->>Accessor: Use generated field accessor
74+
Accessor->>Builder: builder.setField1(param1)
75+
76+
clientInvoke->>BuildField: BuildField(param2)
77+
BuildField->>Accessor: Use generated field Accessor
78+
Accessor->>Builder: builder.setField2(param2)
79+
```
80+
81+
#### Reading Fields
82+
83+
`ReadField` uses a generated parameter `Accessor` to read the appropriate field from the Cap'n Proto Reader object and reconstruct C++ parameters.
84+
85+
```mermaid
86+
sequenceDiagram
87+
participant serverInvoke as clientInvoke or<br/>serverInvoke
88+
participant ReadField
89+
participant Accessor
90+
participant Reader as Params::Reader
91+
participant ServerCall
92+
93+
Note over serverInvoke,ServerCall: Deserializing Parameters
94+
serverInvoke->>ReadField: Read param1
95+
ReadField->>Accessor: Use generated field accessor
96+
Accessor->>Reader: reader.getField1()
97+
Reader-->>ServerCall: call function with param1
98+
```
99+
100+
## Server-Side Request Processing
101+
102+
The generated server code uses a Russian nesting doll structure to process method fields. Each `ServerField` wraps another `ServerField` (for the next parameter), or wraps `ServerRet` (for the return value), which finally wraps `ServerCall` (which invokes the actual C++ method).
103+
104+
Each `ServerField` invokes `PassField`, which:
105+
1. Calls `ReadField` to deserialize the parameter from the `Params::Reader`
106+
2. Calls the next nested layer's `invoke()` with the accumulated parameters
107+
3. Calls `BuildField` to serialize the parameter back if it's an output parameter
108+
109+
`ServerRet` invokes the next layer (typically `ServerCall`), stores the result, and calls `BuildField` to serialize it into the `Results::Builder`.
110+
111+
`ServerCall` uses the generated `ProxyMethod<MethodParams>::impl` pointer-to-member to invoke the actual C++ method on the wrapped implementation object.
112+
113+
```mermaid
114+
sequenceDiagram
115+
participant serverInvoke
116+
participant SF1 as ServerField<br/>(param 1)
117+
participant SF2 as ServerField<br/>(param 2)
118+
participant SR as ServerRet<br/>(return value)
119+
participant SC as ServerCall
120+
participant PMT as ProxyMethodTraits
121+
participant Impl as Actual C++ Method
122+
123+
serverInvoke->>SF1: SF1::invoke
124+
SF1->>SF2: SF2::invoke
125+
SF2->>SR: SR::invoke
126+
SR->>SC: SC::invoke
127+
SC->>PMT: PMT::invoke
128+
PMT->>Impl: Call impl method
129+
Impl->>PMT: return
130+
PMT->>SC: return
131+
SC->>SR: return
132+
SR->>SF2: return
133+
SF2->>SF1: return
134+
SF1->>serverInvoke: return
135+
```
136+
137+
## Advanced Features
138+
139+
### Callbacks
140+
141+
Callbacks (passed as `std::function` arguments) are intercepted by `CustomBuildField` and converted into Cap'n Proto capabilities that can be invoked across process boundaries. On the receiving end, `CustomReadField` intercepts the capability and constructs a `ProxyCallFn` object with an `operator()` that sends function calls back over the socket to invoke the original callback.
142+
143+
```mermaid
144+
sequenceDiagram
145+
participant CT as Client Thread
146+
participant C as clientInvoke
147+
participant CBF1 as CustomBuildField (Client)
148+
participant S as Socket
149+
participant CRF1 as CustomReadField (Server)
150+
participant Srv as Server Code
151+
participant PCF as ProxyCallFn
152+
153+
C->>CBF1: send function parameter
154+
CBF1->>S: creates a Server for the function and sends a capability
155+
S->>CRF1: receives a capability and creates ProxyCallFn
156+
CRF1->>Srv:
157+
Srv->>PCF: call the callback
158+
PCF-->>CT: sends request to Client
159+
```
160+
161+
### Thread Mapping
162+
163+
Thread mapping enables each client thread to have a dedicated server thread processing its requests, preserving thread-local state and allowing recursive mutex usage across process boundaries.
164+
165+
Thread mapping is initialized by defining an interface method with a `ThreadMap` parameter and/or response. The example below adds `ThreadMap` to the `construct` method because libmultiprocess calls the `construct` method automatically.
166+
167+
```capnp
168+
interface InitInterface $Proxy.wrap("Init") {
169+
construct @0 (threadMap: Proxy.ThreadMap) -> (threadMap :Proxy.ThreadMap);
170+
}
171+
```
172+
173+
- **ThreadMap in parameter**: The client's `CustomBuildField` creates a `ThreadMap::Server` capability and sends it to the server, where `CustomReadField` stores the `ThreadMap::Client` in `connection.m_thread_map`
174+
- **ThreadMap in response**: The server's `CustomBuildField` creates a `ThreadMap::Server` capability and sends it to the client, where `CustomReadField` stores the `ThreadMap::Client` in `connection.m_thread_map`
175+
176+
You can specify ThreadMap in the parameter only, response only, or both:
177+
- **Parameter only**: Server can create threads on the client
178+
- **Response only**: Client can create threads on the server
179+
- **Both (as shown)**: Bidirectional thread creation
180+
181+
When both parameter and response include ThreadMap, both processes end up with `ThreadMap::Client` capabilities pointing to each other's `ThreadMap::Server`, allowing both sides to create threads on the other process.
182+
183+
### Async Processing with Context
184+
185+
By adding a `Context` parameter to a method in the capnp interface file, you enable async processing where the client tells the server to execute the request in a separate worker thread. For example:
186+
187+
```capnp
188+
processData @5 (context :Proxy.Context, data :Data) -> (result :Result);
189+
```
190+
191+
If a method does not have a `Context` parameter, then libmultiprocess will execute IPC requests invoking that method on the I/O event loop thread. This is fine for fast and non-blocking methods, but should be avoided for any methods that are slow or blocking or make any IPC calls(including callbacks to the client), since as long as the method is executing, the Cap'n Proto event loop will not be able to perform any I/O.
192+
193+
When a method has a `Context` parameter:
194+
195+
**Client side** (`CustomBuildField`):
196+
If this is the first asynchronous request made from the current client thread, `CustomBuildField` will:
197+
1. Call `connection.m_thread_map.makeThreadRequest()` to request a dedicated worker thread on the server (stored in `request_threads` map)
198+
2. Set the remote thread capability in `Context.thread`
199+
3. Create a local `Thread::Server` object for the current thread (stored in `callback_threads` map)
200+
4. Set the local thread capability in `Context.callbackThread`
201+
202+
Subsequent requests will reuse the existing thread capabilities held in `callback_threads` and `request_threads`.
203+
204+
**Server side** (`PassField`):
205+
1. Looks up the local `Thread::Server` object specified by `context.thread`
206+
2. The worker thread:
207+
- Stores `context.callbackThread` in its `request_threads` map (so callbacks go to the right client thread)
208+
- Posts the work lambda to that thread's queue via `waiter->post(invoke)`
209+
- Cleans up the `request_threads` entry
210+
211+
## Interface Definitions
29212

30213
As explained in the [usage](usage.md) document, interface descriptions need to be consumed both by the _libmultiprocess_ code generator, and by C++ code that calls and implements the interfaces. The C++ code only needs to know about C++ arguments and return types, while the code generator only needs to know about capnp arguments and return types, but both need to know class and method names, so the corresponding `.h` and `.capnp` source files contain some of the same information, and have to be kept in sync manually when methods or parameters change. Despite the redundancy, reconciling the interface definitions is designed to be _straightforward_ and _safe_. _Straightforward_ because there is no need to write manual serialization code or use awkward intermediate types like [`UniValue`](https://github.com/bitcoin/bitcoin/blob/master/src/univalue/include/univalue.h) instead of native types. _Safe_ because if there are any inconsistencies between API and data definitions (even minor ones like using a narrow int data type for a wider int API input), there are errors at build time instead of errors or bugs at runtime.
31214

0 commit comments

Comments
 (0)