diff --git a/src/ipc/libmultiprocess/.github/workflows/bitcoin-core-ci.yml b/src/ipc/libmultiprocess/.github/workflows/bitcoin-core-ci.yml index e6ac83f0..a48b186a 100644 --- a/src/ipc/libmultiprocess/.github/workflows/bitcoin-core-ci.yml +++ b/src/ipc/libmultiprocess/.github/workflows/bitcoin-core-ci.yml @@ -43,7 +43,6 @@ jobs: clang-22 llvm-22 libclang-rt-22-dev - libevent-dev libboost-dev libsqlite3-dev libcapnp-dev @@ -65,7 +64,7 @@ jobs: nproc_multiplier: 2 functional_timeout_factor: 40 runner: macos-15 - brew-packages: ccache capnp boost libevent sqlite pkgconf ninja + brew-packages: ccache capnp boost sqlite pkgconf ninja pip-packages: --break-system-packages pycapnp cmake-args: |- -DREDUCE_EXPORTS=ON diff --git a/src/ipc/libmultiprocess/doc/usage.md b/src/ipc/libmultiprocess/doc/usage.md index f387db4d..df103e9f 100644 --- a/src/ipc/libmultiprocess/doc/usage.md +++ b/src/ipc/libmultiprocess/doc/usage.md @@ -10,7 +10,7 @@ _libmultiprocess_ is a library and code generator that allows calling C++ class The `*.capnp` data definition files are consumed by the _libmultiprocess_ code generator and each `X.capnp` file generates `X.capnp.c++`, `X.capnp.h`, `X.capnp.proxy-client.c++`, `X.capnp.proxy-server.c++`, `X.capnp.proxy-types.c++`, `X.capnp.proxy-types.h`, and `X.capnp.proxy.h` output files. The generated files include `mp::ProxyClient` and `mp::ProxyServer` class specializations for all the interfaces in the `.capnp` files. These allow methods on C++ objects in one process to be called from other processes over IPC sockets. -The `ProxyServer` objects help translate IPC requests from a socket to method calls on a local object. The `ProxyServer` objects are just used internally by the `mp::ServeStream(loop, socket, wrapped_object)` and `mp::ListenConnections(loop, socket, wrapped_object)` functions, and aren't exposed externally. The `ProxyClient` classes are exposed, and returned from the `mp::ConnectStream(loop, socket)` function and meant to be used directly. The classes implement methods described in `.capnp` definitions, and whenever any method is called, a request with the method arguments is sent over the associated IPC connection, and the corresponding `wrapped_object` method on the other end of the connection is called, with the `ProxyClient` method blocking until it returns and forwarding back any return value to the `ProxyClient` method caller. +The `ProxyServer` objects help translate IPC requests from a socket to method calls on a local object. The `ProxyServer` objects are just used internally by the `mp::ServeStream(loop, socket, wrapped_object)` and `mp::ListenConnections(loop, socket, wrapped_object[, max_connections])` functions, and aren't exposed externally. The `ProxyClient` classes are exposed, and returned from the `mp::ConnectStream(loop, socket)` function and meant to be used directly. The classes implement methods described in `.capnp` definitions, and whenever any method is called, a request with the method arguments is sent over the associated IPC connection, and the corresponding `wrapped_object` method on the other end of the connection is called, with the `ProxyClient` method blocking until it returns and forwarding back any return value to the `ProxyClient` method caller. ## Example diff --git a/src/ipc/libmultiprocess/doc/versions.md b/src/ipc/libmultiprocess/doc/versions.md index 3cfa28e3..35ae76bd 100644 --- a/src/ipc/libmultiprocess/doc/versions.md +++ b/src/ipc/libmultiprocess/doc/versions.md @@ -7,77 +7,97 @@ Library versions are tracked with simple Versioning policy is described in the [version.h](../include/mp/version.h) include. -## v11 +## v13 - Current unstable version. +## [v12.0](https://github.com/bitcoin-core/libmultiprocess/commits/v12.0) +- Adds an optional `max_connections` parameter to `ListenConnections` ([#269](https://github.com/bitcoin-core/libmultiprocess/pull/269)). +- Used in Bitcoin Core 32.x and newer releases with [#35684](https://github.com/bitcoin/bitcoin/pull/35684). + +## [v11.0](https://github.com/bitcoin-core/libmultiprocess/commits/v11.0) +- 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](https://github.com/bitcoin-core/libmultiprocess/pull/283)). +- Adds `std::unordered_set` support, a `BuildList` helper, and a `ReadList` helper to reduce duplication in list build and read handlers ([#277](https://github.com/bitcoin-core/libmultiprocess/pull/277), [#285](https://github.com/bitcoin-core/libmultiprocess/pull/285)). +- Adds support for translating C++ `std::optional` struct fields to pairs of `T` + `hasT :Bool` Cap'n Proto struct fields, allowing unset optional primitive fields to be represented ([#243](https://github.com/bitcoin-core/libmultiprocess/pull/243)). +- Produces more readable log output for Proxy object lifecycle events and IPC server-side failures ([#218](https://github.com/bitcoin-core/libmultiprocess/pull/218)). +- Handles exceptions thrown by `destroy` methods by logging instead of aborting ([#273](https://github.com/bitcoin-core/libmultiprocess/pull/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](https://github.com/bitcoin-core/libmultiprocess/issues/219)). +- Handles unexpected exceptions thrown by callbacks (that should never happen) by logging errors instead of deadlocking ([#260](https://github.com/bitcoin-core/libmultiprocess/pull/260)). +- Fixes a rare mptest hang on musl builds caused by a lost wakeup bug in `Waiter` ([#295](https://github.com/bitcoin-core/libmultiprocess/pull/295)). +- Fixes a race condition in a log print detected by TSan ([#286](https://github.com/bitcoin-core/libmultiprocess/pull/286)). +- Build improvements: makes `target_capnp_sources` work correctly when libmultiprocess is used as a CMake subproject ([#289](https://github.com/bitcoin-core/libmultiprocess/pull/289)), adds `mp_headers` target for better lint tool support ([#291](https://github.com/bitcoin-core/libmultiprocess/pull/291)), and fixes compatibility with recent Nix and CMake 4.0 ([#238](https://github.com/bitcoin-core/libmultiprocess/pull/238)). +- Test, CI, documentation, and minor code improvements: design document corrections ([#278](https://github.com/bitcoin-core/libmultiprocess/pull/278)), field constant comments ([#279](https://github.com/bitcoin-core/libmultiprocess/pull/279)), clang-tidy fix ([#292](https://github.com/bitcoin-core/libmultiprocess/pull/292)), new smoke test for double-precision float values ([#294](https://github.com/bitcoin-core/libmultiprocess/pull/294)), new test for recursive async IPC calls ([#301](https://github.com/bitcoin-core/libmultiprocess/pull/301)), removal of libevent from Core CI builds ([#299](https://github.com/bitcoin-core/libmultiprocess/pull/299)), and rename of `EventLoop::m_num_clients` to `m_num_refs` ([#302](https://github.com/bitcoin-core/libmultiprocess/pull/302)). +- Used in Bitcoin Core 32.x and newer releases with [#35661](https://github.com/bitcoin/bitcoin/pull/35661). + ## [v10.0](https://github.com/bitcoin-core/libmultiprocess/commits/v10.0) - Increases spawn test timeout to avoid spurious failures. - Uses `throwRecoverableException` instead of raw `throw` to improve runtime error messages in macOS builds. -- Used in Bitcoin Core master branch, pulled in by [#34977](https://github.com/bitcoin/bitcoin/pull/34977). Also pulled into Bitcoin Core 31.x stable branch by [#35028](https://github.com/bitcoin/bitcoin/pull/35028). +- Used in Bitcoin Core 31.0rc3 and newer releases with [#34977](https://github.com/bitcoin/bitcoin/pull/34977) and backport [#35028](https://github.com/bitcoin/bitcoin/pull/35028). ## [v9.0](https://github.com/bitcoin-core/libmultiprocess/commits/v9.0) - Fixes race conditions where worker thread could be used after destruction, where getParams() could be called after request cancel, and where m_on_cancel could be called after request finishes. - Adds `CustomHasField` hook to map Cap'n Proto null values to C++ null values. - Improves `CustomBuildField` for `std::optional` to use move semantics. - Adds LLVM 22 compatibility fix in type-map. -- Used in Bitcoin Core master branch, pulled in by [#34804](https://github.com/bitcoin/bitcoin/pull/34804). Also pulled into Bitcoin Core 31.x stable branch by [#34952](https://github.com/bitcoin/bitcoin/pull/34952). +- Used in Bitcoin Core 31.0rc3 and newer releases with [#34804](https://github.com/bitcoin/bitcoin/pull/34804) and backport [#34952](https://github.com/bitcoin/bitcoin/pull/34952). ## [v8.0](https://github.com/bitcoin-core/libmultiprocess/commits/v8.0) - Better support for non-libmultiprocess IPC clients: avoiding errors on unclean disconnects, and allowing simultaneous requests to worker threads which previously triggered "thread busy" errors. -- Intermediate version used in Bitcoin Core master branch between 30.x and 31.x branches, pulled in by [#34422](https://github.com/bitcoin/bitcoin/pull/34422). +- Used in Bitcoin Core 31.x and newer releases with [#34422](https://github.com/bitcoin/bitcoin/pull/34422). ## [v7.0](https://github.com/bitcoin-core/libmultiprocess/commits/v7.0) - Adds SpawnProcess race fix, cmake `target_capnp_sources` option, ci and documentation improvements. Adds `version.h` header declaring major and minor version numbers. -- Intermediate version used in Bitcoin Core master branch between 30.x and 31.x branches, pulled in by [#34363](https://github.com/bitcoin/bitcoin/pull/34363). +- Used in Bitcoin Core 31.x and newer releases with [#34363](https://github.com/bitcoin/bitcoin/pull/34363). ## [v7.0-pre2](https://github.com/bitcoin-core/libmultiprocess/commits/v7.0-pre2) - Fixes intermittent mptest hang and makes other minor improvements. -- Used in Bitcoin Core 30.1 and 30.2 releases and 30.x branch, pulled in by [#33518](https://github.com/bitcoin/bitcoin/pull/33518) and [#33519](https://github.com/bitcoin/bitcoin/pull/33519). +- Used in Bitcoin Core 30.1 and newer releases with [#33518](https://github.com/bitcoin/bitcoin/pull/33518) and backport [#33519](https://github.com/bitcoin/bitcoin/pull/33519). ## [v7.0-pre1](https://github.com/bitcoin-core/libmultiprocess/commits/v7.0-pre1) - Adds support for log levels to reduce logging and "thread busy" error to avoid a crash on misuse. -- Minimum required version since Bitcoin Core 30.1, pulled in by [#33412](https://github.com/bitcoin/bitcoin/pull/33412), [#33518](https://github.com/bitcoin/bitcoin/pull/33518), and [#33519](https://github.com/bitcoin/bitcoin/pull/33519). +- Used in Bitcoin Core 30.1 and newer releases with [#33412](https://github.com/bitcoin/bitcoin/pull/33412) and [#33518](https://github.com/bitcoin/bitcoin/pull/33518) and backport [#33519](https://github.com/bitcoin/bitcoin/pull/33519). +- Minimum required version since Bitcoin Core 30.1 due to [#33517](https://github.com/bitcoin/bitcoin/pull/33517) and backport [#33609](https://github.com/bitcoin/bitcoin/pull/33609). ## [v6.0](https://github.com/bitcoin-core/libmultiprocess/commits/v6.0) - Adds CI scripts and build and test fixes. -- Used in Bitcoin Core 30.0 release, pulled in by [#32345](https://github.com/bitcoin/bitcoin/pull/32345), [#33241](https://github.com/bitcoin/bitcoin/pull/33241), and [#33322](https://github.com/bitcoin/bitcoin/pull/33322). +- Used in Bitcoin Core 30.x and newer releases with [#32345](https://github.com/bitcoin/bitcoin/pull/32345), [#33241](https://github.com/bitcoin/bitcoin/pull/33241), and [#33322](https://github.com/bitcoin/bitcoin/pull/33322). ## [v6.0-pre1](https://github.com/bitcoin-core/libmultiprocess/commits/v6.0-pre1) - Adds fixes for unclean shutdowns and thread sanitizer issues. - Drops `EventLoop::addClient` and `EventLoop::removeClient` methods, requiring clients to use new `EventLoopRef` class instead. -- Minimum required version for Bitcoin Core 30.0 release, pulled in by [#31741](https://github.com/bitcoin/bitcoin/pull/31741), [#32641](https://github.com/bitcoin/bitcoin/pull/32641), and [#32345](https://github.com/bitcoin/bitcoin/pull/32345). +- Used in Bitcoin Core 30.x and newer releases with [#31741](https://github.com/bitcoin/bitcoin/pull/31741), [#32641](https://github.com/bitcoin/bitcoin/pull/32641), and [#32345](https://github.com/bitcoin/bitcoin/pull/32345). +- Minimum required version since Bitcoin Core 30.0 due to [#32345](https://github.com/bitcoin/bitcoin/pull/32345). ## [v5.0](https://github.com/bitcoin-core/libmultiprocess/commits/v5.0) - Adds build improvements and tidy/warning fixes. -- Used in Bitcoin Core 29 releases, pulled in by [#31945](https://github.com/bitcoin/bitcoin/pull/31945). +- Used in Bitcoin Core 29.x and newer releases with [#31945](https://github.com/bitcoin/bitcoin/pull/31945). ## [v5.0-pre1](https://github.com/bitcoin-core/libmultiprocess/commits/v5.0-pre1) - Adds many improvements to Bitcoin Core mining interface: splitting up type headers, fixing shutdown bugs, adding subtree build support. - Broke up `proxy-types.h` into `type-*.h` files requiring clients to explicitly include overloads needed for C++ ↔️ Cap'n Proto type conversions. - Now requires C++ 20 support. -- Minimum required version for Bitcoin Core 29 releases, pulled in by [#30509](https://github.com/bitcoin/bitcoin/pull/30509), [#30510](https://github.com/bitcoin/bitcoin/pull/30510), [#31105](https://github.com/bitcoin/bitcoin/pull/31105), [#31740](https://github.com/bitcoin/bitcoin/pull/31740). +- Used in Bitcoin Core 29.x and newer releases with [#30509](https://github.com/bitcoin/bitcoin/pull/30509), [#30510](https://github.com/bitcoin/bitcoin/pull/30510), [#31105](https://github.com/bitcoin/bitcoin/pull/31105), [#31740](https://github.com/bitcoin/bitcoin/pull/31740). +- Minimum required version since Bitcoin Core 29.0 due to [#31740](https://github.com/bitcoin/bitcoin/pull/31740). ## [v4.0](https://github.com/bitcoin-core/libmultiprocess/commits/v4.0) - Added better cmake support, installing cmake package files so clients do not need to use pkgconfig. -- Used in Bitcoin Core 28 releases, pulled in by [#30490](https://github.com/bitcoin/bitcoin/pull/30490) and [#30513](https://github.com/bitcoin/bitcoin/pull/30513). +- Used in Bitcoin Core 28.x and newer releases with [#30490](https://github.com/bitcoin/bitcoin/pull/30490) and [#30513](https://github.com/bitcoin/bitcoin/pull/30513). ## [v3.0](https://github.com/bitcoin-core/libmultiprocess/commits/v3.0) - Dropped compatibility with Cap'n Proto versions before 0.7. -- Used in Bitcoin Core 27 releases, pulled in by [#28735](https://github.com/bitcoin/bitcoin/pull/28735), [#28907](https://github.com/bitcoin/bitcoin/pull/28907), and [#29276](https://github.com/bitcoin/bitcoin/pull/29276). +- Used in Bitcoin Core 27.x and newer releases with [#28735](https://github.com/bitcoin/bitcoin/pull/28735), [#28907](https://github.com/bitcoin/bitcoin/pull/28907), and [#29276](https://github.com/bitcoin/bitcoin/pull/29276). ## [v2.0](https://github.com/bitcoin-core/libmultiprocess/commits/v2.0) - Changed `PassField` function signature. - Now requires C++17 support. -- Used in Bitcoin Core 25 and 26 releases, pulled in by [#26672](https://github.com/bitcoin/bitcoin/pull/26672). +- Used in Bitcoin Core 25.x and newer releases with [#26672](https://github.com/bitcoin/bitcoin/pull/26672). ## [v1.0](https://github.com/bitcoin-core/libmultiprocess/commits/v1.0) - Dropped hardcoded includes in generated files, now requiring `include` and `includeTypes` annotations. -- Used in Bitcoin Core 22, 23, and 24 releases, pulled in by [#19160](https://github.com/bitcoin/bitcoin/pull/19160). +- Used in Bitcoin Core 22.x and newer releases with [#19160](https://github.com/bitcoin/bitcoin/pull/19160). ## [v0.0](https://github.com/bitcoin-core/libmultiprocess/commits/v0.0) - Initial version used in a downstream release. -- Used in Bitcoin Core 21 releases, pulled in by [#16367](https://github.com/bitcoin/bitcoin/pull/16367), [#18588](https://github.com/bitcoin/bitcoin/pull/18588), and [#18677](https://github.com/bitcoin/bitcoin/pull/18677). +- Used in Bitcoin Core 21.x and newer releases with [#16367](https://github.com/bitcoin/bitcoin/pull/16367), [#18588](https://github.com/bitcoin/bitcoin/pull/18588), and [#18677](https://github.com/bitcoin/bitcoin/pull/18677). diff --git a/src/ipc/libmultiprocess/include/mp/proxy-io.h b/src/ipc/libmultiprocess/include/mp/proxy-io.h index 092ea42e..ec9a1539 100644 --- a/src/ipc/libmultiprocess/include/mp/proxy-io.h +++ b/src/ipc/libmultiprocess/include/mp/proxy-io.h @@ -13,7 +13,9 @@ #include #include +#include #include +#include #include #include #include @@ -25,6 +27,7 @@ namespace mp { struct ThreadContext; +struct Listener; struct InvokeContext { @@ -255,7 +258,7 @@ class EventLoop //! Run event loop. Does not return until shutdown. This should only be //! called once from the m_thread_id thread. This will block until - //! the m_num_clients reference count is 0. + //! the m_num_refs reference count is 0. void loop(); //! Run function on event loop thread. Does not return until function completes. @@ -314,9 +317,13 @@ class EventLoop //! Pipe write handle used to wake up the event loop thread. int m_post_fd = -1; - //! Number of clients holding references to ProxyServerBase objects that - //! reference this event loop. - int m_num_clients MP_GUARDED_BY(m_mutex) = 0; + //! Number of EventLoopRef instances referencing this event loop. This is a + //! sum of the number of client and server objects (Connection, ProxyClient, + //! ProxyServer) using the loop, plus temporary references held while + //! posting functions to the loop, plus any references held by external code + //! to keep the loop running. The loop() method exits when this count drops + //! to 0 (and m_async_fns is empty). + int m_num_refs MP_GUARDED_BY(m_mutex) = 0; //! Mutex and condition variable used to post tasks to event loop and async //! thread. @@ -356,6 +363,12 @@ class EventLoop //! Hook called on the worker thread just before returning results. std::function testing_hook_async_request_done; + + //! Hook called on the event loop thread when a client has connected. + std::function testing_hook_connected; + + //! Hook called on the event loop thread when a client has disconnected. + std::function testing_hook_disconnected; }; //! Single element task queue used to handle recursive capnp calls. (If the @@ -477,6 +490,13 @@ class Connection //! ThreadMap.makeThread) used to service requests to clients. ::capnp::CapabilityServerSet m_threads; + //! A thread created by makePool with associated pending work queue. Vector is filled once by makePool() and never resized. + struct PoolSlot { + Thread::Client client; + size_t depth{0}; + }; + std::vector m_thread_pool; + //! Canceler for canceling promises that we want to discard when the //! connection is destroyed. This is used to interrupt method calls that are //! still executing at time of disconnection. @@ -831,8 +851,8 @@ std::unique_ptr> ConnectStream(EventLoop& loop, int f //! handles requests from the stream by calling the init object. Embed the //! ProxyServer in a Connection object that is stored and erased if //! disconnected. This should be called from the event loop thread. -template -void _Serve(EventLoop& loop, kj::Own&& stream, InitImpl& init) +template +void _Serve(EventLoop& loop, kj::Own&& stream, InitImpl& init, OnDisconnect&& on_disconnect) { loop.m_incoming_connections.emplace_front(loop, kj::mv(stream), [&](Connection& connection) { // Disable deleter so proxy server object doesn't attempt to delete the @@ -842,23 +862,46 @@ void _Serve(EventLoop& loop, kj::Own&& stream, InitImpl& init }); auto it = loop.m_incoming_connections.begin(); MP_LOG(loop, Log::Info) << "IPC server: socket connected."; - it->onDisconnect([&loop, it] { + if (loop.testing_hook_connected) loop.testing_hook_connected(); + it->onDisconnect([&loop, it, on_disconnect = std::forward(on_disconnect)]() mutable { MP_LOG(loop, Log::Info) << "IPC server: socket disconnected."; loop.m_incoming_connections.erase(it); + on_disconnect(); + if (loop.testing_hook_disconnected) loop.testing_hook_disconnected(); }); } -//! Given connection receiver and an init object, handle incoming connections by -//! calling _Serve, to create ProxyServer objects and forward requests to the -//! init object. +struct Listener +{ + explicit Listener(kj::Own&& receiver, std::optional max_connections) + : m_receiver(kj::mv(receiver)), m_max_connections(max_connections) {} + + bool atCapacity() const + { + return m_max_connections && m_active_connections >= *m_max_connections; + } + + kj::Own m_receiver; + std::optional m_max_connections; + size_t m_active_connections{0}; +}; + template -void _Listen(EventLoop& loop, kj::Own&& listener, InitImpl& init) +void _Listen(const std::shared_ptr& listener, EventLoop& loop, InitImpl& init) { - auto* ptr = listener.get(); - loop.m_task_set->add(ptr->accept().then( - [&loop, &init, listener = kj::mv(listener)](kj::Own&& stream) mutable { - _Serve(loop, kj::mv(stream), init); - _Listen(loop, kj::mv(listener), init); + if (listener->atCapacity()) return; + + auto* receiver = listener->m_receiver.get(); + loop.m_task_set->add(receiver->accept().then( + [&loop, &init, listener](kj::Own&& stream) { + ++listener->m_active_connections; + _Serve(loop, kj::mv(stream), init, [&loop, &init, listener] { + const bool resume_accept{listener->atCapacity()}; + assert(listener->m_active_connections > 0); + --listener->m_active_connections; + if (resume_accept) _Listen(listener, loop, init); + }); + _Listen(listener, loop, init); })); } @@ -868,18 +911,22 @@ template void ServeStream(EventLoop& loop, int fd, InitImpl& init) { _Serve( - loop, loop.m_io_context.lowLevelProvider->wrapSocketFd(fd, kj::LowLevelAsyncIoProvider::TAKE_OWNERSHIP), init); + loop, + loop.m_io_context.lowLevelProvider->wrapSocketFd(fd, kj::LowLevelAsyncIoProvider::TAKE_OWNERSHIP), + init, + [] {}); } //! Given listening socket file descriptor and an init object, handle incoming //! connections and requests by calling methods on the Init object. template -void ListenConnections(EventLoop& loop, int fd, InitImpl& init) +void ListenConnections(EventLoop& loop, int fd, InitImpl& init, std::optional max_connections = std::nullopt) { loop.sync([&]() { - _Listen(loop, + auto listener{std::make_shared( loop.m_io_context.lowLevelProvider->wrapListenSocketFd(fd, kj::LowLevelAsyncIoProvider::TAKE_OWNERSHIP), - init); + max_connections)}; + _Listen(listener, loop, init); }); } diff --git a/src/ipc/libmultiprocess/include/mp/proxy-types.h b/src/ipc/libmultiprocess/include/mp/proxy-types.h index 1ccf423f..f1127da5 100644 --- a/src/ipc/libmultiprocess/include/mp/proxy-types.h +++ b/src/ipc/libmultiprocess/include/mp/proxy-types.h @@ -291,6 +291,21 @@ void BuildList(TypeList, InvokeContext& invoke_context, Output&& outp } } +template +decltype(auto) ReadList(TypeList, InvokeContext& invoke_context, Input&& input, ReadDest&& read_dest, InitFn&& init, EmplaceFn&& emplace) +{ + return read_dest.update([&](auto& value) { + auto data = input.get(); + init(value, data.size()); + for (auto item : data) { + ReadField(TypeList(), invoke_context, Make(item), + ReadDestEmplace(TypeList(), [&emplace, &value](auto&&... args) -> decltype(auto) { + return emplace(value, std::forward(args)...); + })); + } + }); +} + template void CustomBuildField(TypeList, Priority<0>, InvokeContext& invoke_context, Value&& value, Output&& output) { diff --git a/src/ipc/libmultiprocess/include/mp/proxy.capnp b/src/ipc/libmultiprocess/include/mp/proxy.capnp index 386f8f7a..e0a66fd9 100644 --- a/src/ipc/libmultiprocess/include/mp/proxy.capnp +++ b/src/ipc/libmultiprocess/include/mp/proxy.capnp @@ -45,6 +45,10 @@ interface ThreadMap $count(0) { # execute on. Clients create and name threads and pass the thread handle as # a call parameter. makeThread @0 (name :Text) -> (result :Thread); + # 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) -> (); } interface Thread { diff --git a/src/ipc/libmultiprocess/include/mp/proxy.h b/src/ipc/libmultiprocess/include/mp/proxy.h index 4458ee9e..57362f4e 100644 --- a/src/ipc/libmultiprocess/include/mp/proxy.h +++ b/src/ipc/libmultiprocess/include/mp/proxy.h @@ -44,7 +44,7 @@ inline void CleanupRun(CleanupList& fns) { } } -//! Event loop smart pointer automatically managing m_num_clients. +//! Event loop smart pointer automatically managing m_num_refs. //! If a lock pointer argument is passed, the specified lock will be used, //! otherwise EventLoop::m_mutex will be locked when needed. class EventLoopRef diff --git a/src/ipc/libmultiprocess/include/mp/type-context.h b/src/ipc/libmultiprocess/include/mp/type-context.h index 3ab3d4b0..7e47ca8a 100644 --- a/src/ipc/libmultiprocess/include/mp/type-context.h +++ b/src/ipc/libmultiprocess/include/mp/type-context.h @@ -200,63 +200,95 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn& // asynchronously with getLocalServer(). const auto& params = server_context.call_context.getParams(); Context::Reader context_arg = Accessor::get(params); - auto thread_client = context_arg.getThread(); - auto result = server.m_context.connection->m_threads.getLocalServer(thread_client) - .then([&loop, invoke = kj::mv(invoke), req](const kj::Maybe& perhaps) mutable { - // Assuming the thread object is found, pass it a pointer to the - // `invoke` lambda above which will invoke the function on that - // thread. - KJ_IF_MAYBE (thread_server, perhaps) { - auto& thread = static_cast&>(*thread_server); - MP_LOG(loop, Log::Debug) - << "IPC server post request #" << req << " {" << thread.m_thread_context.thread_name << "}"; - return thread.template post(std::move(invoke)); - } else { - MP_LOG(loop, Log::Error) - << "IPC server error request #" << req << ", missing thread to execute request"; - throw std::runtime_error("invalid thread handle"); - } - }, [&loop, req](::kj::Exception&& e) -> kj::Promise { - // If you see the error "(remote):0: failed: remote exception: - // Called null capability" here, it probably means your Init class - // is missing a declaration like: - // - // construct @0 (threadMap: Proxy.ThreadMap) -> (threadMap :Proxy.ThreadMap); - // - // which passes a ThreadMap reference from the client to the server, - // allowing the server to create threads to run IPC calls on the - // client, and also returns a ThreadMap reference from the server to - // the client, allowing the client to create threads on the server. - // (Typically the latter ThreadMap is used more often because there - // are more client-to-server calls.) - // - // If the other side of the connection did not previously get a - // ThreadMap reference from this side of the connection, when the - // other side calls `m_thread_map.makeThreadRequest()` in - // `BuildField` above, `m_thread_map` will be null, but that call - // will not fail immediately due to Cap'n Proto's request pipelining - // and delayed execution. Instead that call will return an invalid - // Thread reference, and when that reference is passed to this side - // of the connection as `thread_client` above, the - // `getLocalServer(thread_client)` call there will be the first - // thing to overtly fail, leading to an error here. - // - // Potentially there are also other things that could cause errors - // here, but this is the most likely cause. - // - // The log statement here is not strictly necessary since the same - // exception will also be logged in serverInvoke, but this logging - // may provide extra context that could be helpful for debugging. - MP_LOG(loop, Log::Info) - << "IPC server error request #" << req << " CapabilityServerSet::getLocalServer call failed, did you forget to provide a ThreadMap to the client prior to this IPC call?"; - return kj::mv(e); - }); - // Use connection m_canceler object to cancel the result promise if the - // connection is destroyed. (By default Cap'n Proto does not cancel requests - // on disconnect, since it's possible clients might want to make requests - // and immediately disconnect without waiting for results, but not want the - // requests to be canceled.) - return server.m_context.connection->m_canceler.wrap(kj::mv(result)); + if (!context_arg.hasThread()) { + // No client thread specified — dispatch through the server thread + // pool, picking the slot with the smallest in-flight depth. + auto* connection = server.m_context.connection; + auto& pool = connection->m_thread_pool; + if (pool.empty()) { + MP_LOG(loop, Log::Error) + << "IPC server error request #" << req << ", no thread specified and no pool configured"; + throw std::runtime_error("no thread specified and no pool configured"); + } + auto* slot = &pool[0]; + for (size_t i = 1; i < pool.size(); ++i) { + if (pool[i].depth < slot->depth) slot = &pool[i]; + } + ++slot->depth; + auto result = connection->m_threads.getLocalServer(slot->client) + .then([&loop, invoke = kj::mv(invoke), req](const kj::Maybe& perhaps) mutable -> kj::Promise { + KJ_IF_MAYBE (thread_server, perhaps) { + auto& thread = static_cast&>(*thread_server); + MP_LOG(loop, Log::Debug) + << "IPC server post request #" << req << " {" << thread.m_thread_context.thread_name << "}"; + return thread.template post(std::move(invoke)); + } else { + MP_LOG(loop, Log::Error) + << "IPC server error request #" << req << ", pool thread not found"; + throw std::runtime_error("pool thread not found"); + } + }) + .attach(kj::defer([slot] { --slot->depth; })); + return connection->m_canceler.wrap(kj::mv(result)); + } else { + auto thread_client = context_arg.getThread(); + auto result = server.m_context.connection->m_threads.getLocalServer(thread_client) + .then([&loop, invoke = kj::mv(invoke), req](const kj::Maybe& perhaps) mutable { + // Assuming the thread object is found, pass it a pointer to the + // `invoke` lambda above which will invoke the function on that + // thread. + KJ_IF_MAYBE (thread_server, perhaps) { + auto& thread = static_cast&>(*thread_server); + MP_LOG(loop, Log::Debug) + << "IPC server post request #" << req << " {" << thread.m_thread_context.thread_name << "}"; + return thread.template post(std::move(invoke)); + } else { + MP_LOG(loop, Log::Error) + << "IPC server error request #" << req << ", missing thread to execute request"; + throw std::runtime_error("invalid thread handle"); + } + }, [&loop, req](::kj::Exception&& e) -> kj::Promise { + // If you see the error "(remote):0: failed: remote exception: + // Called null capability" here, it probably means your Init class + // is missing a declaration like: + // + // construct @0 (threadMap: Proxy.ThreadMap) -> (threadMap :Proxy.ThreadMap); + // + // which passes a ThreadMap reference from the client to the server, + // allowing the server to create threads to run IPC calls on the + // client, and also returns a ThreadMap reference from the server to + // the client, allowing the client to create threads on the server. + // (Typically the latter ThreadMap is used more often because there + // are more client-to-server calls.) + // + // If the other side of the connection did not previously get a + // ThreadMap reference from this side of the connection, when the + // other side calls `m_thread_map.makeThreadRequest()` in + // `BuildField` above, `m_thread_map` will be null, but that call + // will not fail immediately due to Cap'n Proto's request pipelining + // and delayed execution. Instead that call will return an invalid + // Thread reference, and when that reference is passed to this side + // of the connection as `thread_client` above, the + // `getLocalServer(thread_client)` call there will be the first + // thing to overtly fail, leading to an error here. + // + // Potentially there are also other things that could cause errors + // here, but this is the most likely cause. + // + // The log statement here is not strictly necessary since the same + // exception will also be logged in serverInvoke, but this logging + // may provide extra context that could be helpful for debugging. + MP_LOG(loop, Log::Info) + << "IPC server error request #" << req << " CapabilityServerSet::getLocalServer call failed, did you forget to provide a ThreadMap to the client prior to this IPC call?"; + return kj::mv(e); + }); + // Use connection m_canceler object to cancel the result promise if the + // connection is destroyed. (By default Cap'n Proto does not cancel requests + // on disconnect, since it's possible clients might want to make requests + // and immediately disconnect without waiting for results, but not want the + // requests to be canceled.) + return server.m_context.connection->m_canceler.wrap(kj::mv(result)); + } } } // namespace mp diff --git a/src/ipc/libmultiprocess/include/mp/type-data.h b/src/ipc/libmultiprocess/include/mp/type-data.h index cdcdd30e..09c93df9 100644 --- a/src/ipc/libmultiprocess/include/mp/type-data.h +++ b/src/ipc/libmultiprocess/include/mp/type-data.h @@ -9,6 +9,7 @@ #include #include +#include namespace mp { template @@ -33,7 +34,7 @@ requires (std::is_same_v && IsBy { auto data = std::span{value}; auto result = output.init(data.size()); - memcpy(result.begin(), data.data(), data.size()); + std::ranges::copy(data, result.begin()); } template diff --git a/src/ipc/libmultiprocess/include/mp/type-map.h b/src/ipc/libmultiprocess/include/mp/type-map.h index 213ed811..e00a7813 100644 --- a/src/ipc/libmultiprocess/include/mp/type-map.h +++ b/src/ipc/libmultiprocess/include/mp/type-map.h @@ -56,18 +56,12 @@ decltype(auto) CustomReadField(TypeList>, Input&& input, ReadDest&& read_dest) { - return read_dest.update([&](auto& value) { - auto data = input.get(); - value.clear(); - for (auto item : data) { - ReadField(TypeList>(), invoke_context, - Make(item), - ReadDestEmplace( - TypeList>(), [&](auto&&... args) -> auto& { - return *EmplacePiecewiseSafe(value, std::forward(args)...).first; - })); - } - }); + return ReadList( + TypeList>(), invoke_context, input, read_dest, + [&](auto& value, size_t) { value.clear(); }, + [&](auto& value, auto&&... args) -> auto& { + return *EmplacePiecewiseSafe(value, std::forward(args)...).first; + }); } } // namespace mp diff --git a/src/ipc/libmultiprocess/include/mp/type-set.h b/src/ipc/libmultiprocess/include/mp/type-set.h index f051f738..902dd614 100644 --- a/src/ipc/libmultiprocess/include/mp/type-set.h +++ b/src/ipc/libmultiprocess/include/mp/type-set.h @@ -26,16 +26,12 @@ decltype(auto) CustomReadField(TypeList>, Input&& input, ReadDest&& read_dest) { - return read_dest.update([&](auto& value) { - auto data = input.get(); - value.clear(); - for (auto item : data) { - ReadField(TypeList(), invoke_context, Make(item), - ReadDestEmplace(TypeList(), [&](auto&&... args) -> auto& { - return *value.emplace(std::forward(args)...).first; - })); - } - }); + return ReadList( + TypeList(), invoke_context, input, read_dest, + [&](auto& value, size_t) { value.clear(); }, + [&](auto& value, auto&&... args) -> auto& { + return *value.emplace(std::forward(args)...).first; + }); } } // namespace mp diff --git a/src/ipc/libmultiprocess/include/mp/type-threadmap.h b/src/ipc/libmultiprocess/include/mp/type-threadmap.h index 3005d9de..c38c2ac4 100644 --- a/src/ipc/libmultiprocess/include/mp/type-threadmap.h +++ b/src/ipc/libmultiprocess/include/mp/type-threadmap.h @@ -14,6 +14,7 @@ struct ProxyServer final : public virtual ThreadMap::Server public: ProxyServer(Connection& connection); kj::Promise makeThread(MakeThreadContext context) override; + kj::Promise makePool(MakePoolContext context) override; Connection& m_connection; }; diff --git a/src/ipc/libmultiprocess/include/mp/type-unordered-set.h b/src/ipc/libmultiprocess/include/mp/type-unordered-set.h index ad4c0ab8..d1c714b2 100644 --- a/src/ipc/libmultiprocess/include/mp/type-unordered-set.h +++ b/src/ipc/libmultiprocess/include/mp/type-unordered-set.h @@ -27,16 +27,15 @@ decltype(auto) CustomReadField(TypeList>, Input&& input, ReadDest&& read_dest) { - return read_dest.update([&](auto& value) { - auto data = input.get(); - value.clear(); - for (auto item : data) { - ReadField(TypeList(), invoke_context, Make(item), - ReadDestEmplace(TypeList(), [&](auto&&... args) -> auto& { - return *value.emplace(std::forward(args)...).first; - })); - } - }); + return ReadList( + TypeList(), invoke_context, input, read_dest, + [&](auto& value, size_t size) { + value.clear(); + value.reserve(size); + }, + [&](auto& value, auto&&... args) -> auto& { + return *value.emplace(std::forward(args)...).first; + }); } } // namespace mp diff --git a/src/ipc/libmultiprocess/include/mp/type-vector.h b/src/ipc/libmultiprocess/include/mp/type-vector.h index 648fda5d..7b5197ff 100644 --- a/src/ipc/libmultiprocess/include/mp/type-vector.h +++ b/src/ipc/libmultiprocess/include/mp/type-vector.h @@ -31,35 +31,16 @@ decltype(auto) CustomReadField(TypeList>, Input&& input, ReadDest&& read_dest) { - return read_dest.update([&](auto& value) { - auto data = input.get(); - value.clear(); - value.reserve(data.size()); - for (auto item : data) { - ReadField(TypeList(), invoke_context, Make(item), - ReadDestEmplace(TypeList(), [&](auto&&... args) -> auto& { - value.emplace_back(std::forward(args)...); - return value.back(); - })); - } - }); -} - -template -decltype(auto) CustomReadField(TypeList>, - Priority<1>, - InvokeContext& invoke_context, - Input&& input, - ReadDest&& read_dest) -{ - return read_dest.update([&](auto& value) { - auto data = input.get(); - value.clear(); - value.reserve(data.size()); - for (auto item : data) { - value.push_back(ReadField(TypeList(), invoke_context, Make(item), ReadDestTemp())); - } - }); + return ReadList( + TypeList(), invoke_context, input, read_dest, + [&](auto& value, size_t size) { + value.clear(); + value.reserve(size); + }, + [&](auto& value, auto&&... args) -> decltype(auto) { + value.emplace_back(std::forward(args)...); + return value.back(); + }); } } // namespace mp diff --git a/src/ipc/libmultiprocess/include/mp/version.h b/src/ipc/libmultiprocess/include/mp/version.h index 423ed460..bbf54524 100644 --- a/src/ipc/libmultiprocess/include/mp/version.h +++ b/src/ipc/libmultiprocess/include/mp/version.h @@ -24,7 +24,7 @@ //! pointing at the prior merge commit. The /doc/versions.md file should also be //! updated, noting any significant or incompatible changes made since the //! previous version. -#define MP_MAJOR_VERSION 11 +#define MP_MAJOR_VERSION 13 //! Minor version number. Should be incremented in stable branches after //! backporting changes. The /doc/versions.md file should also be updated to diff --git a/src/ipc/libmultiprocess/src/mp/proxy.cpp b/src/ipc/libmultiprocess/src/mp/proxy.cpp index 963050c3..c1d2f3b0 100644 --- a/src/ipc/libmultiprocess/src/mp/proxy.cpp +++ b/src/ipc/libmultiprocess/src/mp/proxy.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -36,6 +37,7 @@ #include #include #include +#include namespace mp { @@ -51,7 +53,7 @@ EventLoopRef::EventLoopRef(EventLoop& loop, Lock* lock) : m_loop(&loop), m_lock( { auto loop_lock{PtrOrValue{m_lock, m_loop->m_mutex}}; loop_lock->assert_locked(m_loop->m_mutex); - m_loop->m_num_clients += 1; + m_loop->m_num_refs += 1; } // Due to the conditionals in this function, MP_NO_TSA is required to avoid @@ -63,8 +65,8 @@ void EventLoopRef::reset(bool relock) MP_NO_TSA m_loop = nullptr; auto loop_lock{PtrOrValue{m_lock, loop->m_mutex}}; loop_lock->assert_locked(loop->m_mutex); - assert(loop->m_num_clients > 0); - loop->m_num_clients -= 1; + assert(loop->m_num_refs > 0); + loop->m_num_refs -= 1; if (loop->done()) { loop->m_cv.notify_all(); int post_fd{loop->m_post_fd}; @@ -218,7 +220,7 @@ EventLoop::~EventLoop() KJ_ASSERT(!m_async_fns); KJ_ASSERT(m_wait_fd == -1); KJ_ASSERT(m_post_fd == -1); - KJ_ASSERT(m_num_clients == 0); + KJ_ASSERT(m_num_refs == 0); // Spin event loop. wait for any promises triggered by RPC shutdown. // auto cleanup = kj::evalLater([]{}); @@ -320,8 +322,8 @@ void EventLoop::startAsyncThread() bool EventLoop::done() const { - assert(m_num_clients >= 0); - return m_num_clients == 0 && m_async_fns->empty(); + assert(m_num_refs >= 0); + return m_num_refs == 0 && m_async_fns->empty(); } std::tuple SetThread(GuardedRef threads, Connection* connection, const std::function& make_thread) @@ -415,6 +417,29 @@ kj::Promise ProxyServer::getName(GetNameContext context) ProxyServer::ProxyServer(Connection& connection) : m_connection(connection) {} +kj::Promise ProxyServer::makePool(MakePoolContext context) +{ + if (!m_connection.m_thread_pool.empty()) { + throw std::runtime_error("makePool called on connection with existing pool"); + } + EventLoop& loop{*m_connection.m_loop}; + const uint32_t count = context.getParams().getCount(); + for (uint32_t i = 0; i < count; ++i) { + const std::string thread_name = "pool/" + std::to_string(i); + std::promise thread_context; + std::thread thread([&loop, &thread_context, thread_name]() { + g_thread_context.thread_name = ThreadName(loop.m_exe_name) + " (" + thread_name + ")"; + g_thread_context.waiter = std::make_unique(); + Lock lock(g_thread_context.waiter->m_mutex); + thread_context.set_value(&g_thread_context); + g_thread_context.waiter->wait(lock, [] { return !g_thread_context.waiter; }); + }); + auto thread_server = kj::heap>(m_connection, *thread_context.get_future().get(), std::move(thread)); + m_connection.m_thread_pool.push_back({m_connection.m_threads.add(kj::mv(thread_server))}); + } + return kj::READY_NOW; +} + kj::Promise ProxyServer::makeThread(MakeThreadContext context) { EventLoop& loop{*m_connection.m_loop}; diff --git a/src/ipc/libmultiprocess/test/CMakeLists.txt b/src/ipc/libmultiprocess/test/CMakeLists.txt index 1f21ba44..13246293 100644 --- a/src/ipc/libmultiprocess/test/CMakeLists.txt +++ b/src/ipc/libmultiprocess/test/CMakeLists.txt @@ -26,6 +26,7 @@ if(BUILD_TESTING AND TARGET CapnProto::kj-test) ${MP_PROXY_HDRS} mp/test/foo-types.h mp/test/foo.h + mp/test/listen_tests.cpp mp/test/spawn_tests.cpp mp/test/test.cpp ) diff --git a/src/ipc/libmultiprocess/test/mp/test/foo.capnp b/src/ipc/libmultiprocess/test/mp/test/foo.capnp index 31736452..9e6213fd 100644 --- a/src/ipc/libmultiprocess/test/mp/test/foo.capnp +++ b/src/ipc/libmultiprocess/test/mp/test/foo.capnp @@ -27,6 +27,7 @@ interface FooInterface $Proxy.wrap("mp::test::FooImplementation") { callbackExtended @10 (context :Proxy.Context, callback :ExtendedCallback, arg: Int32) -> (result :Int32); passCustom @11 (arg :FooCustom) -> (result :FooCustom); passEmpty @12 (arg :FooEmpty) -> (result :FooEmpty); + passData @24 (arg :Data) -> (result :Data); passMessage @13 (arg :FooMessage) -> (result :FooMessage); passMutable @14 (arg :FooMutable) -> (arg :FooMutable); passEnum @15 (arg :Int32) -> (result :Int32); @@ -55,10 +56,11 @@ interface FooFn $Proxy.wrap("ProxyCallback>") { struct FooStruct $Proxy.wrap("mp::test::FooStruct") { name @0 :Text; setInt @1 :List(Int32) $Proxy.name("set_int"); - vBool @2 :List(Bool) $Proxy.name("v_bool"); + vectorBool @2 :List(Bool) $Proxy.name("vector_bool"); optionalInt @3 :Int32 $Proxy.name("optional_int"); hasOptionalInt @4 :Bool; unorderedSetInt @5 :List(Int32) $Proxy.name("unordered_set_int"); + mapStringInt @6 :List(StringIntPair) $Proxy.name("map_string_int"); } struct FooCustom $Proxy.wrap("mp::test::FooCustom") { @@ -81,3 +83,8 @@ struct Pair(T1, T2) { first @0 :T1; second @1 :T2; } + +struct StringIntPair { + first @0 :Text; + second @1 :Int32; +} diff --git a/src/ipc/libmultiprocess/test/mp/test/foo.h b/src/ipc/libmultiprocess/test/mp/test/foo.h index 5b66b85e..779c8db1 100644 --- a/src/ipc/libmultiprocess/test/mp/test/foo.h +++ b/src/ipc/libmultiprocess/test/mp/test/foo.h @@ -22,9 +22,10 @@ struct FooStruct { std::string name; std::set set_int; - std::vector v_bool; + std::vector vector_bool; std::optional optional_int; std::unordered_set unordered_set_int; + std::map map_string_int; }; enum class FooEnum : uint8_t { ONE = 1, TWO = 2, }; @@ -83,6 +84,7 @@ class FooImplementation int callbackExtended(ExtendedCallback& callback, int arg) { return callback.callExtended(arg); } FooCustom passCustom(FooCustom foo) { return foo; } FooEmpty passEmpty(FooEmpty foo) { return foo; } + FooData passData(FooData foo) { return foo; } FooMessage passMessage(FooMessage foo) { foo.message += " call"; return foo; } void passMutable(FooMutable& foo) { foo.message += " call"; } FooEnum passEnum(FooEnum foo) { return foo; } diff --git a/src/ipc/libmultiprocess/test/mp/test/listen_tests.cpp b/src/ipc/libmultiprocess/test/mp/test/listen_tests.cpp new file mode 100644 index 00000000..4e579d99 --- /dev/null +++ b/src/ipc/libmultiprocess/test/mp/test/listen_tests.cpp @@ -0,0 +1,294 @@ +// Copyright (c) The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include // IWYU pragma: keep +#include +#include +#include +#include +#include +#include +#include + +namespace mp { +namespace test { +namespace { + +constexpr auto FAILURE_TIMEOUT = std::chrono::seconds{30}; + +//! Owns a temporary Unix-domain listening socket used by ListenSetup. Tests call +//! Connect() to create client socket FDs and release() to transfer the listening +//! FD to ListenConnections(). +class UnixListener +{ +public: + UnixListener() + { + std::string dir_template = (std::filesystem::temp_directory_path() / "mptest-listener-XXXXXX").string(); + char* dir = mkdtemp(dir_template.data()); + KJ_REQUIRE(dir != nullptr); + m_dir = dir; + m_path = m_dir + "/socket"; + + m_fd = socket(AF_UNIX, SOCK_STREAM, 0); + KJ_REQUIRE(m_fd >= 0); + + sockaddr_un addr{}; + addr.sun_family = AF_UNIX; + KJ_REQUIRE(m_path.size() < sizeof(addr.sun_path)); + std::strncpy(addr.sun_path, m_path.c_str(), sizeof(addr.sun_path) - 1); + KJ_REQUIRE(bind(m_fd, reinterpret_cast(&addr), sizeof(addr)) == 0); + KJ_REQUIRE(listen(m_fd, SOMAXCONN) == 0); + } + + ~UnixListener() + { + if (m_fd >= 0) close(m_fd); + if (!m_path.empty()) unlink(m_path.c_str()); + if (!m_dir.empty()) rmdir(m_dir.c_str()); + } + + int release() + { + assert(m_fd >= 0); + int fd = m_fd; + m_fd = -1; + return fd; + } + + int MakeConnectedSocket() const + { + int fd = socket(AF_UNIX, SOCK_STREAM, 0); + KJ_REQUIRE(fd >= 0); + + sockaddr_un addr{}; + addr.sun_family = AF_UNIX; + KJ_REQUIRE(m_path.size() < sizeof(addr.sun_path)); + std::strncpy(addr.sun_path, m_path.c_str(), sizeof(addr.sun_path) - 1); + KJ_REQUIRE(connect(fd, reinterpret_cast(&addr), sizeof(addr)) == 0); + return fd; + } + +private: + int m_fd{-1}; + std::string m_dir; + std::string m_path; +}; + +//! Runs a client EventLoop on its own thread and connects one socket FD to the +//! server. The constructed ProxyClient can be used by the test thread to make +//! calls over that connection. +class ClientSetup +{ +public: + explicit ClientSetup(int fd) + : thread([this, fd] { + EventLoop loop("mptest-client", [](mp::LogMessage log) { + KJ_LOG(INFO, log.level, log.message); + if (log.level == mp::Log::Raise) throw std::runtime_error(log.message); + }); + client_promise.set_value(ConnectStream(loop, fd)); + loop.loop(); + }) + { + client = client_promise.get_future().get(); + } + + ~ClientSetup() + { + client.reset(); + thread.join(); + } + + std::promise>> client_promise; + std::unique_ptr> client; + + //! Thread variable should be after other struct members so the thread does + //! not start until the other members are initialized. + std::thread thread; +}; + +//! Runs a server EventLoop on its own thread, starts ListenConnections() on a +//! UnixListener socket, and records connection/disconnection counts through +//! EventLoop test hooks +class ListenSetup +{ +public: + explicit ListenSetup(std::optional max_connections = std::nullopt) + : thread([this, max_connections] { + EventLoop loop("mptest-server", [](mp::LogMessage log) { + KJ_LOG(INFO, log.level, log.message); + if (log.level == mp::Log::Raise) throw std::runtime_error(log.message); + }); + loop.testing_hook_disconnected = [&] { + Lock lock(counter_mutex); + ++disconnected_count; + counter_cv.notify_all(); + }; + loop.testing_hook_connected = [&] { + Lock lock(counter_mutex); + ++connected_count; + counter_cv.notify_all(); + }; + m_loop_ref.emplace(loop); + FooImplementation foo; + ListenConnections(loop, listener.release(), foo, max_connections); + ready_promise.set_value(); + loop.loop(); + }) + { + ready_promise.get_future().get(); + } + + ~ListenSetup() + { + m_loop_ref.reset(); + thread.join(); + } + + size_t ConnectedCount() + { + Lock lock(counter_mutex); + return connected_count; + } + + size_t DisconnectedCount() + { + Lock lock(counter_mutex); + return disconnected_count; + } + + void WaitForConnectedCount(size_t expected_count) + { + Lock lock(counter_mutex); + const auto deadline = std::chrono::steady_clock::now() + FAILURE_TIMEOUT; + const bool matched = counter_cv.wait_until(lock.m_lock, deadline, [&]() MP_REQUIRES(counter_mutex) { + return connected_count >= expected_count; + }); + KJ_REQUIRE(matched); + } + + void WaitForDisconnectedCount(size_t expected_count) + { + Lock lock(counter_mutex); + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(5); + const bool matched = counter_cv.wait_until(lock.m_lock, deadline, [&]() MP_REQUIRES(counter_mutex) { + return disconnected_count >= expected_count; + }); + KJ_REQUIRE(matched); + } + + UnixListener listener; + std::promise ready_promise; + std::optional m_loop_ref; + Mutex counter_mutex; + std::condition_variable counter_cv; + size_t connected_count MP_GUARDED_BY(counter_mutex) {0}; + size_t disconnected_count MP_GUARDED_BY(counter_mutex) {0}; + //! Thread variable should be after other struct members so the thread does + //! not start until the other members are initialized. + std::thread thread; +}; + +KJ_TEST("ListenConnections accepts incoming connections") +{ + ListenSetup server; + KJ_EXPECT(server.ConnectedCount() == 0) + auto client = std::make_unique(server.listener.MakeConnectedSocket()); + + server.WaitForConnectedCount(1); + KJ_EXPECT(client->client->add(1, 2) == 3); +} + +KJ_TEST("ListenConnections enforces a local connection limit") +{ + // With max-connections=1, the second socket can connect to the kernel + // backlog, but ListenConnections should not accept or serve it until the + // first accepts clients disconnects. + + ListenSetup server(/*max_connections=*/1); + + KJ_EXPECT(server.ConnectedCount() == 0) + auto client1 = std::make_unique(server.listener.MakeConnectedSocket()); + server.WaitForConnectedCount(1); + + KJ_EXPECT(client1->client->add(1, 2) == 3); + + auto client2 = std::make_unique(server.listener.MakeConnectedSocket()); + // Without this sync, ConnectedCount() == 1 might pass even if + // max_connections was not enforced because the event loop has not accepted + // client2 yet. + (**server.m_loop_ref).sync([] {}); + + KJ_EXPECT(server.ConnectedCount() == 1); + KJ_EXPECT(server.DisconnectedCount() == 0); + client1.reset(); + server.WaitForDisconnectedCount(1); + server.WaitForConnectedCount(2); + + KJ_EXPECT(client2->client->add(2, 3) == 5); + + KJ_EXPECT(server.DisconnectedCount() == 1); + client2.reset(); + server.WaitForDisconnectedCount(2); + + KJ_EXPECT(server.DisconnectedCount() == 2); + auto client3 = std::make_unique(server.listener.MakeConnectedSocket()); + server.WaitForConnectedCount(3); + KJ_EXPECT(client3->client->add(3, 4) == 7); +} + +KJ_TEST("ListenConnections accepts multiple connections") +{ + // With max-connections=2, two clients should be accepted and usable at the + // same time, while a third waits until one active client disconnects. + + ListenSetup server(/*max_connections=*/2); + + KJ_EXPECT(server.ConnectedCount() == 0); + auto client1 = std::make_unique(server.listener.MakeConnectedSocket()); + auto client2 = std::make_unique(server.listener.MakeConnectedSocket()); + server.WaitForConnectedCount(2); + + KJ_EXPECT(client1->client->add(1, 2) == 3); + KJ_EXPECT(client2->client->add(2, 3) == 5); + + auto client3 = std::make_unique(server.listener.MakeConnectedSocket()); + // Without this sync, ConnectedCount() == 2 might pass even if + // max_connections was not enforced because the event loop has not accepted + // client3 yet. + (**server.m_loop_ref).sync([] {}); + + KJ_EXPECT(server.ConnectedCount() == 2); + KJ_EXPECT(server.DisconnectedCount() == 0); + client1.reset(); + server.WaitForDisconnectedCount(1); + server.WaitForConnectedCount(3); + + KJ_EXPECT(client3->client->add(3, 4) == 7); +} + +} // namespace +} // namespace test +} // namespace mp diff --git a/src/ipc/libmultiprocess/test/mp/test/test.cpp b/src/ipc/libmultiprocess/test/mp/test/test.cpp index eb4b5ec7..7a0940c8 100644 --- a/src/ipc/libmultiprocess/test/mp/test/test.cpp +++ b/src/ipc/libmultiprocess/test/mp/test/test.cpp @@ -18,9 +18,12 @@ #include #include #include +#include #include #include +#include #include +#include #include #include #include @@ -38,6 +41,15 @@ #include #include +//! Assert that a call throws std::runtime_error with the given message. +#define EXPECT_EXCEPTION(call, message) \ + try { \ + call; \ + KJ_EXPECT(false); \ + } catch (const std::runtime_error& e) { \ + KJ_EXPECT(std::string_view{e.what()} == message); \ + } + namespace mp { namespace test { @@ -146,10 +158,12 @@ KJ_TEST("Call FooInterface methods") in.set_int.insert(1); in.unordered_set_int.insert(2); in.unordered_set_int.insert(1); - in.v_bool.push_back(false); - in.v_bool.push_back(true); - in.v_bool.push_back(false); + in.vector_bool.push_back(false); + in.vector_bool.push_back(true); + in.vector_bool.push_back(false); in.optional_int = 3; + in.map_string_int.emplace("a", 1); + in.map_string_int.emplace("b", 2); FooStruct out = foo->pass(in); KJ_EXPECT(in.name == out.name); KJ_EXPECT(in.set_int.size() == out.set_int.size()); @@ -160,11 +174,16 @@ KJ_TEST("Call FooInterface methods") for (const auto& elem : in.unordered_set_int) { KJ_EXPECT(out.unordered_set_int.count(elem) == 1); } - KJ_EXPECT(in.v_bool.size() == out.v_bool.size()); - for (size_t i = 0; i < in.v_bool.size(); ++i) { - KJ_EXPECT(in.v_bool[i] == out.v_bool[i]); + KJ_EXPECT(in.vector_bool.size() == out.vector_bool.size()); + for (size_t i = 0; i < in.vector_bool.size(); ++i) { + KJ_EXPECT(in.vector_bool[i] == out.vector_bool[i]); } KJ_EXPECT(in.optional_int == out.optional_int); + KJ_EXPECT(in.map_string_int.size() == out.map_string_int.size()); + for (auto init{in.map_string_int.begin()}, outit{out.map_string_int.begin()}; init != in.map_string_int.end() && outit != out.map_string_int.end(); ++init, ++outit) { + KJ_EXPECT(init->first == outit->first); + KJ_EXPECT(init->second == outit->second); + } // Additional checks for std::optional member KJ_EXPECT(foo->pass(in).optional_int == 3); @@ -220,6 +239,9 @@ KJ_TEST("Call FooInterface methods") foo->passEmpty(FooEmpty{}); + FooData empty_data_out = foo->passData(FooData{}); + KJ_EXPECT(empty_data_out.empty()); + FooMessage message1; message1.message = "init"; FooMessage message2{foo->passMessage(message1)}; @@ -234,6 +256,11 @@ KJ_TEST("Call FooInterface methods") KJ_EXPECT(foo->passFn([]{ return 10; }) == 10); + // Recursive async IPC calls + KJ_EXPECT(foo->passFn([foo]{ + return foo->passFn([]{ return 1; }); + }) == 1); + std::vector data_in; data_in.push_back(std::make_shared(FooData{'H', 'i'})); data_in.push_back(nullptr); @@ -251,14 +278,7 @@ KJ_TEST("Call IPC method after client connection is closed") KJ_EXPECT(foo->add(1, 2) == 3); setup.client_disconnect(); - bool disconnected{false}; - try { - foo->add(1, 2); - } catch (const std::runtime_error& e) { - KJ_EXPECT(std::string_view{e.what()} == "IPC client method called after disconnect."); - disconnected = true; - } - KJ_EXPECT(disconnected); + EXPECT_EXCEPTION(foo->add(1, 2), "IPC client method called after disconnect."); } KJ_TEST("Calling IPC method after server connection is closed") @@ -268,14 +288,7 @@ KJ_TEST("Calling IPC method after server connection is closed") KJ_EXPECT(foo->add(1, 2) == 3); setup.server_disconnect(); - bool disconnected{false}; - try { - foo->add(1, 2); - } catch (const std::runtime_error& e) { - KJ_EXPECT(std::string_view{e.what()} == "IPC client method call interrupted by disconnect."); - disconnected = true; - } - KJ_EXPECT(disconnected); + EXPECT_EXCEPTION(foo->add(1, 2), "IPC client method call interrupted by disconnect."); } KJ_TEST("Calling IPC method and disconnecting during the call") @@ -288,14 +301,7 @@ KJ_TEST("Calling IPC method and disconnecting during the call") // handling the callFn call to make sure this case is handled cleanly. setup.server->m_impl->m_fn = setup.client_disconnect; - bool disconnected{false}; - try { - foo->callFn(); - } catch (const std::runtime_error& e) { - KJ_EXPECT(std::string_view{e.what()} == "IPC client method call interrupted by disconnect."); - disconnected = true; - } - KJ_EXPECT(disconnected); + EXPECT_EXCEPTION(foo->callFn(), "IPC client method call interrupted by disconnect."); } KJ_TEST("Calling IPC method, disconnecting and blocking during the call") @@ -330,14 +336,7 @@ KJ_TEST("Calling IPC method, disconnecting and blocking during the call") signal.get_future().get(); }; - bool disconnected{false}; - try { - foo->callFnAsync(); - } catch (const std::runtime_error& e) { - KJ_EXPECT(std::string_view{e.what()} == "IPC client method call interrupted by disconnect."); - disconnected = true; - } - KJ_EXPECT(disconnected); + EXPECT_EXCEPTION(foo->callFnAsync(), "IPC client method call interrupted by disconnect."); // Now that the disconnect has been detected, set signal allowing the // callFnAsync() IPC call to return. Since signalling may not wake up the @@ -374,14 +373,7 @@ KJ_TEST("Worker thread destroyed before it is initialized") std::this_thread::sleep_for(std::chrono::milliseconds(10)); }; - bool disconnected{false}; - try { - foo->callFnAsync(); - } catch (const std::runtime_error& e) { - KJ_EXPECT(std::string_view{e.what()} == "IPC client method call interrupted by disconnect."); - disconnected = true; - } - KJ_EXPECT(disconnected); + EXPECT_EXCEPTION(foo->callFnAsync(), "IPC client method call interrupted by disconnect."); } KJ_TEST("Calling async IPC method, with server disconnect racing the call") @@ -406,12 +398,7 @@ KJ_TEST("Calling async IPC method, with server disconnect racing the call") std::this_thread::sleep_for(std::chrono::milliseconds(10)); }; - try { - foo->callFnAsync(); - KJ_EXPECT(false); - } catch (const std::runtime_error& e) { - KJ_EXPECT(std::string_view{e.what()} == "IPC client method call interrupted by disconnect."); - } + EXPECT_EXCEPTION(foo->callFnAsync(), "IPC client method call interrupted by disconnect."); } KJ_TEST("Calling async IPC method, with server disconnect after cleanup") @@ -435,12 +422,7 @@ KJ_TEST("Calling async IPC method, with server disconnect after cleanup") setup.server_disconnect(); }; - try { - foo->callFnAsync(); - KJ_EXPECT(false); - } catch (const std::runtime_error& e) { - KJ_EXPECT(std::string_view{e.what()} == "IPC client method call interrupted by disconnect."); - } + EXPECT_EXCEPTION(foo->callFnAsync(), "IPC client method call interrupted by disconnect."); } KJ_TEST("Destroying ProxyClient<> with destroy method after peer disconnect") @@ -524,5 +506,76 @@ KJ_TEST("Make simultaneous IPC calls on single remote thread") KJ_EXPECT(expected == 400); } +KJ_TEST("Call async IPC method dispatched to pool thread") +{ + TestSetup setup; + ProxyClient* foo = setup.client.get(); + + // Set up the thread map exchange so the client has the server's ThreadMap, + // then call makePool to pre-allocate two server threads. + foo->initThreadMap(); + setup.server->m_impl->m_int_fn = [](int n) { return n * 2; }; + + ThreadContext& tc{g_thread_context}; + std::atomic running{3}; + std::promise pool_ready; + foo->m_context.loop->sync([&] { + auto pool_req = foo->m_context.connection->m_thread_map.makePoolRequest(); + pool_req.setCount(2); + foo->m_context.loop->m_task_set->add( + pool_req.send().then([&](auto&&) { pool_ready.set_value(); })); + }); + pool_ready.get_future().get(); + + // Send three callIntFnAsync requests with no context.thread set. + // The server should dispatch each to a pool thread. + auto client{foo->m_client}; + foo->m_context.loop->sync([&] { + for (size_t i = 0; i < running; ++i) { + auto request{client.callIntFnAsyncRequest()}; + request.initContext(); // context present but thread unset + request.setArg(static_cast(i + 1)); + foo->m_context.loop->m_task_set->add(request.send().then( + [&running, &tc, i](auto&& results) { + assert(results.getResult() == static_cast((i + 1) * 2)); + running -= 1; + Lock lock(tc.waiter->m_mutex); + tc.waiter->m_cv.notify_all(); + })); + } + }); + { + Lock lock(tc.waiter->m_mutex); + tc.waiter->wait(lock, [&running] { return running == 0; }); + } +} + +KJ_TEST("Call async IPC method without thread or pool errors correctly") +{ + TestSetup setup; + ProxyClient* foo = setup.client.get(); + setup.server->m_impl->m_fn = [] {}; + + // Send a callFnAsync request with no context.thread and no pool configured. + // The server should throw the "no thread specified and no pool configured" error. + std::promise done; + bool error_thrown{false}; + foo->m_context.loop->sync([&] { + 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( + "no thread specified and no pool configured") != std::string_view::npos); + done.set_value(); + })); + }); + done.get_future().get(); + KJ_EXPECT(error_thrown); +} + } // namespace test } // namespace mp diff --git a/src/sv2/connman.cpp b/src/sv2/connman.cpp index 4016ba89..a8f03c75 100644 --- a/src/sv2/connman.cpp +++ b/src/sv2/connman.cpp @@ -19,6 +19,7 @@ Sv2Connman::~Sv2Connman() for (const auto& client : m_sv2_clients) { LogTrace(BCLog::SV2, "Disconnecting client id=%zu\n", client.first); + LOCK(client.second->cs_status); client.second->m_disconnect_flag = true; } DisconnectFlagged(); diff --git a/src/sv2/template_provider.cpp b/src/sv2/template_provider.cpp index 416a51cc..52b4b036 100644 --- a/src/sv2/template_provider.cpp +++ b/src/sv2/template_provider.cpp @@ -285,7 +285,11 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id) LogPrintLevel(BCLog::SV2, BCLog::Level::Trace, "%s block template for client id=%zu\n", constraints_generation_at_build == 0 ? "Generate initial" : "Regenerate", client_id); // Create block template and store interface reference - uint64_t template_id{WITH_LOCK(m_tp_mutex, return ++m_template_id;)}; + uint64_t template_id; + { + LOCK(m_tp_mutex); + template_id = ++m_template_id; + } node::BlockCreateOptions block_create_options{.use_mempool = true}; if (!prepare_block_create_options(block_create_options)) break;