Add makePool method on ThreadMap - #283
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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. |
makePool method on ThreadMapmakePool method on ThreadMap
d56e637 to
158f30d
Compare
makePool method on ThreadMapmakePool method on ThreadMap
8050527 to
0ac739b
Compare
|
Concept ACK. This is a good idea that should make development of rust & python clients easier, while making the behavior opt-in and not affecting c++ clients or existing clients using current behavior. With this feature, we may also want to add a way to mark certain methods that can take long time before returning to not use the thread pool, so they do not block other work. These methods could create their own threads to run on, or require threads to be specified.
Actually I think this is not an issue. If a server method is called with a callbackThread value it can still pass the same value back to the client when making the callback, no matter where the server thread is running.
Yes I need to look more closely at current implementation, but would seem better to have a single queue of waiting tasks instead of giving each thread its own queue. Otherwise execution order could be very unpredictable and if a single request is slow, it could create a backlog of requests assigned to the same thread. It seems like it should be possible to have a shared queue without too much complexity, so probably worth looking into. |
|
Concept ACK. This feature significantly reduces integration burden for clients written in other programming languages. I just glanced at it, planning to review it deeply soon. One thing to note: This PR will definitely need release notes. |
|
Would be nice to have a before and after usage example (with |
|
Should I take a hack at implementing proper work delegation throughout the pool or would reviewers prefer I keep it as is for now? |
|
re: #283 (comment)
On my side, I'd love to see a more elaborate pool. I was thinking about something like https://github.com/bitcoin/bitcoin/blob/master/src/util/threadpool.h as reference |
|
Just tested 0ac739b on peer-observer's ipc-extractor with a It's working as expected and, notably, reduces the overhead of creating a |
0ac739b to
acce408
Compare
|
Re-implemented as a "shortest queue" pool, where the thread with the lowest count of pending jobs is selected, tiebreaker by lowest index. I'd say this is more than sufficient for now (I would imagine users would not have more outstanding jobs than allocated threads in the current mining use case). |
7973f8d to
a828771
Compare
ViniciusCestarii
left a comment
There was a problem hiding this comment.
Shortest-queue selection is looking good
a828771 to
baab0fe
Compare
|
Nice feedback, updated baab0fe |
xyzconstant
left a comment
There was a problem hiding this comment.
Thanks for the changes @rustaceanrob
I exercised this test again: #283 (comment) and downstream IPC tests, everything is working as expected.
I am still reviewing but I have left a couple of comments.
Note that doc/design.md needs to be updated too to reflect this new feature
| } else { | ||
| MP_LOG(loop, Log::Error) | ||
| << "IPC server error request #" << req << ", pool thread not found"; | ||
| throw std::runtime_error("pool thread not found"); |
There was a problem hiding this comment.
I'm wondering how this block could be exercised. A test for this would be great.
There was a problem hiding this comment.
This can only happen if a worker is removed from m_threads and not m_thread_pool, which only be possible if we added a shrink/resize method. I could add something like this, but for now I'm not sure if it's necessary. FWIW the previous else path also seems untested.
diff --git a/test/mp/test/test.cpp b/test/mp/test/test.cpp
index 9be8742..cd5d5f4 100644
--- a/test/mp/test/test.cpp
+++ b/test/mp/test/test.cpp
@@ -598,5 +598,38 @@ KJ_TEST("Call async IPC method without thread or pool errors correctly")
KJ_EXPECT(error_thrown);
}
+KJ_TEST("Pool dispatch with torn-down slot surfaces error")
+{
+ TestSetup setup;
+ ProxyClient<messages::FooInterface>* foo = setup.client.get();
+ setup.server->m_impl->m_fn = [] {};
+
+ std::promise<void> done;
+ bool error_thrown{false};
+ foo->m_context.loop->sync([&] {
+ foo->m_context.connection->m_thread_pool.push_back(
+ {Thread::Client{KJ_EXCEPTION(FAILED, "pool slot torn down")}});
+
+ auto request{foo->m_client.callFnAsyncRequest()};
+ request.initContext();
+ foo->m_context.loop->m_task_set->add(
+ request.send().then(
+ [&](auto&&) { done.set_value(); },
+ [&](kj::Exception&& e) {
+ error_thrown = true;
+ KJ_EXPECT(std::string_view{e.getDescription().cStr()}.find(
+ "pool thread not found") != std::string_view::npos);
+ done.set_value();
+ }));
+ });
+ done.get_future().get();
+ KJ_EXPECT(error_thrown);
+}
+| if (pool[i].depth < slot->depth) slot = &pool[i]; | ||
| } | ||
| ++slot->depth; | ||
| auto guard = kj::defer([slot] { --slot->depth; }); |
There was a problem hiding this comment.
nit: KJ_DEFER macro would be more consistent IMO.
There was a problem hiding this comment.
KJ_DEFER doesn't support move assignment
There was a problem hiding this comment.
I actually meant something like this:
diff --git a/include/mp/type-context.h b/include/mp/type-context.h
index d241d95..e31ceee 100644
--- a/include/mp/type-context.h
+++ b/include/mp/type-context.h
@@ -223,9 +223,9 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn&
if (pool[i].depth < slot->depth) slot = &pool[i];
}
++slot->depth;
- auto guard = kj::defer([slot] { --slot->depth; });
return connection->m_threads.getLocalServer(slot->client)
- .then([&loop, invoke = kj::mv(invoke), req](const kj::Maybe<Thread::Server&>& pool_perhaps) mutable {
+ .then([&loop, invoke = kj::mv(invoke), req, slot](const kj::Maybe<Thread::Server&>& pool_perhaps) mutable {
+ KJ_DEFER(--slot->depth);
KJ_IF_MAYBE (pt, pool_perhaps) {
auto& pool_thread = static_cast<ProxyServer<Thread>&>(*pt);
MP_LOG(loop, Log::Debug)
@@ -236,8 +236,7 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn&
<< "IPC server error request #" << req << ", pool thread not found";
throw std::runtime_error("pool thread not found");
}
- })
- .attach(kj::mv(guard));
+ });
}
}, [&loop, req](::kj::Exception&& e) -> kj::Promise<typename ServerContext::CallContext> {
// If you see the error "(remote):0: failed: remote exception:However, this is a nit
There was a problem hiding this comment.
The diff above compiles and passes the tests, but so does this other:
diff --git a/include/mp/type-context.h b/include/mp/type-context.h
index d241d95..0a99610 100644
--- a/include/mp/type-context.h
+++ b/include/mp/type-context.h
@@ -223,7 +223,6 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn&
if (pool[i].depth < slot->depth) slot = &pool[i];
}
++slot->depth;
- auto guard = kj::defer([slot] { --slot->depth; });
return connection->m_threads.getLocalServer(slot->client)
.then([&loop, invoke = kj::mv(invoke), req](const kj::Maybe<Thread::Server&>& pool_perhaps) mutable {
KJ_IF_MAYBE (pt, pool_perhaps) {
@@ -236,8 +235,7 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn&
<< "IPC server error request #" << req << ", pool thread not found";
throw std::runtime_error("pool thread not found");
}
- })
- .attach(kj::mv(guard));
+ });
}
}, [&loop, req](::kj::Exception&& e) -> kj::Promise<typename ServerContext::CallContext> {
// If you see the error "(remote):0: failed: remote exception:baab0fe to
ac910d2
Compare
baab0fe to
dd05eea
Compare
|
dd05eea removes |
|
@ryanofsky, r.e. second comment, is this what you mean? diff --git a/include/mp/type-context.h b/include/mp/type-context.h
index d241d95..9751de7 100644
--- a/include/mp/type-context.h
+++ b/include/mp/type-context.h
@@ -202,7 +202,10 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn&
Context::Reader context_arg = Accessor::get(params);
auto thread_client = context_arg.getThread();
auto* connection = server.m_context.connection;
- auto result = connection->m_threads.getLocalServer(thread_client)
+ auto thread_promise = context_arg.hasThread()
+ ? connection->m_threads.getLocalServer(thread_client)
+ : kj::Promise<kj::Maybe<Thread::Server&>>(nullptr);
+ auto result = thread_promise
.then([&loop, invoke = kj::mv(invoke), req, connection](const kj::Maybe<Thread::Server&>& perhaps) mutable {
// If the client specified a thread, dispatch to it directly.
KJ_IF_MAYBE (thread_server, perhaps) { |
That's close but not exactly what I mean. I'd like to preserve the current "invalid thread handle" error in the case where So idea would be more like |
dd05eea to
d77e897
Compare
|
Unfortunately a larger diff but all existing code moved into the |
|
Concept ACK This removes the requirement for non-libmultiprocess clients to manage thread handles explicitly. A server-wide threadpool is worth considering. The current per-connection pool allocates O(connections × pool_size) OS threads. External clients can open an unpredictable number of connections, so this scales without bound. A shared pool would cap total thread count at a fixed size across all connections. |
xyzconstant
left a comment
There was a problem hiding this comment.
Code review ACK d77e897
I think the changes are clean and ready to be merged.
Ran local and IPC tests on downstream, compiled a bitcoin-node instance to verify it wasn't affected, and finally tested the changes in a Rust client (see: xyzconstant/peer-observer@c414765).
This significantly improves how clients written in other languages consume libmultiprocess processes by eliminating the need to create a new thread for each IPC call, because the serving processes now automatically assign threads to requests with no threads specified.
I explored another alternative to the current approach but felt this was the most effective solution.
As a next step, based on ryanofsky's suggestion (comment), it might be helpful to rename the ThreadMap interface to something like Connection or Runtime for better clarity.
| # Pre-allocate a pool of server threads for implicit dispatch. When a | ||
| # request arrives with no context.thread set, the server dispatches it | ||
| # through this pool via a shared work queue. | ||
| makePool @1 (count :UInt32) -> (); |
There was a problem hiding this comment.
nit: Relocate the comment below makePool definition line to maintain consistency with the file.
|
Following on from my review:
I've made some progress on top of these changes. Happy to open a follow-up PR if/once this one gets merged. |
|
tACK d77e897 Tested with https://github.com/ViniciusCestarii/bitcoin-ipc-demo/tree/make-pool-tester and it successfully worked passing simultaneous calls to the threads with smaller queues. |
ryanofsky
left a comment
There was a problem hiding this comment.
Code review ACK d77e897. Looks good! Sorry for the delay getting to this and I plan to merge this soon after looking over the other review comments.
I still do have the same concern about scheduling algorithm not being very robust (#283 (comment)) since requests that should be fast could get stuck queued behind much slower requests and be delayed for a long time (potentially indefinitely) even if workers are free which could execute them. A good fix for this would be to add incoming requests to a shared queue and have workers read from the queue. But it sounds like current design does work well in practice, so I'm not very concerned about this and this could be improved in a followup
| const std::string thread_name = "pool/" + std::to_string(i); | ||
| std::promise<ThreadContext*> thread_context; | ||
| std::thread thread([&loop, &thread_context, thread_name]() { | ||
| g_thread_context.thread_name = ThreadName(loop.m_exe_name) + " (from " + thread_name + ")"; |
There was a problem hiding this comment.
In commit "Add makePool method on ThreadMap" (d77e897)
Not important would probably be a little better to drop "from " + here.
Format for client thread names is
client_exe_name-123/client_thread_name-456And for server thread names
server_exe_name-234/server_thread_name-567 (from client_exe_name-123/client_thread_name-456)Where numbers are process/thread ids. This code would make the server thread name look like:
server_exe_name-234/server_thread_name-567 (from pool/0)Which makes pool/0 look ilke a client process.
This patch introduces a pool of threads to the `Connection` class, and allows this pool to be populated with the thread map via `makePool`. When a client thread is not set in a request context, it is delegated to the pool. The pool is implemented as shortest-queue, where the thread with the shortest list of pending work handles the request. Tiebreaking is by lowest index. This was raised to me by Rust users, as they did not particularly care where work is executed on the server-side, but they have to set the thread regardless. ref: https://github.com/2140-dev/bitcoin-capnp-types/blob/master/tests/util/bitcoin_core.rs#L149
d77e897 to
0e49d91
Compare
|
Dropped FYI will not be able to rebase again until July 6th if more changes are required |
|
re-ACK 0e49d91 |
|
re-ACK 0e49d91 |
16bf05d Merge bitcoin-core/libmultiprocess#302: refactor: rename EventLoop::m_num_clients to m_num_refs dd537da Merge bitcoin-core/libmultiprocess#301: test: recursive async IPC calls and cleanups 400291d Merge bitcoin-core/libmultiprocess#299: ci: remove libevent from Core CIs 092be51 Merge bitcoin-core/libmultiprocess#285: Add ReadList helper 5b61788 Merge bitcoin-core/libmultiprocess#283: Add `makePool` method on `ThreadMap` d499830 refactor: rename EventLoop::m_num_clients to m_num_refs 6450345 type: reserve first when reading std::unordered_set 4d0f8db proxy: add ReadList helper and dedup map/set/vector read handlers 0e49d91 Add `makePool` method on `ThreadMap` 5519f7f test: recursive async IPC calls a29ceff ci: remove libevent from Core CIs 8412fcd Merge bitcoin-core/libmultiprocess#295: Mark Waiter m_cv as guarded by m_mutex 1593ee2 Merge bitcoin-core/libmultiprocess#294: test: Add passDouble smoke test 9885d7d Merge bitcoin-core/libmultiprocess#286: proxy-client: fix TSan data race in clientDestroy fa35501 Mark Waiter m_cv as guarded by m_mutex faaedb1 test: Add passDouble smoke test 733c643 Merge bitcoin-core/libmultiprocess#292: type-number: fix clang-tidy modernize-use-nullptr 9cc3479 Merge bitcoin-core/libmultiprocess#291: cmake: Add `mp_headers` custom target 201abd9 Merge bitcoin-core/libmultiprocess#289: cmake: make target_capnp_sources use CURRENT dirs 99820c8 Merge bitcoin-core/libmultiprocess#279: doc: Add comments to FIELD_* constants in proxy.h 73b9855 Merge bitcoin-core/libmultiprocess#278: doc: Fix and expand design.md e7e91b2 Merge bitcoin-core/libmultiprocess#277: Add std::unordered_set support and a helper BuildList to dedup list build handlers 91a951f tidy fix: modernize-use-nullptr 16362f4 cmake: Add `mp_headers` custom target 615a94f cmake: document ONLY_CAPNP option in target_capnp_sources 90982f7 mpgen: iwyu changes required by previous commit 25bb3e6 proxy-client: fix TSan data race in clientDestroy 620f297 cmake: make target_capnp_sources use CURRENT dirs 9de4b88 test: use camelCase + $Proxy.name for FooStruct fields 011b917 type: add std::unordered_set support 20d19b9 proxy: add BuildList helper and dedup map/set/vector build handlers e863c6c doc: Add comments to FIELD_* constants in proxy.h 18db0ab doc: Fix and expand design.md 61de697 Merge bitcoin-core/libmultiprocess#273: proxy-client: tolerate exceptions from remote destroy during cleanup 9cec9d6 Merge bitcoin-core/libmultiprocess#243: mpgen: support primitive std::optional struct fields 4aaff11 Merge bitcoin-core/libmultiprocess#238: cmake, ci: updates for recent nixpkgs 2ac55a5 Merge bitcoin-core/libmultiprocess#218: Better error and log messages 6de92e1 proxy-client: tolerate exceptions from remote destroy during cleanup 90be835 test: regression for ~ProxyClient destroy after peer disconnect 3c69d12 Merge bitcoin-core/libmultiprocess#260: event loop: tolerate unexpected exceptions in `post()` callbacks b8a48c6 event loop: tolerate unexpected exceptions in `post()` callbacks f787863 Merge bitcoin-core/libmultiprocess#270: doc: Bump version 10 > 11 a22f602 doc: Bump version 10 > 11 4eae445 debug: Add TypeName() function and log statements for Proxy objects being created and destroyed f326c5b logging: Add better logging on IPC server-side failures 6dbfa56 mpgen: support primitive std::optional struct fields 8d1277d mpgen refactor: add AccessorType function db716bb mpgen refactor: Move field handling code to FieldList class db7acb3 ci: Fix shell.nix compatibility with CMake 4.0 91a7759 cmake: Fix IWYU in nix by adding CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES git-subtree-dir: src/ipc/libmultiprocess git-subtree-split: 16bf05d
…ol` method 6b0a907 Squashed 'src/ipc/libmultiprocess/' changes from 3edbe8f..16bf05d (Ryan Ofsky) Pull request description: The changes can be verified by running `test/lint/git-subtree-check.sh src/ipc/libmultiprocess` as described in [developer notes](https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#subtrees) and [lint instructions](https://github.com/bitcoin/bitcoin/tree/master/test/lint#git-subtree-checksh). Changes since last subtree update (#34977): - Adds `makePool` method on `ThreadMap` to support thread pool routing, allowing requests without a specific client thread to be dispatched to a pool using a shortest-queue strategy ([#283](bitcoin-core/libmultiprocess#283)). - Adds `std::unordered_set` support, a `BuildList` helper, and a `ReadList` helper to reduce duplication in list build and read handlers ([#277](bitcoin-core/libmultiprocess#277), [#285](bitcoin-core/libmultiprocess#285)). - Adds support for translating C++ `std::optional<T>` struct fields to pairs of `T` + `hasT :Bool` Cap'n Proto struct fields, allowing unset optional primitive fields to be represented ([#243](bitcoin-core/libmultiprocess#243)). - Produces more readable log output for Proxy object lifecycle events and IPC server-side failures ([#218](bitcoin-core/libmultiprocess#218)). - Handles exceptions thrown by `destroy` methods by logging instead of aborting ([#273](bitcoin-core/libmultiprocess#273)). This can prevent server crashes when non-libmultiprocess clients disconnect without destroying objects, in the case where a server object owns client objects and the server destructor tries to call the disconnected client to free them ([#219](bitcoin-core/libmultiprocess#219)). - Handles unexpected exceptions thrown by callbacks (that should never happen) by logging errors instead of deadlocking ([#260](bitcoin-core/libmultiprocess#260)). - Fixes a rare mptest hang on musl builds caused by a lost wakeup bug in `Waiter` ([#295](bitcoin-core/libmultiprocess#295)). - Fixes a race condition in a log print detected by TSan ([#286](bitcoin-core/libmultiprocess#286)). - Build improvements: makes `target_capnp_sources` work correctly when libmultiprocess is used as a CMake subproject ([#289](bitcoin-core/libmultiprocess#289)), adds `mp_headers` target for better lint tool support ([#291](bitcoin-core/libmultiprocess#291)), and fixes compatibility with recent Nix and CMake 4.0 ([#238](bitcoin-core/libmultiprocess#238)). - Test, CI, documentation, and minor code improvements: design document corrections ([#278](bitcoin-core/libmultiprocess#278)), field constant comments ([#279](bitcoin-core/libmultiprocess#279)), clang-tidy fix ([#292](bitcoin-core/libmultiprocess#292)), new smoke test for double-precision float values ([#294](bitcoin-core/libmultiprocess#294)), new test for recursive async IPC calls ([#301](bitcoin-core/libmultiprocess#301)), removal of libevent from Core CI builds ([#299](bitcoin-core/libmultiprocess#299)), and rename of `EventLoop::m_num_clients` to `m_num_refs` ([#302](bitcoin-core/libmultiprocess#302)). ACKs for top commit: fanquake: ACK 02afa66 hebasto: ACK 02afa66. Tree-SHA512: ef81a951c971f328a0a98436030467eeea30925eb6016eafd9bc7a25726c87628a852bbb1d84b88bce340aeea2bed25c65bc55db1168ebcb850628cd18808883
…hreadMap.makePool` method 6b0a907 Squashed 'src/ipc/libmultiprocess/' changes from 3edbe8f67c1..16bf05dea02 (Ryan Ofsky) Pull request description: The changes can be verified by running `test/lint/git-subtree-check.sh src/ipc/libmultiprocess` as described in [developer notes](https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#subtrees) and [lint instructions](https://github.com/bitcoin/bitcoin/tree/master/test/lint#git-subtree-checksh). Changes since last subtree update (#34977): - Adds `makePool` method on `ThreadMap` to support thread pool routing, allowing requests without a specific client thread to be dispatched to a pool using a shortest-queue strategy ([#283](bitcoin-core/libmultiprocess#283)). - Adds `std::unordered_set` support, a `BuildList` helper, and a `ReadList` helper to reduce duplication in list build and read handlers ([#277](bitcoin-core/libmultiprocess#277), [#285](bitcoin-core/libmultiprocess#285)). - Adds support for translating C++ `std::optional<T>` struct fields to pairs of `T` + `hasT :Bool` Cap'n Proto struct fields, allowing unset optional primitive fields to be represented ([#243](bitcoin-core/libmultiprocess#243)). - Produces more readable log output for Proxy object lifecycle events and IPC server-side failures ([#218](bitcoin-core/libmultiprocess#218)). - Handles exceptions thrown by `destroy` methods by logging instead of aborting ([#273](bitcoin-core/libmultiprocess#273)). This can prevent server crashes when non-libmultiprocess clients disconnect without destroying objects, in the case where a server object owns client objects and the server destructor tries to call the disconnected client to free them ([#219](bitcoin-core/libmultiprocess#219)). - Handles unexpected exceptions thrown by callbacks (that should never happen) by logging errors instead of deadlocking ([#260](bitcoin-core/libmultiprocess#260)). - Fixes a rare mptest hang on musl builds caused by a lost wakeup bug in `Waiter` ([#295](bitcoin-core/libmultiprocess#295)). - Fixes a race condition in a log print detected by TSan ([#286](bitcoin-core/libmultiprocess#286)). - Build improvements: makes `target_capnp_sources` work correctly when libmultiprocess is used as a CMake subproject ([#289](bitcoin-core/libmultiprocess#289)), adds `mp_headers` target for better lint tool support ([#291](bitcoin-core/libmultiprocess#291)), and fixes compatibility with recent Nix and CMake 4.0 ([#238](bitcoin-core/libmultiprocess#238)). - Test, CI, documentation, and minor code improvements: design document corrections ([#278](bitcoin-core/libmultiprocess#278)), field constant comments ([#279](bitcoin-core/libmultiprocess#279)), clang-tidy fix ([#292](bitcoin-core/libmultiprocess#292)), new smoke test for double-precision float values ([#294](bitcoin-core/libmultiprocess#294)), new test for recursive async IPC calls ([#301](bitcoin-core/libmultiprocess#301)), removal of libevent from Core CI builds ([#299](bitcoin-core/libmultiprocess#299)), and rename of `EventLoop::m_num_clients` to `m_num_refs` ([#302](bitcoin-core/libmultiprocess#302)). ACKs for top commit: fanquake: ACK cd09e20 hebasto: ACK cd09e20. Tree-SHA512: ef81a951c971f328a0a98436030467eeea30925eb6016eafd9bc7a25726c87628a852bbb1d84b88bce340aeea2bed25c65bc55db1168ebcb850628cd18808883
e8de5c7b68 Merge bitcoin-core/libmultiprocess#305: refactor: memcpy to std::ranges::copy to work around ubsan warn 9307e68e5a Merge bitcoin-core/libmultiprocess#306: doc: Bump version 12 > 13 fac7b9b7f6 refactor: memcpy to std::ranges::copy to work around ubsan warn 1bd7025609 Merge bitcoin-core/libmultiprocess#297: test: add map serialization round-trip coverage 438fdd243d doc: Bump version 12 > 13 28e056576a Merge bitcoin-core/libmultiprocess#269: proxy: add local connection limit to ListenConnections 39a10ce895 proxy: add local connection limit to ListenConnections() 43172f52d9 test: add dedicated ListenConnections coverage 033f812195 doc/version: Bump version 11 > 12 463d073cb8 test: rename vBool to vector_bool 16bf05dea0 Merge bitcoin-core/libmultiprocess#302: refactor: rename EventLoop::m_num_clients to m_num_refs dd537da9e4 Merge bitcoin-core/libmultiprocess#301: test: recursive async IPC calls and cleanups 400291de00 Merge bitcoin-core/libmultiprocess#299: ci: remove libevent from Core CIs 092be515ad Merge bitcoin-core/libmultiprocess#285: Add ReadList helper 5b617880c5 Merge bitcoin-core/libmultiprocess#283: Add `makePool` method on `ThreadMap` d499830415 refactor: rename EventLoop::m_num_clients to m_num_refs 6450345c98 type: reserve first when reading std::unordered_set 4d0f8db5f9 proxy: add ReadList helper and dedup map/set/vector read handlers 0e49d91186 Add `makePool` method on `ThreadMap` 5519f7f948 test: recursive async IPC calls a29ceff40b ci: remove libevent from Core CIs 85df233845 test: add mapStringInt to foo.capnp to cover map serialization and deserialization git-subtree-dir: src/ipc/libmultiprocess git-subtree-split: e8de5c7b68e0ae21c94ae92aa22e5c3b213f9c12
e8de5c7b68 Merge bitcoin-core/libmultiprocess#305: refactor: memcpy to std::ranges::copy to work around ubsan warn 9307e68e5a Merge bitcoin-core/libmultiprocess#306: doc: Bump version 12 > 13 fac7b9b7f6 refactor: memcpy to std::ranges::copy to work around ubsan warn 1bd7025609 Merge bitcoin-core/libmultiprocess#297: test: add map serialization round-trip coverage 438fdd243d doc: Bump version 12 > 13 28e056576a Merge bitcoin-core/libmultiprocess#269: proxy: add local connection limit to ListenConnections 39a10ce895 proxy: add local connection limit to ListenConnections() 43172f52d9 test: add dedicated ListenConnections coverage 033f812195 doc/version: Bump version 11 > 12 463d073cb8 test: rename vBool to vector_bool 16bf05dea0 Merge bitcoin-core/libmultiprocess#302: refactor: rename EventLoop::m_num_clients to m_num_refs dd537da9e4 Merge bitcoin-core/libmultiprocess#301: test: recursive async IPC calls and cleanups 400291de00 Merge bitcoin-core/libmultiprocess#299: ci: remove libevent from Core CIs 092be515ad Merge bitcoin-core/libmultiprocess#285: Add ReadList helper 5b617880c5 Merge bitcoin-core/libmultiprocess#283: Add `makePool` method on `ThreadMap` d499830415 refactor: rename EventLoop::m_num_clients to m_num_refs 6450345c98 type: reserve first when reading std::unordered_set 4d0f8db5f9 proxy: add ReadList helper and dedup map/set/vector read handlers 0e49d91186 Add `makePool` method on `ThreadMap` 5519f7f948 test: recursive async IPC calls a29ceff40b ci: remove libevent from Core CIs 85df233845 test: add mapStringInt to foo.capnp to cover map serialization and deserialization git-subtree-dir: src/ipc/libmultiprocess git-subtree-split: e8de5c7b68e0ae21c94ae92aa22e5c3b213f9c12
…hreadMap.makePool` method 6b0a907 Squashed 'src/ipc/libmultiprocess/' changes from 3edbe8f67c1..16bf05dea02 (Ryan Ofsky) Pull request description: The changes can be verified by running `test/lint/git-subtree-check.sh src/ipc/libmultiprocess` as described in [developer notes](https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#subtrees) and [lint instructions](https://github.com/bitcoin/bitcoin/tree/master/test/lint#git-subtree-checksh). Changes since last subtree update (#34977): - Adds `makePool` method on `ThreadMap` to support thread pool routing, allowing requests without a specific client thread to be dispatched to a pool using a shortest-queue strategy ([#283](bitcoin-core/libmultiprocess#283)). - Adds `std::unordered_set` support, a `BuildList` helper, and a `ReadList` helper to reduce duplication in list build and read handlers ([#277](bitcoin-core/libmultiprocess#277), [#285](bitcoin-core/libmultiprocess#285)). - Adds support for translating C++ `std::optional<T>` struct fields to pairs of `T` + `hasT :Bool` Cap'n Proto struct fields, allowing unset optional primitive fields to be represented ([#243](bitcoin-core/libmultiprocess#243)). - Produces more readable log output for Proxy object lifecycle events and IPC server-side failures ([#218](bitcoin-core/libmultiprocess#218)). - Handles exceptions thrown by `destroy` methods by logging instead of aborting ([#273](bitcoin-core/libmultiprocess#273)). This can prevent server crashes when non-libmultiprocess clients disconnect without destroying objects, in the case where a server object owns client objects and the server destructor tries to call the disconnected client to free them ([#219](bitcoin-core/libmultiprocess#219)). - Handles unexpected exceptions thrown by callbacks (that should never happen) by logging errors instead of deadlocking ([#260](bitcoin-core/libmultiprocess#260)). - Fixes a rare mptest hang on musl builds caused by a lost wakeup bug in `Waiter` ([#295](bitcoin-core/libmultiprocess#295)). - Fixes a race condition in a log print detected by TSan ([#286](bitcoin-core/libmultiprocess#286)). - Build improvements: makes `target_capnp_sources` work correctly when libmultiprocess is used as a CMake subproject ([#289](bitcoin-core/libmultiprocess#289)), adds `mp_headers` target for better lint tool support ([#291](bitcoin-core/libmultiprocess#291)), and fixes compatibility with recent Nix and CMake 4.0 ([#238](bitcoin-core/libmultiprocess#238)). - Test, CI, documentation, and minor code improvements: design document corrections ([#278](bitcoin-core/libmultiprocess#278)), field constant comments ([#279](bitcoin-core/libmultiprocess#279)), clang-tidy fix ([#292](bitcoin-core/libmultiprocess#292)), new smoke test for double-precision float values ([#294](bitcoin-core/libmultiprocess#294)), new test for recursive async IPC calls ([#301](bitcoin-core/libmultiprocess#301)), removal of libevent from Core CI builds ([#299](bitcoin-core/libmultiprocess#299)), and rename of `EventLoop::m_num_clients` to `m_num_refs` ([#302](bitcoin-core/libmultiprocess#302)). ACKs for top commit: fanquake: ACK 02afa66169b5dc58d0fc6f608d0c6f4facefd5ec hebasto: ACK 02afa66169b5dc58d0fc6f608d0c6f4facefd5ec. Tree-SHA512: ef81a951c971f328a0a98436030467eeea30925eb6016eafd9bc7a25726c87628a852bbb1d84b88bce340aeea2bed25c65bc55db1168ebcb850628cd18808883
2dba13047 Merge bitcoin-core/libmultiprocess#363: ci: use LLVM 23 in Bitcoin Core CI 161197a5c Merge bitcoin-core/libmultiprocess#352: ci: add cmake debug output fb4ac7eb8 ci: use LLVM 23 in Bitcoin Core CI 7bac69de1 Merge bitcoin-core/libmultiprocess#360: pull latest .clang-tidy from downstream 79ddc44eb Merge bitcoin-core/libmultiprocess#359: ci: bump cmake version to 4.3.4 in newdeps job bf229bf82 Merge bitcoin-core/libmultiprocess#351: ci: do not ignore NIXPKGS_CHANNEL in local ci runs 073ac4f19 Merge bitcoin-core/libmultiprocess#347: refactor: Replace EventLoop::post() with sync() taking kj::FunctionParam fa1db7a9e pull latest .clang-tidy from downstream 5c49666a1 refactor: rename EventLoop::m_post_fn to m_sync_fn 2330fbe81 refactor: replace EventLoop::post() with sync() taking kj::FunctionParam 4d454a81d Merge bitcoin-core/libmultiprocess#358: refactor: Enable readability-container-contains dba99582b Merge bitcoin-core/libmultiprocess#357: doc: Update Cap'n Proto version to match minimum 00923922a Merge bitcoin-core/libmultiprocess#356: ci: Remove hard-coded -j4 from sanitize config 766867fb3 ci: clarify CAPNP_CHECKOUT=master is the v1.x release branch cc3675280 ci: bump cmake version to 4.3.4 in newdeps job fa101113b refactor: Enable readability-container-contains 81f824b02 doc: Update Cap'n Proto version to match minimum fa30e2093 ci: Remove hard-coded -j4 from sanitize config a7ff9d5da ci: add cmake debug output f2e8df82e Merge bitcoin-core/libmultiprocess#350: cmake: add type-unordered-set.h and version.h to public headers 0e146c046 Merge bitcoin-core/libmultiprocess#349: type-context: fix async disconnect race condition found by antithesis 49f95e263 Merge bitcoin-core/libmultiprocess#212: ci: add newdeps job testing newer versions of cmake and capnproto 7c73cceda ci: rename CI-internal variables to use CI_ prefix fe1b8339f ci: do not ignore NIXPKGS_CHANNEL in local ci runs 914dc839f proxy: fix data race between server request threads and disconnect handling 275c8eefd Merge bitcoin-core/libmultiprocess#345: Remove trailing whitespace and Add -Wtrailing-whitespace to default ci config cd7162fb8 Merge bitcoin-core/libmultiprocess#304: proxy: fix BuildList to use non-const iteration for interface types 9b136782a ci: Add -Wtrailing-whitespace to default config 2f4be9ec6 refactor: Remove trailing whitespace 2448d282c cmake: add type-unordered-set.h and version.h to public headers b3fc922ee ci: add newdeps job testing newest versions of cmake and capnproto 390b5f901 Merge bitcoin-core/libmultiprocess#344: test: listen_tests and connect_tests follow-ups d6f8588d1 proxy: fix BuildList to use non-const iteration for interface types e18ca520f Merge bitcoin-core/libmultiprocess#343: test: fix race in connect_tests disconnect-deferred-failure test c39c7850c doc: note construct() call in valid init interface test b9c36c617 test: close sockets unconditionally and check errors with KJ_SYSCALL 7eb741e63 test: drop unnecessary KJ_EXPECT(true) 113f1d4d2 test: join server thread unconditionally in connect tests 44bc4630b test: drop mp:: prefixes in connect tests 038d33eb3 test: share DefaultLogHandler between test files b54a16330 test: drop TestSetup socket members in connect tests 70467c5a7 test: add m_ prefix to TestSetup members in connect tests cc260f252 test: replace capnp fix link with upstream PR 137a6e4e0 test: fix race in connect_tests disconnect-deferred-failure test 8dab0d4bd Merge bitcoin-core/libmultiprocess#341: ci: add -Wextra-semi to llvm config b3b134eed ci: add -Wextra-semi to llvm config bdd0cd694 Merge bitcoin-core/libmultiprocess#339: refactor: add `[[noreturn]]` attributes a779a0976 ci: add -Wmissing-noreturn 636aaff57 refactor: add missing [[noreturn]] attributes cc11c2b1b Merge bitcoin-core/libmultiprocess#338: test: check ReadList return value 2d6e863c7 Merge bitcoin-core/libmultiprocess#334: ci: Set CMAKE_BUILD_PARALLEL_LEVEL to enable parallelism by default d4d10ff98 Merge bitcoin-core/libmultiprocess#332: ci: add -Wextra-semi to default config b540e70f2 Merge bitcoin-core/libmultiprocess#324: proxy: Name threads spawned by the event loop e5e367e78 Merge bitcoin-core/libmultiprocess#312: util: report back child errors to parent and throw 2220df68c Merge bitcoin-core/libmultiprocess#298: Fix error handling when creating clients (`mp::ConnectStream`) 51defb79e Merge bitcoin-core/libmultiprocess#340: ci: Update `capnproto` prerequisites on NetBSD 7e94790b0 ci: Update `capnproto` prerequisites on NetBSD 9f25ffca5 test: Cover OS thread names for worker, pool, and async threads 648a18589 proxy: Name threads spawned by the event loop 49834b260 ci: add -Wextra-semi to default config fae9a637e example: Remove unused kj/async.h include bb473690c Fix error handling when creating clients 44d191420 Add test coverage for ConnectStream 231361ae5 Correct stale UnixListener doc comment 060c1a50d Extract `UnixListener` class to a dedicated file 62f25af06 test: check ReadList return value ce51d7372 ci: Set CMAKE_BUILD_PARALLEL_LEVEL to enable parallism in build jobs by default 67302cd13 Merge bitcoin-core/libmultiprocess#331: Remove code for Cap'n Proto versions before 0.9 f13c64ab5 Merge bitcoin-core/libmultiprocess#330: ci: Compile with minimum supported g++ in olddeps 8e026f662 Merge bitcoin-core/libmultiprocess#327: build: avoid unnecessary capnp-rpc dependency for mpgen e5206e9eb Merge bitcoin-core/libmultiprocess#325: cmake: Remove `QUIET` option from `find_package(CapnProto ...)` 879efea2b Merge bitcoin-core/libmultiprocess#321: ci: Roll NetBSD releases to 11.0, drop 9.4 abf127a31 Merge bitcoin-core/libmultiprocess#317: ipc: Fix mpgen capnp tool path for vcpkg/Windows builds c437d7f10 Merge bitcoin-core/libmultiprocess#310: test: cover immediate client disconnects for `ListenConnections` 31bff8a67 Merge bitcoin-core/libmultiprocess#307: refactor: memcpy -> std::ranges::copy f355108b0 Merge bitcoin-core/libmultiprocess#303: type-chrono: Add CustomBuildField/CustomReadField overloads for std::chrono::time_point 2d678177c Merge bitcoin-core/libmultiprocess#296: ci: Bump channel to nixos-26.05 3f05b1162 util: kill and reap child on SpawnProcess error 4a56c1837 util: report back child error to parent and throw a9e70dbe7 ci: Add NetBSD release 11.0 2d33b14fb ci: Switch to default compiler on NetBSD 9.4 36f740027 ci: Drop NetBSD release 9.4 bd508311b refactor: Drop stray semicolons after function definitions 788f17a85 Remove code for Cap'n Proto versions before 0.9 7402affd0 ci: Pin oldeps config to older nixpkgs channel to compile older cmake with older gcc edf634356 ci: Compile with minimum supported g++-11 in olddeps fa47449af cmake: avoid unnecessary capnp-rpc dependency for mpgen a494b764d cmake: Remove `QUIET` option from `find_package(CapnProto ...)` 26452e02d refactor: memcpy -> std::ranges::copy e1dcc6eb1 Merge bitcoin-core/libmultiprocess#316: cmake: Fix stale codegen when mpgen binary changes 7a72df02e type-chrono: Add CustomBuildField/CustomReadField overloads for std::chrono::time_point 45b685c3f type-number, type-chrono: Fix static assert signed/unsigned comparisons 45f625597 type-number: exclude bool from the integral overload 8d6d46494 Merge bitcoin-core/libmultiprocess#315: Fix startup race in example a6fc80d25 Merge bitcoin-core/libmultiprocess#311: bugfix: clear FD_CLOEXEC in child instead of parent before fork 496fb84e6 test: cover immediate client disconnects for `ListenConnections` 36c6c6352 doc: Document reference-counted EventLoop lifetime 3a997e113 Fix startup race in mpexample f5c15ce33 Merge bitcoin-core/libmultiprocess#323: refactor: access ThreadContext through CurrentThread(), ci: switch Bitcoin Core to master 66298c737 ci: Switch back to Bitcoin Core's master branch 86b481050 refactor: access ThreadContext through CurrentThread() eea9c64f6 cmake: Fix stale codegen when mpgen binary changes a26a08496 cmake: Fix mpgen capnp tool path for vcpkg/Windows builds 140d9ba6f test: allow custom log handler in `ListenSetup` 1e0c7ff9a util: Clear FD_CLOEXEC in child instead of parent before fork 8550ee6a3 util, refactor: Add ChildFail helper for post-fork child errors 17eab90b5 test: Fix typo in listen_tests.cpp ce865a9ba refactor: Directly use value in CustomBuildField 3f221b5bf Merge bitcoin-core/libmultiprocess#274: Add nonunix platform support e8de5c7b6 Merge bitcoin-core/libmultiprocess#305: refactor: memcpy to std::ranges::copy to work around ubsan warn 1b0f60560 doc: Remove trailing whitespace d8f8ca311 ipc: Wrap mpgen main() in try-catch to print errors fbe5a14ad ci: Check out bitcoin/bitcoin PR #35084 instead of master 39d3690d8 types: Replace SFINAE with requires clauses to avoid MSVC C2039 error ba6852020 proxy, refactor: Fix C4305 truncation warning in Accessor on MSVC 1d81d4781 util, refactor: Fix PtrOrValue constructor for move-only types on MSVC b883fe1e5 proxy: Fix shutdownWrite() exception handling on macOS with dynamic libraries 0012411cc proxy: Call shutdownWrite() in Connection destructor 38312ad19 proxy, refactor: Change ConnectStream and ServeStream to accept stream objects e96d5d742 proxy, refactor: Replace EventLoop wakeup fd integers with KJ stream objects db4f9a3d7 cmake: Bump minimum required Cap'n Proto version to 0.9 652934fb7 util, refactor: Add SocketPair() and use it in SpawnProcess 1c6ef7a26 util, refactor: Do not fork() and exec() separately 1389cf313 util, refactor: Add SpawnConnectInfo type alias and use it c7ca1f00b util, refactor: Add SocketId type alias and use it be46a3520 util, refactor: Add ProcessId type alias and use it 91a78db78 doc: Bump version 13 > 14 9307e68e5 Merge bitcoin-core/libmultiprocess#306: doc: Bump version 12 > 13 fac7b9b7f refactor: memcpy to std::ranges::copy to work around ubsan warn 1bd702560 Merge bitcoin-core/libmultiprocess#297: test: add map serialization round-trip coverage 438fdd243 doc: Bump version 12 > 13 28e056576 Merge bitcoin-core/libmultiprocess#269: proxy: add local connection limit to ListenConnections 39a10ce89 proxy: add local connection limit to ListenConnections() 43172f52d test: add dedicated ListenConnections coverage 033f81219 doc/version: Bump version 11 > 12 463d073cb test: rename vBool to vector_bool 16bf05dea Merge bitcoin-core/libmultiprocess#302: refactor: rename EventLoop::m_num_clients to m_num_refs dd537da9e Merge bitcoin-core/libmultiprocess#301: test: recursive async IPC calls and cleanups 400291de0 Merge bitcoin-core/libmultiprocess#299: ci: remove libevent from Core CIs 092be515a Merge bitcoin-core/libmultiprocess#285: Add ReadList helper 5b617880c Merge bitcoin-core/libmultiprocess#283: Add `makePool` method on `ThreadMap` d49983041 refactor: rename EventLoop::m_num_clients to m_num_refs 6450345c9 type: reserve first when reading std::unordered_set 4d0f8db5f proxy: add ReadList helper and dedup map/set/vector read handlers 0e49d9118 Add `makePool` method on `ThreadMap` 5519f7f94 test: recursive async IPC calls a29ceff40 ci: remove libevent from Core CIs 85df23384 test: add mapStringInt to foo.capnp to cover map serialization and deserialization fa2c56ec2 ci: Bump channel to nixos-26.05 8412fcdc6 Merge bitcoin-core/libmultiprocess#295: Mark Waiter m_cv as guarded by m_mutex 1593ee2d1 Merge bitcoin-core/libmultiprocess#294: test: Add passDouble smoke test 9885d7dd3 Merge bitcoin-core/libmultiprocess#286: proxy-client: fix TSan data race in clientDestroy fa35501c4 Mark Waiter m_cv as guarded by m_mutex faaedb11f test: Add passDouble smoke test 733c64318 Merge bitcoin-core/libmultiprocess#292: type-number: fix clang-tidy modernize-use-nullptr 9cc3479ab Merge bitcoin-core/libmultiprocess#291: cmake: Add `mp_headers` custom target 201abd9e3 Merge bitcoin-core/libmultiprocess#289: cmake: make target_capnp_sources use CURRENT dirs 99820c8ae Merge bitcoin-core/libmultiprocess#279: doc: Add comments to FIELD_* constants in proxy.h 73b985540 Merge bitcoin-core/libmultiprocess#278: doc: Fix and expand design.md e7e91b2e2 Merge bitcoin-core/libmultiprocess#277: Add std::unordered_set support and a helper BuildList to dedup list build handlers 91a951f59 tidy fix: modernize-use-nullptr 16362f42d cmake: Add `mp_headers` custom target 615a94fe3 cmake: document ONLY_CAPNP option in target_capnp_sources 90982f75c mpgen: iwyu changes required by previous commit 25bb3e67f proxy-client: fix TSan data race in clientDestroy 620f297f3 cmake: make target_capnp_sources use CURRENT dirs 9de4b885a test: use camelCase + $Proxy.name for FooStruct fields 011b91793 type: add std::unordered_set support 20d19b964 proxy: add BuildList helper and dedup map/set/vector build handlers e863c6cdf doc: Add comments to FIELD_* constants in proxy.h 18db0ab95 doc: Fix and expand design.md git-subtree-dir: src/ipc/libmultiprocess git-subtree-split: 2dba130478ab71a745fe96c5726719fe357e6d17
This patch introduces a pool of threads to the
Connectionclass, and allows this pool to be populated with the thread map viamakePool. When a client thread is not set in a request context, it is delegated to the pool. This is unable to handle the guarentees with server-invoked callbacks that the current API offers, but these callbacks are not yet present in the interface.The pool is implemented as
round-robin as it is simplestshortest queue, but perhaps the pool could be a queue of requests with work-stealing for threads that are available.This was raised to me by Rust users, as they did not particularly care where work is executed on the server-side, but they have to set the thread regardless.
Tests in Rust can be seen here: 2140-dev/bitcoin-capnp-types#24
ref: https://github.com/2140-dev/bitcoin-capnp-types/blob/master/tests/util/bitcoin_core.rs#L149
ref: #281