Repository navigation
proxy-server: Reject destroy() during active calls and calls after destroy() - #382
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. |
3b890c4 to
b17f79c
Compare
edd1b3b to
0e96139
Compare
04a0465 ci: Compile capnproto with GCC in gnu32 job (xyzconstant) Pull request description: The gnu32 job [segfaulted](https://github.com/bitcoin-core/libmultiprocess/actions/runs/40358769633/job/121698239990?pr=382) in #382 because of an i686 ABI mismatch between Clang-compiled libkj (the nixpkgs default for capnproto) and GCC-compiled libmultiprocess. Clang and GCC handle empty struct arguments differently on i686: Clang omits them, while GCC reserves a stack slot. When the `fn.invoke()` call in the `PassField` overload for `Proxy.Context` [fails with an uncaught exception](https://github.com/bitcoin-core/libmultiprocess/blob/2e7812a/include/mp/type-context.h#L203), `kj::str(exception)` calls `kj::operator*(kj::_::Stringifier, const kj::Exception&)` with `Stringifier` as an empty struct. As a result, the Clang-compiled callee reads `Exception&` from the stack slot reserved by the GCC-compiled caller for `Stringifier`, thereby causing an invalid read. This PR builds capnproto with GCC in the gnu32 job, so both sides agree on the layout. The same crash happened in #218, where it [was worked around with `getDescription()`](#218 (comment)). ACKs for top commit: Sjors: ACK 04a0465 ryanofsky: Code review ACK 04a0465. Nice find. I was very confused by these crashes previously and glad cause could be pinned down. Would be good as a followup to revert the workaround from #218 and log the full exception instead of just the description field. Tree-SHA512: 44e83b86d00d3e5dc50f03e4eda51f161a5e48f15f744be7c13fd00e10f41cce37e789fd339f2fa990bec4ca0ec7cfea2fe28cd0886cc575bcf2f0ecafde93dc
ryanofsky
left a comment
There was a problem hiding this comment.
Code review ACK 0e96139. This is a nice straightforward fix for #378, and much simpler than I was expecting.
Both differences from the suggestion in the issue look right to me (#382 (comment)). The main suggestion is a test check that destroy succeeds once the blocked call has finished (#382 (comment)). The gnu32 and OpenBSD CI failures are fixed on master by #381 and #383, so I'm trying to rerun the CI jobs now.
| // scope when it happens. | ||
| cancel_monitor.m_on_cancel = nullptr; | ||
| auto self_dispose{kj::mv(self)}; | ||
| auto active_call_dispose{kj::mv(active_call)}; |
There was a problem hiding this comment.
In commit "proxy: Reject destroy() during active calls and calls after destroy()" (0e96139)
Note: I agree with both differences from the fix suggested in #378. Counting only mp.Context requests is enough, because a request without one runs entirely inside serverInvoke on the event loop thread, results included, so a destroy can't run in the middle of it. A mutex isn't needed because the counter is only incremented, decremented and checked on the event loop thread.
Decrementing here, in the same sync() call that drops self, also does what I wanted from decrementing "when the request's promise settles": it happens after fn.invoke() has filled in the results and before the response is sent, so a client that has received the response to a call can always destroy the object afterwards.
0e96139 to
b8a6215
Compare
A non-libmultiprocess client can send requests after destroy(), causing a null m_impl pointer to be dereferenced and triggering a SIGSEGV. It can also call destroy() while another request on the same object is active, freeing m_impl while that request is using it. Track requests queued or executing on worker threads with m_active_calls. Reject destroy() while other requests are active, and reject any requests received after destruction. Fixes bitcoin-core#378
b8a6215 to
42fa11e
Compare
|
Thanks for the review @ryanofsky, I just rebased to master, so the fixes for the failing jobs take effect. And updated the code with your inline suggestions. |
Fixes #378
There are 2 issues with non-libmultiprocess clients:
destroy(), any request can still be sent as long as the capability is not dropped. Such request dereferenced a nullm_implpointer (reset by the previousdestroy()call) and crashed the server (also reported in ipc: AddressSanitizer: SEGV on unknown address bitcoin/bitcoin#36502).destroy()while another request on the same object was running could freem_implwhile that request was still using it.The libmultiprocess C++ client cannot trigger either case because it sends
destroy()only from theProxyClientdestructor and releases the capability after that.This PR implements the fixes suggested in the issue.
serverInvokechecksm_destroyedand rejects requests received afterdestroy()with "method called after destroy".invokeDestroychecksm_active_callsand rejectsdestroy()with "destroy called while other calls are in progress" when another request on the object is being executed.Differences from the suggestion:
m_active_callsandm_destroyedare accessed only from the event-loop thread.mp.Contextare counted inm_active_calls. Requests without one run to completion on the event-loop thread, sodestroy()cannot run during their execution.Also added a regression test that manually builds and sends requests with Cap'n Proto, reproducing the behavior of a buggy non-libmultiprocess client.