Skip to content

proxy-server: Reject destroy() during active calls and calls after destroy() - #382

Merged
ryanofsky merged 1 commit into
bitcoin-core:masterfrom
xyzconstant:2026-10-19-fix-issues-around-destroy
Nov 10, 2026
Merged

ryanofsky merged 1 commit into
bitcoin-core:masterfrom
xyzconstant:2026-10-19-fix-issues-around-destroy

Conversation

@xyzconstant

Copy link
Copy Markdown
Contributor

Fixes #378

There are 2 issues with non-libmultiprocess clients:

  1. After a destroy(), any request can still be sent as long as the capability is not dropped. Such request dereferenced a null m_impl pointer (reset by the previous destroy() call) and crashed the server (also reported in ipc: AddressSanitizer: SEGV on unknown address bitcoin/bitcoin#36502).
  2. Sending destroy() while another request on the same object was running could free m_impl while that request was still using it.

The libmultiprocess C++ client cannot trigger either case because it sends destroy() only from the ProxyClient destructor and releases the capability after that.

This PR implements the fixes suggested in the issue.

  • serverInvoke checks m_destroyed and rejects requests received after destroy() with "method called after destroy".
  • invokeDestroy checks m_active_calls and rejects destroy() with "destroy called while other calls are in progress" when another request on the object is being executed.

Differences from the suggestion:

  • No mutex is needed because m_active_calls and m_destroyed are accessed only from the event-loop thread.
  • Only requests with an mp.Context are counted in m_active_calls. Requests without one run to completion on the event-loop thread, so destroy() 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.

@DrahtBot

DrahtBot commented Oct 25, 2026 •

Copy link
Copy Markdown

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK ryanofsky

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #390 (proxy: Rethrow EventLoop::sync() callback exceptions in the calling thread by ryanofsky)

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.

@xyzconstant
xyzconstant force-pushed the 2026-10-19-fix-issues-around-destroy branch from 3b890c4 to b17f79c Compare October 26, 2026 16:24
@xyzconstant

xyzconstant commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor Author

#381 fixes the OpenBSD job failure. I'm working on a fix for the gnu32 one.

EDIT: Opened #383 to fix the gnu32 failure. It makes shell.nix compile capnproto with GCC too when an i686 platform is detected.

@xyzconstant
xyzconstant force-pushed the 2026-10-19-fix-issues-around-destroy branch 2 times, most recently from edd1b3b to 0e96139 Compare October 28, 2026 01:05
ryanofsky added a commit that referenced this pull request Nov 3, 2026
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 ryanofsky left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread include/mp/type-context.h
// scope when it happens.
cancel_monitor.m_on_cancel = nullptr;
auto self_dispose{kj::mv(self)};
auto active_call_dispose{kj::mv(active_call)};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread test/mp/test/test.cpp Resolved
Comment thread doc/design.md Resolved
@xyzconstant
xyzconstant force-pushed the 2026-10-19-fix-issues-around-destroy branch from 0e96139 to b8a6215 Compare November 10, 2026 17:38
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
@xyzconstant
xyzconstant force-pushed the 2026-10-19-fix-issues-around-destroy branch from b8a6215 to 42fa11e Compare November 10, 2026 17:51
@xyzconstant

Copy link
Copy Markdown
Contributor Author

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.

@ryanofsky ryanofsky left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK 42fa11e

Thanks for the updates! Just suggested changes since last review

@ryanofsky
ryanofsky merged commit e244e29 into bitcoin-core:master Nov 10, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling an IPC object after or during destroy() makes the server use a null or freed implementation

3 participants