From 4f2c203bffe7f895301462a18926b61b5aa43eb6 Mon Sep 17 00:00:00 2001 From: xyzconstant <263061129+xyzconstant@users.noreply.github.com> Date: Sun, 23 Aug 2026 03:56:59 -0300 Subject: [PATCH 1/6] Allow wrapped C++ methods to take parameters that are not sent over RPC Add a `$Proxy.extraParam` method annotation that declares an extra C++-only parameter in the generated method signature. The parameter has no corresponding capnp parameter and is not serialized or sent over RPC. The annotation value names the parameter in generated C++ code. Client behavior: - A matching `CustomBuildExtraParam(TypeList, ClientInvokeContext&, T&&)` overload MUST be implemented. The parameter is passed to it before the RPC message is dispatched. Server behavior: - A matching `CustomReadExtraParam(TypeList, ServerContext&)` overload MUST be implemented. No data arrives for this parameter so this overload reconstructs the parameter value on the server side. The test checks the values the client passes reach the client overload and the ones the server reconstructs arrive instead. --- include/mp/proxy-types.h | 75 ++++++++++++++++++++++++++++++++++------ include/mp/proxy.capnp | 6 ++++ src/mp/gen.cpp | 50 +++++++++++++++++++++++++-- test/mp/test/foo-types.h | 12 +++++++ test/mp/test/foo.capnp | 1 + test/mp/test/foo.h | 1 + test/mp/test/test.cpp | 9 +++++ 7 files changed, 140 insertions(+), 14 deletions(-) diff --git a/include/mp/proxy-types.h b/include/mp/proxy-types.h index 1e1e966c..992fbf94 100644 --- a/include/mp/proxy-types.h +++ b/include/mp/proxy-types.h @@ -540,12 +540,61 @@ ClientParam MakeClientParam(Types&&... values) return {std::forward(values)...}; } +//! Client parameter with no capnp field, generated for method parameters +//! declared with `$Proxy.extraParam`. Its value is handed to a +//! CustomBuildExtraParam overload instead of being built into the request. +template +struct ClientParam +{ + ClientParam(Types&&... values) : m_values{std::forward(values)...} {} + + struct BuildParams : IterateFieldsHelper + { + template + void handleField(ClientInvokeContext& invoke_context, Params&, ParamList) + { + static_assert((requires(ClientInvokeContext& context, Types&& value) { + CustomBuildExtraParam(TypeList>(), context, std::forward(value)); + } && ...), + "Wrapped C++ method has more parameters than its corresponding Cap'n Proto method has fields. " + "Declare extra parameters with $Proxy.extraParam in the Cap'n Proto schema and add a matching " + "`CustomBuildExtraParam` overload."); + auto const fun = [&](Types&&... values) { + (CustomBuildExtraParam(TypeList>(), invoke_context, std::forward(values)), ...); + }; + std::apply(fun, std::move(m_client_param->m_values)); + } + BuildParams(ClientParam* client_param) : m_client_param(client_param) {} + ClientParam* m_client_param; + }; + + struct ReadResults : IterateFieldsHelper + { + template + void handleField(ClientInvokeContext&, Results&, ParamList) + { + } + ReadResults(ClientParam*) {} + }; + + std::tuple m_values; +}; + struct ServerCall { // FIXME: maybe call call_context.releaseParams() - template - decltype(auto) invoke(ServerContext& server_context, TypeList<>, Args&&... args) const + template + decltype(auto) invoke(ServerContext& server_context, TypeList, Args&&... args) const { + // Construct the extra parameters before cancel_lock is released below. + // CustomReadExtraParam overloads build values from the request being + // executed, so they need the same protection as normal capnp fields + // from the event loop deleting request state on cancellation. + static_assert((requires { CustomReadExtraParam(TypeList>(), server_context); } && ...), + "Wrapped C++ method has more parameters than its corresponding Cap'n Proto method has fields. " + "Declare extra parameters with $Proxy.extraParam in the Cap'n Proto schema and add a matching " + "`CustomReadExtraParam` overload."); + std::tuple...> extra{CustomReadExtraParam(TypeList>(), server_context)...}; // If cancel_lock is set, release it while executing the method, and // reacquire it afterwards. The lock is needed to prevent params and // response structs from being deleted by the event loop thread if the @@ -556,9 +605,13 @@ struct ServerCall if (server_context.cancel_lock) server_context.cancel_lock->m_lock.unlock(); return TryFinally( [&]() -> decltype(auto) { - return ProxyServerMethodTraits< - typename decltype(server_context.call_context.getParams())::Reads - >::invoke(server_context, std::forward(args)...); + return std::apply( + [&](RemoveCvRef&... extra_args) -> decltype(auto) { + return ProxyServerMethodTraits< + typename decltype(server_context.call_context.getParams())::Reads + >::invoke(server_context, std::forward(args)..., std::move(extra_args)...); + }, + extra); }, [&] { if (server_context.cancel_lock) server_context.cancel_lock->m_lock.lock(); @@ -593,10 +646,10 @@ struct ServerRet : Parent { ServerRet(Parent parent) : Parent(parent) {} - template - void invoke(ServerContext& server_context, TypeList<>, Args&&... args) const + template + void invoke(ServerContext& server_context, ArgTypes arg_types, Args&&... args) const { - auto&& result = Parent::invoke(server_context, TypeList<>(), std::forward(args)...); + auto&& result = Parent::invoke(server_context, arg_types, std::forward(args)...); auto&& results = server_context.call_context.getResults(); InvokeContext& invoke_context = server_context; BuildField(TypeList(), invoke_context, Make(results), @@ -609,11 +662,11 @@ struct ServerExcept : Parent { ServerExcept(Parent parent) : Parent(parent) {} - template - void invoke(ServerContext& server_context, TypeList<>, Args&&... args) const + template + void invoke(ServerContext& server_context, ArgTypes arg_types, Args&&... args) const { try { - return Parent::invoke(server_context, TypeList<>(), std::forward(args)...); + return Parent::invoke(server_context, arg_types, std::forward(args)...); } catch (const Exception& exception) { auto&& results = server_context.call_context.getResults(); BuildField(TypeList(), server_context, Make(results), exception); diff --git a/include/mp/proxy.capnp b/include/mp/proxy.capnp index e0a66fd9..9c7e1090 100644 --- a/include/mp/proxy.capnp +++ b/include/mp/proxy.capnp @@ -40,6 +40,12 @@ annotation name(field, method): Text; annotation skip(field): Void; # Synonym for count(0). +annotation extraParam(method): Text; +# Adds an extra C++-only parameter to the generated method signature. +# +# This parameter has no corresponding capnp parameter and is not serialized or +# sent over RPC. The annotation value names the parameter in generated C++ code. + interface ThreadMap $count(0) { # Interface letting clients control which thread a method call should # execute on. Clients create and name threads and pass the thread handle as diff --git a/src/mp/gen.cpp b/src/mp/gen.cpp index d99b97b7..0eb45ded 100644 --- a/src/mp/gen.cpp +++ b/src/mp/gen.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -42,6 +43,7 @@ constexpr uint64_t COUNT_ANNOTATION_ID = 0xd02682b319f69b38ull; // From prox constexpr uint64_t EXCEPTION_ANNOTATION_ID = 0x996a183200992f88ull; // From proxy.capnp constexpr uint64_t NAME_ANNOTATION_ID = 0xb594888f63f4dbb9ull; // From proxy.capnp constexpr uint64_t SKIP_ANNOTATION_ID = 0x824c08b82695d8ddull; // From proxy.capnp +constexpr uint64_t EXTRA_PARAM_ANNOTATION_ID = 0xf1731507694af05aull; // From proxy.capnp template static bool AnnotationExists(const Reader& reader, uint64_t id) @@ -133,9 +135,11 @@ struct Field int args = 0; bool retval = false; bool optional = false; + bool extra = false; bool requested = false; bool skip = false; kj::StringPtr exception; + kj::String extra_name; }; struct FieldList @@ -542,6 +546,35 @@ static void Generate(kj::StringPtr src_prefix, } fields.mergeFields(); + kj::StringPtr extra_param; + bool has_extra{GetAnnotationText(method.getProto(), EXTRA_PARAM_ANNOTATION_ID, &extra_param)}; + if (has_extra) { + if (is_construct || is_destroy) { + throw std::runtime_error(method_prefix + + ": $Proxy.extraParam is not supported on construct and destroy methods, which have no " + "corresponding C++ method"); + } + // The extra entries go right after the last field that + // consumes C++ arguments, so the parameters are last in + // the C++ method signature. + auto pos = fields.fields.begin(); + for (auto it = pos; it != fields.fields.end(); ++it) { + if (!it->skip && it->args > 0) pos = std::next(it); + } + int extra_count{0}; + for (const auto annotation : method.getProto().getAnnotations()) { + if (annotation.getId() != EXTRA_PARAM_ANNOTATION_ID) continue; + Field field; + field.extra = true; + // The annotation text may be empty, so synthesize a name for the C++ parameter. + const kj::StringPtr name{annotation.getValue().getText()}; + field.extra_name = name.size() > 0 ? kj::str(name) : kj::str("unnamed_extra_param", extra_count); + field.args = 1; + ++extra_count; + pos = std::next(fields.fields.insert(pos, std::move(field))); + } + } + if (!is_construct && !is_destroy && (&method_interface == &interface)) { methods << "template<>\n"; methods << "struct ProxyMethod<" << method_prefix << "Params>\n"; @@ -559,9 +592,14 @@ static void Generate(kj::StringPtr src_prefix, for (const auto& field : fields.fields) { if (field.skip) continue; - const auto& f = field.param_is_set ? field.param : field.result; - auto field_name = f.getProto().getName(); - add_accessor(field_name); + kj::StringPtr field_name; + const bool extra{field.extra}; + if (extra) { + field_name = field.extra_name; + } else { + field_name = (field.param_is_set ? field.param : field.result).getProto().getName(); + add_accessor(field_name); + } std::ostringstream fwd_args; for (int i = 0; i < field.args; ++i) { @@ -579,6 +617,12 @@ static void Generate(kj::StringPtr src_prefix, ++argc; } + + if (extra) { + // No capnp field, no accessor. + client_invoke << ", MakeClientParam(" << fwd_args.str() << ")"; + continue; + } client_invoke << ", "; if (field.exception.size()) { diff --git a/test/mp/test/foo-types.h b/test/mp/test/foo-types.h index 95f0ba51..269f63a7 100644 --- a/test/mp/test/foo-types.h +++ b/test/mp/test/foo-types.h @@ -112,6 +112,18 @@ inline void CustomPassMessage(InvokeContext&, fn(mut); builder.setMessage(mut.message + " return"); } + +inline void CustomBuildExtraParam(TypeList, ClientInvokeContext& invoke_context, int&& value) +{ + auto& loop = *invoke_context.connection.m_loop; + if (loop.testing_hook_misc) loop.testing_hook_misc(value); +} + +template +int CustomReadExtraParam(TypeList, ServerContext&) +{ + return 1; +} } // namespace mp #endif // MP_TEST_FOO_TYPES_H diff --git a/test/mp/test/foo.capnp b/test/mp/test/foo.capnp index 2480c8e2..4313730c 100644 --- a/test/mp/test/foo.capnp +++ b/test/mp/test/foo.capnp @@ -39,6 +39,7 @@ interface FooInterface $Proxy.wrap("mp::test::FooImplementation") { passDataPointers @22 (arg :List(Data)) -> (result :List(Data)); listBars @25 (context :Proxy.Context, n :Int32) -> (result :List(BarInterface)); callMessageAsync @26 (context :Proxy.Context) -> (result :FooMessage); + passExtra @27 (arg :Int32) -> (result :Int32) $Proxy.extraParam("extra"); } interface FooInit $Proxy.wrap("mp::test::FooInit") { diff --git a/test/mp/test/foo.h b/test/mp/test/foo.h index f6d4b7af..7123e389 100644 --- a/test/mp/test/foo.h +++ b/test/mp/test/foo.h @@ -125,6 +125,7 @@ class FooImplementation FooEnum passEnum(FooEnum foo) { return foo; } double passDouble(double value) { return value; } int passFn(std::function fn) { return fn(); } + int passExtra(int arg, int extra) { return arg + extra; } std::vector passDataPointers(std::vector values) { return values; } std::vector> listBars(int n) { diff --git a/test/mp/test/test.cpp b/test/mp/test/test.cpp index c53c83d7..1e4cdbf3 100644 --- a/test/mp/test/test.cpp +++ b/test/mp/test/test.cpp @@ -266,6 +266,15 @@ KJ_TEST("Call FooInterface methods") KJ_EXPECT(foo->passFn([]{ return 10; }) == 10); + // The `CustomReadExtraParam` overload in `foo-types.h` builds the + // server-side value, hardcoded to 1. As a result this always returns + // arg + 1 regardless of the value passed for extra. + int client_extra{0}; + foo->m_context.loop->testing_hook_misc = [&](std::any value) { client_extra = std::any_cast(value); }; + KJ_EXPECT(foo->passExtra(1, 999) == 2); + KJ_EXPECT(client_extra == 999); + foo->m_context.loop->testing_hook_misc = nullptr; + // Recursive async IPC calls KJ_EXPECT(foo->passFn([foo]{ return foo->passFn([]{ return 1; }); From 43c37e9e18afc16cb0421cb39623e73cc534046c Mon Sep 17 00:00:00 2001 From: xyzconstant <263061129+xyzconstant@users.noreply.github.com> Date: Sun, 23 Aug 2026 05:30:06 -0300 Subject: [PATCH 2/6] proxy: rename `cancel_lock` and `cancel_mutex` to `request_lock` and `request_mutex` The mutex guards the request's params and results structs, not the cancellation itself. The old names would be confusing next to the CancelState class added in the following commits. Pure rename, no behavior change. --- include/mp/proxy-io.h | 4 ++-- include/mp/proxy-types.h | 8 ++++---- include/mp/type-context.h | 20 ++++++++++---------- test/mp/test/test.cpp | 2 +- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/include/mp/proxy-io.h b/include/mp/proxy-io.h index c4633cca..2d982cbe 100644 --- a/include/mp/proxy-io.h +++ b/include/mp/proxy-io.h @@ -59,11 +59,11 @@ struct ServerInvokeContext : InvokeContext //! results structs if the request is canceled while the worker thread is //! reading params (`call_context.getParams()`) or writing results //! (`call_context.getResults()`). - Lock* cancel_lock{nullptr}; + Lock* request_lock{nullptr}; //! For IPC methods that execute asynchronously, not on the event-loop //! thread, this is set to true if the IPC call was canceled by the client //! or canceled by a disconnection. If the call runs on the event-loop - //! thread, it can't be canceled. This should be accessed with cancel_lock + //! thread, it can't be canceled. This should be accessed with request_lock //! held if it is not null, since in the asynchronous case it is accessed //! from multiple threads. bool request_canceled{false}; diff --git a/include/mp/proxy-types.h b/include/mp/proxy-types.h index 992fbf94..11ab0ac0 100644 --- a/include/mp/proxy-types.h +++ b/include/mp/proxy-types.h @@ -586,7 +586,7 @@ struct ServerCall template decltype(auto) invoke(ServerContext& server_context, TypeList, Args&&... args) const { - // Construct the extra parameters before cancel_lock is released below. + // Construct the extra parameters before request_lock is released below. // CustomReadExtraParam overloads build values from the request being // executed, so they need the same protection as normal capnp fields // from the event loop deleting request state on cancellation. @@ -595,14 +595,14 @@ struct ServerCall "Declare extra parameters with $Proxy.extraParam in the Cap'n Proto schema and add a matching " "`CustomReadExtraParam` overload."); std::tuple...> extra{CustomReadExtraParam(TypeList>(), server_context)...}; - // If cancel_lock is set, release it while executing the method, and + // If request_lock is set, release it while executing the method, and // reacquire it afterwards. The lock is needed to prevent params and // response structs from being deleted by the event loop thread if the // request is canceled, so it is only needed before and after method // execution. It is important to release the lock during execution // because the method can take arbitrarily long to return and the event // loop will need the lock itself in on_cancel if the call is canceled. - if (server_context.cancel_lock) server_context.cancel_lock->m_lock.unlock(); + if (server_context.request_lock) server_context.request_lock->m_lock.unlock(); return TryFinally( [&]() -> decltype(auto) { return std::apply( @@ -614,7 +614,7 @@ struct ServerCall extra); }, [&] { - if (server_context.cancel_lock) server_context.cancel_lock->m_lock.lock(); + if (server_context.request_lock) server_context.request_lock->m_lock.lock(); // If the IPC request was canceled, throw InterruptException // because there is no point continuing and trying to fill the // call_context.getResults() struct. It's also important to stop diff --git a/include/mp/type-context.h b/include/mp/type-context.h index 2407b29a..650fecd7 100644 --- a/include/mp/type-context.h +++ b/include/mp/type-context.h @@ -99,9 +99,9 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn& auto& request_threads = thread_context.request_threads; ConnThread request_thread; bool inserted{false}; - Mutex cancel_mutex; - Lock cancel_lock{cancel_mutex}; - server_context.cancel_lock = &cancel_lock; + Mutex request_mutex; + Lock request_lock{request_mutex}; + server_context.request_lock = &request_lock; loop.sync([&] { // Detect request being canceled before it executes. if (cancel_monitor.m_canceled) { @@ -110,9 +110,9 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn& } // Detect request being canceled while it executes. assert(!cancel_monitor.m_on_cancel); - cancel_monitor.m_on_cancel = [&loop, &server_context, &cancel_mutex, req]() { + cancel_monitor.m_on_cancel = [&loop, &server_context, &request_mutex, req]() { MP_LOG(loop, Log::Info) << "IPC server request #" << req << " canceled while executing."; - // Lock cancel_mutex here to block the event loop + // Lock request_mutex here to block the event loop // thread and prevent it from deleting the request's // params and response structs while the execution // thread is accessing them. Because this lock is @@ -123,7 +123,7 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn& // it. So in addition to locking the mutex, the // execution thread always checks request_canceled // as well before accessing the structs. - Lock cancel_lock{cancel_mutex}; + Lock request_lock{request_mutex}; server_context.request_canceled = true; }; // Update requests_threads map if not canceled. We know @@ -154,14 +154,14 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn& // makes another IPC call), so avoid modifying the map. const bool erase_thread{inserted}; KJ_DEFER( - // Release the cancel lock before calling loop->sync and + // Release the request lock before calling loop->sync and // waiting for the event loop thread, because if a // cancellation happened, it needs to run the on_cancel - // callback above. It's safe to release cancel_lock at + // callback above. It's safe to release request_lock at // this point because the fn.invoke() call below will be // finished and no longer accessing the params or // results structs. - cancel_lock.m_lock.unlock(); + request_lock.m_lock.unlock(); // Erase the request_threads entry on the event loop // thread with loop->sync(), so if the connection is // broken there is not a race between this thread and @@ -174,7 +174,7 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn& // cancellation happened. So we do not need to be // notified of cancellations after this point. Also // we do not want to be notified because - // cancel_mutex and server_context could be out of + // request_mutex and server_context could be out of // scope when it happens. cancel_monitor.m_on_cancel = nullptr; auto self_dispose{kj::mv(self)}; diff --git a/test/mp/test/test.cpp b/test/mp/test/test.cpp index 1e4cdbf3..0d04e364 100644 --- a/test/mp/test/test.cpp +++ b/test/mp/test/test.cpp @@ -537,7 +537,7 @@ KJ_TEST("Calling async IPC method, with server disconnect after cleanup") // Use testing_hook_async_request_done to trigger a disconnect from the // worker thread after it executes an async request but before it returns. // Without the bugfix, the m_on_cancel callback would be called at this - // point, accessing the cancel_mutex stack variable that had gone out of + // point, accessing the request_mutex stack variable that had gone out of // scope. TestSetup setup; ProxyClient* foo = setup.client.get(); From 5913f1c14364e71ebfe34c3cf19e2e3bf48b78de Mon Sep 17 00:00:00 2001 From: xyzconstant <263061129+xyzconstant@users.noreply.github.com> Date: Sun, 23 Aug 2026 09:20:39 -0300 Subject: [PATCH 3/6] proxy: expose cancel and error handling hooks in ClientInvokeContext Add two optional hooks to `ClientInvokeContext` that client overloads can set: - `set_canceler` receives the `kj::Canceler` object wrapping the request promise once it is sent, and null when the request finishes and the canceler is destroyed. `clientInvoke` only creates the canceler (heap-allocated and attached to the promise) if the hook is set. - `handle_error` receives the `kj::Exception` of a failed request and lets clients throw a custom exception, which `clientInvoke` rethrows to the caller. For example, `kj::Canceler::cancel` raises `DISCONNECTED`, the same error a disconnect produces, so only the parameter that canceled can tell them apart. --- include/mp/proxy-io.h | 13 +++++++++++++ include/mp/proxy-types.h | 34 +++++++++++++++++++++++++++++++--- src/mp/gen.cpp | 8 ++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/include/mp/proxy-io.h b/include/mp/proxy-io.h index 2d982cbe..19f5efa9 100644 --- a/include/mp/proxy-io.h +++ b/include/mp/proxy-io.h @@ -18,6 +18,8 @@ #include #include #include +#include +#include #include #include #include @@ -40,6 +42,17 @@ struct InvokeContext struct ClientInvokeContext : InvokeContext { ThreadContext& thread_context; + //! Callback that overloads can set while building a parameter to make a + //! function call cancelable. If set, clientInvoke calls it on the event loop + //! thread with the kj::Canceler object wrapping the request when the request is + //! sent, and with null when the request finishes and the canceler is destroyed. + std::function set_canceler; + //! Callback that lets clients set a custom error handler for kj::Exception. + //! For example, a canceled request fails the same way a disconnect does, + //! and this is where the cancellation parameter can throw its own + //! exception instead. If set, clientInvoke calls it and rethrows anything + //! it throws. + std::function handle_error; ClientInvokeContext(Connection& conn, ThreadContext& thread_context) : InvokeContext{conn}, thread_context{thread_context} { diff --git a/include/mp/proxy-types.h b/include/mp/proxy-types.h index 11ab0ac0..4f7de2ff 100644 --- a/include/mp/proxy-types.h +++ b/include/mp/proxy-types.h @@ -8,6 +8,9 @@ #include #include +#include +#include +#include #include #include #include @@ -813,7 +816,18 @@ void clientInvoke(ProxyClient& proxy_client, const GetRequest& get_request, Fiel MP_LOGPLAIN(*proxy_client.m_context.loop, Log::Trace) << "send data: " << LogEscape(request.toString(), proxy_client.m_context.loop->m_log_opts.max_chars); - proxy_client.m_context.loop->m_task_set->add(request.send().then( + kj::Promise<::capnp::Response> promise{request.send()}; + + // If `set_canceler` was set, construct a kj::Canceler object and wrap + // the request promise with it. + kj::Own canceler; + if (invoke_context->set_canceler) { + canceler = kj::heap(); + promise = canceler->wrap(kj::mv(promise)); + invoke_context->set_canceler(canceler.get()); + } + + proxy_client.m_context.loop->m_task_set->add(promise.then( [&](::capnp::Response&& response) { MP_LOGPLAIN(*proxy_client.m_context.loop, Log::Debug) << "{" << thread_context.thread_name << "} IPC client recv " @@ -831,7 +845,16 @@ void clientInvoke(ProxyClient& proxy_client, const GetRequest& get_request, Fiel thread_context.waiter->m_cv.notify_all(); }, [&](const ::kj::Exception& e) { - if (e.getType() == ::kj::Exception::Type::DISCONNECTED) { + if (invoke_context->handle_error) { + try { + invoke_context->handle_error(e); + } catch (...) { + exception = std::current_exception(); + } + } + if (exception) { + // Rethrown below. + } else if (e.getType() == ::kj::Exception::Type::DISCONNECTED) { disconnected = "IPC client method call interrupted by disconnect."; } else { kj_exception = kj::str("kj::Exception: ", e).cStr(); @@ -841,7 +864,12 @@ void clientInvoke(ProxyClient& proxy_client, const GetRequest& get_request, Fiel const Lock lock(thread_context.waiter->m_mutex); done = true; thread_context.waiter->m_cv.notify_all(); - })); + }).attach(kj::defer([canceler = kj::mv(canceler), set_canceler = invoke_context->set_canceler] { + // Runs on the event loop thread when the request promise is + // destroyed. Tell the cancellation parameter the canceler is + // about to go away. + if (set_canceler) set_canceler(nullptr); + }))); }); Lock lock(thread_context.waiter->m_mutex); diff --git a/src/mp/gen.cpp b/src/mp/gen.cpp index 0eb45ded..fc2ef12c 100644 --- a/src/mp/gen.cpp +++ b/src/mp/gen.cpp @@ -319,6 +319,13 @@ static void Generate(kj::StringPtr src_prefix, std::ofstream cpp_client(output_path + ".proxy-client.c++"); cpp_client << "// Generated by " PROXY_BIN " from " << src_file << "\n\n"; + cpp_client << "// GCC reports false positives in Cap'n Proto's kj::Maybe and\n"; + cpp_client << "// kj::_::ExceptionOr internals when clientInvoke wraps request\n"; + cpp_client << "// promises in a kj::Canceler. The pragma must precede the\n"; + cpp_client << "// includes to cover warnings reported at header locations.\n"; + cpp_client << "#if defined(__GNUC__) && !defined(__clang__)\n"; + cpp_client << "#pragma GCC diagnostic ignored \"-Wmaybe-uninitialized\"\n"; + cpp_client << "#endif\n\n"; cpp_client << "// IWYU pragma: no_include \n"; cpp_client << "// IWYU pragma: no_include \n"; cpp_client << "// IWYU pragma: begin_keep\n"; @@ -331,6 +338,7 @@ static void Generate(kj::StringPtr src_prefix, cpp_client << "#include \n"; cpp_client << "#include \n"; cpp_client << "#include \n"; + cpp_client << "#include \n"; cpp_client << "#include \n"; cpp_client << "#include \n"; cpp_client << "#include \n"; From 4083b4cb15664e7e151b57de3b93a70f2a6d1a14 Mon Sep 17 00:00:00 2001 From: xyzconstant <263061129+xyzconstant@users.noreply.github.com> Date: Mon, 24 Aug 2026 14:01:20 -0300 Subject: [PATCH 4/6] proxy: expose request cancellation state in ServerInvokeContext Add two members to `ServerInvokeContext` that server overloads can use: - `request_mutex` is the mutex `request_lock` refers to, set together with it by the `Proxy.Context` `PassField` overload, and null for methods running on the event loop thread. Overloads lock it to touch `cancel_fn`. - `cancel_fn` holds a callback that the cancel monitor runs on the event loop thread, when the request is canceled while the method executes. `ServerCall` clears it after the method returns. --- include/mp/proxy-io.h | 8 ++++++++ include/mp/proxy-types.h | 3 +++ include/mp/type-context.h | 2 ++ include/mp/util.h | 10 ++++------ 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/include/mp/proxy-io.h b/include/mp/proxy-io.h index 19f5efa9..31314eec 100644 --- a/include/mp/proxy-io.h +++ b/include/mp/proxy-io.h @@ -74,12 +74,20 @@ struct ServerInvokeContext : InvokeContext //! (`call_context.getResults()`). Lock* request_lock{nullptr}; //! For IPC methods that execute asynchronously, not on the event-loop + //! thread: mutex request_lock refers to, set together with it. Null for + //! methods executing on the event-loop thread. + Mutex* request_mutex{nullptr}; + //! For IPC methods that execute asynchronously, not on the event-loop //! thread, this is set to true if the IPC call was canceled by the client //! or canceled by a disconnection. If the call runs on the event-loop //! thread, it can't be canceled. This should be accessed with request_lock //! held if it is not null, since in the asynchronous case it is accessed //! from multiple threads. bool request_canceled{false}; + //! For IPC methods that execute asynchronously, not on the event-loop + //! thread: callback registered by a wrapped method. Runs when request + //! cancellation is detected. + std::function cancel_fn; ServerInvokeContext(ProxyServer& proxy_server, CallContext& call_context, int req) : InvokeContext{*proxy_server.m_context.connection}, proxy_server{proxy_server}, call_context{call_context}, req{req} diff --git a/include/mp/proxy-types.h b/include/mp/proxy-types.h index 4f7de2ff..803e6d79 100644 --- a/include/mp/proxy-types.h +++ b/include/mp/proxy-types.h @@ -618,6 +618,9 @@ struct ServerCall }, [&] { if (server_context.request_lock) server_context.request_lock->m_lock.lock(); + // The method returned, so destroy the callback it registered + // through its cancellation argument, if any. + server_context.cancel_fn = nullptr; // If the IPC request was canceled, throw InterruptException // because there is no point continuing and trying to fill the // call_context.getResults() struct. It's also important to stop diff --git a/include/mp/type-context.h b/include/mp/type-context.h index 650fecd7..78e12bd9 100644 --- a/include/mp/type-context.h +++ b/include/mp/type-context.h @@ -102,6 +102,7 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn& Mutex request_mutex; Lock request_lock{request_mutex}; server_context.request_lock = &request_lock; + server_context.request_mutex = &request_mutex; loop.sync([&] { // Detect request being canceled before it executes. if (cancel_monitor.m_canceled) { @@ -125,6 +126,7 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn& // as well before accessing the structs. Lock request_lock{request_mutex}; server_context.request_canceled = true; + if (server_context.cancel_fn) server_context.cancel_fn(); }; // Update requests_threads map if not canceled. We know // the request is not canceled currently because diff --git a/include/mp/util.h b/include/mp/util.h index adc7cfbb..87e843b3 100644 --- a/include/mp/util.h +++ b/include/mp/util.h @@ -359,12 +359,10 @@ struct InterruptException final : std::exception { class CancelProbe; -//! Helper class that detects when a promise is canceled. Used to detect -//! canceled requests and prevent potential crashes on unclean disconnects. -//! -//! In the future, this could also be used to support a way for wrapped C++ -//! methods to detect cancellation (like approach #4 in -//! https://github.com/bitcoin/bitcoin/issues/33575). +//! Helper class that reports request cancellation when the promise executing +//! its IPC method is destroyed. Cap'n Proto abandons a call by destroying +//! that promise so the paired `CancelProbe` is attached to it, and its +//! destructor notifies this class. class CancelMonitor { public: From 8aa26b18f8aa4295bb8ed5c59fb5804a62b1d8f6 Mon Sep 17 00:00:00 2001 From: xyzconstant <263061129+xyzconstant@users.noreply.github.com> Date: Mon, 24 Aug 2026 15:49:39 -0300 Subject: [PATCH 5/6] build: require Cap'n Proto 1.0 The `$Cxx.allowCancellation` annotation used by the cancellation tests does not exist in older versions. Remove the configure-time checks that only covered them, and move the olddeps CI config to 1.0.0. --- CMakeLists.txt | 60 ++--------------------------------------- ci/configs/olddeps.bash | 2 +- doc/install.md | 2 +- 3 files changed, 4 insertions(+), 60 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3462f3e7..41c56d50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,72 +13,16 @@ endif() include("cmake/compat_find.cmake") find_package(Threads REQUIRED) -find_package(CapnProto 0.9 NO_MODULE) +find_package(CapnProto 1.0 NO_MODULE) if(NOT CapnProto_FOUND) message(FATAL_ERROR "Cap'n Proto is required but was not found.\n" "To resolve, choose one of the following:\n" - " - Install Cap'n Proto (version 1.0+ recommended)\n" + " - Install Cap'n Proto (version 1.0+ required)\n" " - For Bitcoin Core compilation build with -DENABLE_IPC=OFF to disable multiprocess support\n" ) endif() -# Cap'n Proto compatibility checks -set(CAPNPROTO_ISSUES "") -set(CAPNPROTO_CVE_AFFECTED FALSE) -set(CAPNPROTO_CLANG_INCOMPATIBLE FALSE) - -# Check for list-of-pointers memory access bug from Nov 2022 -# https://nvd.nist.gov/vuln/detail/CVE-2022-46149 -# https://github.com/advisories/GHSA-qqff-4vw4-f6hx -# https://github.com/capnproto/capnproto/security/advisories/GHSA-qqff-4vw4-f6hx -# https://github.com/capnproto/capnproto/blob/master/security-advisories/2022-11-30-0-pointer-list-bounds.md -# https://capnproto.org/news/2022-11-30-CVE-2022-46149-security-advisory.html -# https://dwrensha.github.io/capnproto-rust/2022/11/30/out_of_bounds_memory_access_bug.html -if(CapnProto_VERSION STREQUAL "0.9.0" - OR CapnProto_VERSION STREQUAL "0.9.1" - OR CapnProto_VERSION STREQUAL "0.10.0" - OR CapnProto_VERSION STREQUAL "0.10.1" - OR CapnProto_VERSION STREQUAL "0.10.2") - set(CAPNPROTO_CVE_AFFECTED TRUE) - string(APPEND CAPNPROTO_ISSUES "- CVE-2022-46149 security vulnerability (details: https://github.com/advisories/GHSA-qqff-4vw4-f6hx)\n") -endif() - -# Check for Cap'n Proto / Clang / C++20 incompatibility -# Cap'n Proto 0.9.x and 0.10.x are incompatible with Clang 16+ when using C++20 -# due to P2468R2 implementation. This was fixed in Cap'n Proto 1.0+. -# See: https://github.com/bitcoin-core/libmultiprocess/issues/199 -if((CapnProto_VERSION VERSION_GREATER_EQUAL "0.9.0") AND - (CapnProto_VERSION VERSION_LESS "1.0.0") AND - (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") AND - (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "16") AND - (CMAKE_CXX_STANDARD EQUAL 20)) - set(CAPNPROTO_CLANG_INCOMPATIBLE TRUE) - string(APPEND CAPNPROTO_ISSUES "- Incompatible with Clang ${CMAKE_CXX_COMPILER_VERSION} when using C++20\n") -endif() - -if(CAPNPROTO_CVE_AFFECTED OR CAPNPROTO_CLANG_INCOMPATIBLE) - set(RESOLUTION_OPTIONS "") - - # Fixes both issues - string(APPEND RESOLUTION_OPTIONS " - Upgrade to Cap'n Proto version 1.0 or newer (recommended)\n") - - if(CAPNPROTO_CVE_AFFECTED AND NOT CAPNPROTO_CLANG_INCOMPATIBLE) - string(APPEND RESOLUTION_OPTIONS " - Upgrade to a patched minor version (0.9.2, 0.10.3, or later)\n") - elseif(CAPNPROTO_CLANG_INCOMPATIBLE AND NOT CAPNPROTO_CVE_AFFECTED) - string(APPEND RESOLUTION_OPTIONS " - Use GCC instead of Clang\n") - endif() - - string(APPEND RESOLUTION_OPTIONS " - For Bitcoin Core compilation build with -DENABLE_IPC=OFF to disable multiprocess support\n") - - message(FATAL_ERROR - "The version of Cap'n Proto detected: ${CapnProto_VERSION} has known compatibility issues:\n" - "${CAPNPROTO_ISSUES}" - "To resolve, choose one of the following:\n" - "${RESOLUTION_OPTIONS}" - ) -endif() - set(MPGEN_EXECUTABLE "" CACHE FILEPATH "If specified, should be full path to an external mpgen binary to use rather than the one built internally.") option(MP_ENABLE_CLANG_TIDY "Run clang-tidy with the compiler." OFF) diff --git a/ci/configs/olddeps.bash b/ci/configs/olddeps.bash index b2bc08b0..cea647d1 100644 --- a/ci/configs/olddeps.bash +++ b/ci/configs/olddeps.bash @@ -3,6 +3,6 @@ CI_DIR=build-olddeps # Pin olddeps to an older Nixpkgs channel, to be able to test an older GCC. CI_NIXPKGS_CHANNEL=nixos-25.05 export CXXFLAGS="-Werror -Wall -Wextra -Wpedantic -Wunused -Wunused-const-variable -Wno-error=array-bounds" -CAPNP_CHECKOUT=v0.9.2 # Use a checkout to compile it with the selected GCC +CAPNP_CHECKOUT=v1.0.0 # Use a checkout to compile it with the selected GCC NIX_ARGS=(--argstr capnprotoVersion "none" --argstr cmakeVersion "3.12.4" --argstr gccVersion "10") BUILD_ARGS=(-k) diff --git a/doc/install.md b/doc/install.md index 015cfd19..699dd469 100644 --- a/doc/install.md +++ b/doc/install.md @@ -1,6 +1,6 @@ # libmultiprocess Installation -Installation currently requires Cap'n Proto 0.9 or higher: +Installation currently requires Cap'n Proto 1.0 or higher: ```sh apt install libcapnp-dev capnproto From 7d1decda30834265268d072728cc91c9328c5e64 Mon Sep 17 00:00:00 2001 From: xyzconstant <263061129+xyzconstant@users.noreply.github.com> Date: Wed, 9 Sep 2026 20:04:44 -0300 Subject: [PATCH 6/6] test: Add testing for request cancellation - One test cancels an in-flight `ProxyClient` call from another thread. - Another drops the response promise mid-execution, imitating non-libmultiprocess clients. Additionally, this commit adds a reference implementation of a cancel parameter and its corresponding `CustomBuildExtraParam`/`CustomReadExtraParam` overloads that applications could replicate to fully support cancellations. --- test/mp/test/foo-types.h | 80 ++++++++++++++++++++++++++++++++ test/mp/test/foo.capnp | 1 + test/mp/test/foo.h | 37 +++++++++++++++ test/mp/test/test.cpp | 98 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 216 insertions(+) diff --git a/test/mp/test/foo-types.h b/test/mp/test/foo-types.h index 269f63a7..127efe08 100644 --- a/test/mp/test/foo-types.h +++ b/test/mp/test/foo-types.h @@ -33,6 +33,10 @@ #include // IWYU pragma: end_exports +#include +#include +#include + namespace mp { namespace test { namespace messages { @@ -43,6 +47,46 @@ struct FooInterface; // IWYU pragma: export struct BarInterface; // IWYU pragma: export } // namespace messages +class CancelFnState +{ +public: + explicit CancelFnState(EventLoop& loop) : m_loop(loop) {} + + void setCanceler(kj::Canceler* canceler) + { + const Lock lock{m_mutex}; + m_canceler = canceler; + if (m_canceler && m_canceled) m_canceler->cancel("canceled by client"); + } + + //! Cancel the request. Callable from any thread. + void cancel() + { + { + const Lock lock{m_mutex}; + if (m_canceled) return; + m_canceled = true; + if (!m_canceler) return; + } + m_loop->sync([&] { + const Lock lock{m_mutex}; + if (m_canceler) m_canceler->cancel("canceled by client"); + }); + } + + bool canceled() + { + const Lock lock{m_mutex}; + return m_canceled; + } + +private: + EventLoopRef m_loop; + Mutex m_mutex; + bool m_canceled MP_GUARDED_BY(m_mutex){false}; + kj::Canceler* m_canceler MP_GUARDED_BY(m_mutex){nullptr}; +}; + template void CustomBuildField(TypeList, Priority<1>, InvokeContext& invoke_context, const FooCustom& value, Output&& output) { @@ -124,6 +168,42 @@ int CustomReadExtraParam(TypeList, ServerContext&) { return 1; } + +inline void CustomBuildExtraParam(TypeList, ClientInvokeContext& invoke_context, test::CancelArg&& value) +{ + if (!value) return; + auto state{std::make_shared(*invoke_context.connection.m_loop)}; + invoke_context.set_canceler = [state](kj::Canceler* canceler) { state->setCanceler(canceler); }; + invoke_context.handle_error = [state](const kj::Exception&) { + if (state->canceled()) throw InterruptException{"canceled"}; + }; + // The `CancelGuard` object returned on the client side has nothing to unregister. + static_cast(value([state] { state->cancel(); })); +} + +template +test::CancelArg CustomReadExtraParam(TypeList, ServerContext& server_context) +{ + if (!server_context.request_mutex) { + // Not an async method means this method will run on the event loop + // thread which fires cancellations, just return a dummy function. + return [](test::CancelFn) { return test::CancelGuard{}; }; + } + return [&server_context](test::CancelFn fn) { + { + const Lock lock{*server_context.request_mutex}; + if (!server_context.request_canceled) { + server_context.cancel_fn = std::move(fn); + return test::CancelGuard{[&server_context] { + const Lock lock{*server_context.request_mutex}; + server_context.cancel_fn = nullptr; + }}; + } + } + fn(); + return test::CancelGuard{}; + }; +} } // namespace mp #endif // MP_TEST_FOO_TYPES_H diff --git a/test/mp/test/foo.capnp b/test/mp/test/foo.capnp index 4313730c..320c5037 100644 --- a/test/mp/test/foo.capnp +++ b/test/mp/test/foo.capnp @@ -40,6 +40,7 @@ interface FooInterface $Proxy.wrap("mp::test::FooImplementation") { listBars @25 (context :Proxy.Context, n :Int32) -> (result :List(BarInterface)); callMessageAsync @26 (context :Proxy.Context) -> (result :FooMessage); passExtra @27 (arg :Int32) -> (result :Int32) $Proxy.extraParam("extra"); + callCancelFnAsync @28 (context :Proxy.Context) -> () $Proxy.extraParam("cancel") $Cxx.allowCancellation; } interface FooInit $Proxy.wrap("mp::test::FooInit") { diff --git a/test/mp/test/foo.h b/test/mp/test/foo.h index 7123e389..e1d63969 100644 --- a/test/mp/test/foo.h +++ b/test/mp/test/foo.h @@ -15,6 +15,7 @@ #include #include #include +#include #include namespace mp { @@ -101,6 +102,36 @@ class SimpleBar : public Bar int value() override { return m_value; } int m_value; }; + +using CancelFn = std::function; + +class [[nodiscard]] CancelGuard +{ +public: + CancelGuard() = default; + explicit CancelGuard(std::function unregister) : m_unregister(std::move(unregister)) {} + CancelGuard(CancelGuard&& other) noexcept : m_unregister(std::exchange(other.m_unregister, nullptr)) {} + CancelGuard(const CancelGuard&) = delete; + CancelGuard& operator=(const CancelGuard&) = delete; + CancelGuard& operator=(CancelGuard&& other) noexcept + { + if (this != &other) { + if (m_unregister) m_unregister(); + m_unregister = std::exchange(other.m_unregister, nullptr); + } + return *this; + } + ~CancelGuard() + { + if (m_unregister) m_unregister(); + } + +private: + std::function m_unregister; +}; + +using CancelArg = std::function; + class FooImplementation { public: @@ -139,8 +170,14 @@ class FooImplementation void callFnAsync() { assert(m_fn); m_fn(); } int callIntFnAsync(int arg) { assert(m_int_fn); return m_int_fn(arg); } FooMessage callMessageAsync() { assert(m_fn); m_fn(); return {}; } + void callCancelFnAsync(CancelArg cancel) + { + assert(m_cancel_fn); + m_cancel_fn(std::move(cancel)); + } std::function m_fn; std::function m_int_fn; + std::function m_cancel_fn; }; } // namespace test diff --git a/test/mp/test/test.cpp b/test/mp/test/test.cpp index 0d04e364..79033398 100644 --- a/test/mp/test/test.cpp +++ b/test/mp/test/test.cpp @@ -27,6 +27,7 @@ #include // IWYU pragma: keep #include #include +#include #include #include #include @@ -886,5 +887,102 @@ KJ_TEST("Calling IPC method after destroy and destroy during a call") loop.sync([&] { bar2.reset(); }); } +KJ_TEST("Cancel an in-flight IPC call") +{ + TestSetup setup; + ProxyClient* foo = setup.client.get(); + foo->initThreadMap(); + std::promise waiting; + std::promise done; + + // Install a function that blocks until its `CancelArg` fires, so + // cancellation is the only way out. + setup.server->m_impl->m_cancel_fn = [&](CancelArg cancel) { + std::mutex mutex; + std::condition_variable cv; + bool canceled = false; + const CancelGuard guard{cancel([&] { + const std::scoped_lock lock{mutex}; + canceled = true; + cv.notify_all(); + })}; + std::unique_lock lock{mutex}; + waiting.set_value(); + cv.wait(lock, [&] { return canceled; }); + done.set_value(); + }; + + std::promise cancel_fn; + std::thread canceler([&] { + CancelFn fire{cancel_fn.get_future().get()}; + waiting.get_future().wait(); + fire(); + }); + bool interrupted = false; + try { + foo->callCancelFnAsync([&](CancelFn fn) { + cancel_fn.set_value(std::move(fn)); + return CancelGuard{}; + }); + } catch (const InterruptException&) { + interrupted = true; + } + canceler.join(); + KJ_EXPECT(interrupted); + KJ_EXPECT(done.get_future().wait_for(std::chrono::minutes{5}) == std::future_status::ready); + + // Connection should be unaffected. + KJ_EXPECT(foo->add(1, 2) == 3); +} + +KJ_TEST("Dropping the client promise cancels an executing method") +{ + TestSetup setup; + constexpr std::chrono::seconds timeout{30}; + std::promise waiting; + std::promise done; + + // Install a function that blocks until its `CancelArg` fires, so + // cancellation is the only way out. + setup.server->m_impl->m_cancel_fn = [&](CancelArg cancel) { + std::mutex mutex; + std::condition_variable cv; + bool canceled = false; + const CancelGuard guard{cancel([&] { + const std::scoped_lock lock{mutex}; + canceled = true; + cv.notify_all(); + })}; + std::unique_lock lock{mutex}; + waiting.set_value(); + cv.wait(lock, [&] { return canceled; }); + done.set_value(); + }; + ProxyClient* foo{setup.client.get()}; + foo->initThreadMap(); + + // Build the request by hand, the way a non-C++ client would. A normal + // proxy call cannot be abandoned because `clientInvoke` blocks on it. + std::optional> remote; + foo->m_context.loop->sync([&] { + auto request{foo->m_client.callCancelFnAsyncRequest()}; + request.initContext().setThread( + foo->m_context.connection->m_thread_map.makeThreadRequest().send().getResult()); + remote.emplace(request.send()); + }); + KJ_REQUIRE(waiting.get_future().wait_for(timeout) == std::future_status::ready); + + auto done_future{done.get_future()}; + KJ_EXPECT(done_future.wait_for(std::chrono::seconds{0}) == std::future_status::timeout); + + // Abandon the call without disconnecting. + foo->m_context.loop->sync([&] { remote.reset(); }); + + KJ_EXPECT(done_future.wait_for(timeout) == std::future_status::ready); + + // Connection should be unaffected. + KJ_EXPECT(foo->add(1, 2) == 3); +} + } // namespace test } // namespace mp