Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
3300995 to
0986a13
Compare
|
CI failures seem unrelated |
|
Nice work! I've had a number of ideas about this feature over the years so it's really interesting to see it implemented. I will just give some quick thoughts for now so I don't get nerdsniped and wind up spending all day or more on this. Thoughts:
EDIT: Added |
|
Thanks for your feedback @ryanofsky. Sorry for the late response. I took some time to fill some C++ gaps (mostly to understand whether client-side detection of the
This makes complete sense. I believe I now grasp the original idea. This is cleaner for consumers and non-libmultiprocess clients, which don't need a
Yes, you're correct here as well.
This is interesting. I think we could combine this with
I prefer this over the dedicated hooks. However, I don't fully understand how it would handle the client side. For So I think the hook can tell
Yes, definitely we can cover both sides in this PR. I need to think about this more though, together with the mpgen issue above.
Ok perfect. I'll drop it.
Thanks, I really thought it wasn't caused by this PR, but I guess I had it wrong. I have some WIP rework locally, so these errors may be different the next time I push. |
Hmm yeah. Actually when I wrote this I wasn't thinking about the need to unregister, and the
You're right, I wasn't thinking about the client side of this very clearly. I was thinking as the client code was processing parameters it could use So I think a capnproto method annotation of some kind might be necessary. I was thinking of adding annotations anyway to support more flexible mapping of c++ parameters to capnproto parameters like: in context of #282. But something less general could also work well here. |
The vector of callbacks exists because another callback (pre-existing) is registered in
So during a cancellable request, there are two registrations: one internal, and another the wrapped method itself registers. But I do agree it looks a bit awkward. The raw pointers are there so
Good, this settles the client side then. I'd like to start with something minimal that we can scale up later into something more general. I was thinking of a method annotation (your waitValue @7 (context :Proxy.Context, timeoutMs :Int32) -> (result :Int32) $Proxy.extraParam("cancel"); |
4c8b3db to
4cfc251
Compare
4821d99 to
4731f5d
Compare
4731f5d to
f6944b2
Compare
|
@ryanofsky Just addressed your comments, rebased with master, and pushed. Added 9422b97, which supports the Updated the description too. Thanks for your feedback :) |
|
Thanks for the updates! And approach ACK f6944b2. I think it's good to add client and server cancellation support together in the same PR. Quickly skimming changes, the It would also be great to see this put to use in bitcoin core by dropping the |
d2014d9 to
22000b4
Compare
6a2d1df to
913d942
Compare
8c2d02d to
371f955
Compare
|
Thanks for the update! I only skimmed 371f955 so far, but this looks like what I was hoping for: the library supports re: #342 (comment)
I'm fine with keeping One small thing: the PR description still describes the I'm planning to review this more fully this week. |
…macOS 1b47a0a ci: build Bitcoin Core with its minimum Xcode version on macOS (xyzconstant) Pull request description: The macOS job used the macos-15 runner's default Xcode version (16.4), which is older than the Xcode 26.3 minimum in Bitcoin Core's `doc/dependencies.md` and in the CI job there. Select Xcode 26.3, as Bitcoin Core's own CI does. See: bitcoin/bitcoin#36287. ACKs for top commit: maflcko: lgtm ACK 1b47a0a hebasto: ACK 1b47a0a. ryanofsky: Code review ACK 1b47a0a. This PR doesn't do what it was originally trying to do, which was to make `std::stop_token` available for #342. But matching Bitcoin Core's CI may be beneficial anyway. Tree-SHA512: 63a7b8429f1bcb1e61080bc8f090c02251294455086f988142d28e109f63dda2a12f4c00a2ddd8b1339da84fcbee6edecb9933d2b88ffa47d5eb7190b09b34f2
371f955 to
fed20c8
Compare
|
Just rebased onto master and updated the PR description. re: #342 (comment)
Yeah, this makes sense, but as you said, this way it could be modified easily later. |
9d47ded to
b9fe90d
Compare
The `$Cxx.allowCancellation` annotation used by the cancellation tests added in later commits 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.
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. A method can have several `$Proxy.extraParam` annotations, and an empty value gets a generated name. Client behavior: - A matching `CustomBuildExtraParam(TypeList<T>, ClientInvokeContext&, T&&)` overload MUST be implemented. The parameter is passed to it before the RPC message is dispatched. Server behavior: - A matching `CustomReadExtraParam(TypeList<T>, 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.
…`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 cancellation state added in the following commits. Pure rename, no behavior change.
ProxyServer<Thread>::post() runs each call on the server thread after the previous call starts, by chaining it on m_thread_ready, and passes a ready_fulfiller signaling the next call. If a call is canceled while it is waiting for the previous call to start, its continuation is destroyed along with ready_fulfiller, which rejects the next call's m_thread_ready, so the next call on the thread fails with "PromiseFulfiller was destroyed without fulfilling the promise". Currently this can only happen on disconnect, when later calls fail anyway. Once methods can have the `$Cxx.allowCancellation` annotation, a client dropping the response promise of a queued call would break the next call it makes on the same thread. Fix this by forking the previous call's ready promise, and if a call's ready promise is rejected, making the next call wait for the previous call's ready promise instead. Treating the rejection as ready would be wrong, because the previous call may not have started yet, and the next call would then be posted while the thread is busy, failing assert(posted). The new test calls post() directly, since no test methods allow cancellation yet, and fails without the fix. This change was written with Claude Opus 5.5. Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
…tions In the next commit, request promises are being wrapped in a `kj::Canceler` object, which causes GCC (versions 12 to 16) to report "'ptr' and 'disposer' may be used uninitialized [-Werror=maybe-uninitialized]" warnings. These are false positives, so just suppress them for code included through the schema header. See capnproto/capnproto#2939.
Add a `register_cancel` hook to `ClientInvokeContext` that `CustomBuildExtraParam` overloads can set. A method is cancelable if one of its `$Proxy.extraParam` parameter types is marked with the `IsCancelParam` trait, which is checked at compile time, so methods without one don't compile the cancellation code. A method can have at most one cancelable parameter, which is checked with a static_assert. When the hook is set, `clientInvoke` constructs a `kj::Canceler` to wrap the request being sent and calls the hook with a function that cancels the request. The hook returns a guard that `clientInvoke` destroys before returning. No mutex is needed for the cancellation state because it is accessed only from the event loop thread. The hook doesn't depend on a particular token type, so this builds with standard libraries that don't provide std::stop_token, like libc++ 19. A later commit adds a `std::stop_token` overload that uses the hook, allowing callers to stop waiting for a request.
Add `cancel_fn` to `ServerInvokeContext`, so a server method can react to its request being canceled while it executes. Before, cancellation was only noticed after the method returned, when `ServerCall` checks `request_canceled`. `CustomReadExtraParam` overloads can set it, since they run with `request_lock` held before the method is called. The `Proxy.Context` `PassField` overload calls it from the cancel monitor's `m_on_cancel` callback on the event loop thread, with the request mutex held, and `ServerCall` clears it after the method returns. Also update the `CancelMonitor` comment to describe when a request is canceled.
Add mp/type-stop-token.h with `CustomBuildExtraParam`/`CustomReadExtraParam` overloads for std::stop_token, so methods can take a std::stop_token extra parameter. The header is empty when the standard library doesn't provide std::stop_token, like libc++ 19, where applications can add overloads for their own token type. On the client, requesting stop cancels the request. On the server, the token is stopped when the request is canceled. The tests use a `StopToken` alias for std::stop_token, or a small replacement type with its own overloads where it isn't available. Additionally, add tests covering: - Requesting stop from another thread while a call is in flight. - Calling a method with a token that is already stopped. - Requesting stop after a call returns. - Dropping the response promise of an in-flight call, simulating the behavior of non-libmultiprocess clients.
Add tests covering: - Calling a method that runs on the event loop thread, which receives a token that is never stopped. - Requesting stop on one call while another call from a different client thread is in flight. - Requesting stop from another thread while calls complete. - Disconnecting during a call, which requests stop on the server. - Dropping a call queued behind another call on the same server thread. Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
b9fe90d to
85cdc9d
Compare
Currently, libmultiprocess requests block and can't be canceled from either the client or server side. Downstream code works around this by pairing each blocking method with a dedicated
interruptX()method whose only purpose is to wake it up. For example, bitcoin/bitcoin#33676 introducedBlockTemplate::interruptWait()specifically to wake an in-progressBlockTemplate::waitNext()call.Cap'n Proto provides a useful cancellation mechanism that libmultiprocess can use to support cancellation from both non-libmultiprocess and libmultiprocess clients. When a promise is dropped, it sends a cancellation request. The server may either cancel immediately or ignore the signal.
This PR implements approach 4 from bitcoin/bitcoin#33575, adding cancellation support on both sides:
ProxyClientmethod calls to be canceled from another thread.ClientInvokeContextprovides aregister_cancelhook that a cancelable parameter type can set.clientInvokecalls it with a function that cancels the request. If that function is called before the call returns,clientInvokecancels the request and throws. Parameter types are marked cancelable with theIsCancelParamtrait, so methods without a cancelable parameter don't compile any cancellation code, and a method can have at most one.ProxyServerto register a callback that runs when the request is canceled, whether because the promise was dropped or the connection was interrupted.CustomReadExtraParamoverloads can set it throughServerInvokeContext::cancel_fn.The PR also adds a new capnp annotation,
$Proxy.extraParam, which declares C++-only parameters that are not sent through RPC. These parameters are handled byCustomBuildExtraParamon the client side andCustomReadExtraParamon the server side. Paired with the cancellation mechanism, this feature lets methods take astd::stop_token, supported by the newmp/type-stop-token.hheader. Applications can also define their own cancel parameter types with the same hooks.For example, given the following capnp schema method:
and this C++ method:
The caller (client) passes a token, and requesting stop on its source from another thread cancels the call:
The callee (server) registers a stop callback that interrupts its wait. The callback runs when the request is canceled:
mp/type-stop-token.his empty when the standard library doesn't providestd::stop_token. For example, libc++ 19, the FreeBSD base-system library, only provides it with-fexperimental-library. There, applications can use their own token type with the same hooks. The tests do this with a stub intest/mp/test/foo.handtest/mp/test/foo-types.h.Detecting a dropped promise on the server requires the
$Cxx.allowCancellationannotation on the method, file, or interface. Without it, Cap'n Proto runs the abandoned call to completion. The annotation requires Cap'n Proto 1.0 (see the "Breaking change" section in https://capnproto.org/news/2023-07-28-capnproto-1.0.html), which this PR also sets as the minimum supported version.NOTE: bitcoin/bitcoin#36097 builds on this PR by adding cancel arguments to the blocking mining methods (
waitTipChanged,createNewBlock,BlockTemplate::waitNext) and deprecating theinterrupt()/interruptWait()workarounds.