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

pull xyzconstant wants to merge 1 commits into bitcoin-core:master from xyzconstant:2026-10-19-fix-issues-around-destroy changing 6 files +149 −3
  1. xyzconstant commented at 8:43 AM on October 25, 2026: 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 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.

  2. DrahtBot commented at 8:44 AM on October 25, 2026: none

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--021abf342d371248e50ceaed478a90ca-->

    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 <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    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.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  3. xyzconstant force-pushed on Oct 26, 2026
  4. xyzconstant commented at 4:58 PM on October 27, 2026: contributor

    #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.

  5. xyzconstant force-pushed on Oct 27, 2026
  6. xyzconstant force-pushed on Oct 28, 2026
  7. ryanofsky referenced this in commit 81e5557804 on Nov 3, 2026
  8. in include/mp/type-context.h:181 in 0e96139da1 outdated
     177 | @@ -176,6 +178,7 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn&
     178 |                  // scope when it happens.
     179 |                  cancel_monitor.m_on_cancel = nullptr;
     180 |                  auto self_dispose{kj::mv(self)};
     181 | +                auto active_call_dispose{kj::mv(active_call)};
    


    ryanofsky commented at 5:21 PM on November 10, 2026:

    In commit "proxy: Reject destroy() during active calls and calls after destroy()" (0e96139da1c6b56fa8f73171bc982f5fd23d81da)

    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.

  9. in test/mp/test/test.cpp:868 in 0e96139da1 outdated
     863 | +            [&](kj::Exception&& e) { destroy_error.set_value(e.getDescription().cStr()); }));
     864 | +    });
     865 | +    KJ_EXPECT(destroy_error.get_future().get().find("destroy called while other calls are in progress") != std::string::npos);
     866 | +
     867 | +    value_release.set_value();
     868 | +    KJ_EXPECT(value.get_future().get() == 1);
    


    ryanofsky commented at 5:21 PM on November 10, 2026:

    In commit "proxy: Reject destroy() during active calls and calls after destroy()" (0e96139da1c6b56fa8f73171bc982f5fd23d81da)

    It would be good to check here that destroy succeeds once value() has returned. That checks the decrement: if m_active_calls weren't decremented after the value() call, or after the refused destroy above, every later destroy would fail, and the test would still pass as written. Something like:

    // Destroy succeeds after the value() call has finished.
    std::promise<std::string> destroy_result;
    loop.sync([&] {
        auto request{bar2->destroyRequest()};
        request.initContext();
        loop.m_task_set->add(request.send().then(
            [&](auto&&) { destroy_result.set_value(""); },
            [&](kj::Exception&& e) { destroy_result.set_value(e.getDescription().cStr()); }));
    });
    KJ_EXPECT(destroy_result.get_future().get() == "");
    

    xyzconstant commented at 5:53 PM on November 10, 2026:

    Thanks! done at 42fa11ef802fda0ee5b470fee2c16074054857b3

  10. in doc/design.md:137 in 0e96139da1 outdated
     133 | @@ -134,7 +134,7 @@ sequenceDiagram
     134 |      SF1->>serverInvoke: return
     135 |  ```
     136 |  
     137 | -Destroy methods are a special case: a capnp interface can declare a `destroy` method that is handled by `ServerDestroy` instead of `ServerCall`. Rather than dispatching through the `ServerField`/`ServerRet`/`ServerCall` chain to a C++ interface method, `ServerDestroy` calls `invokeDestroy()` on the `ProxyServer`, which resets `m_impl` and runs any registered cleanup functions, giving the client a way to synchronously destroy the wrapped object on the server side.
     138 | +Destroy methods are a special case: a capnp interface can declare a `destroy` method that is handled by `ServerDestroy` instead of `ServerCall`. Rather than dispatching through the `ServerField`/`ServerRet`/`ServerCall` chain to a C++ interface method, `ServerDestroy` calls `invokeDestroy()` on the `ProxyServer`, which resets `m_impl` and runs any registered cleanup functions, giving the client a way to synchronously destroy the wrapped object on the server side. The destroy request fails if other requests on the object are running, and requests sent after it fail. Clients that use Cap'n Proto directly should drop the capability instead.
    


    ryanofsky commented at 5:21 PM on November 10, 2026:

    In commit "proxy: Reject destroy() during active calls and calls after destroy()" (0e96139da1c6b56fa8f73171bc982f5fd23d81da)

    nit: Could say why dropping the capability is better, since that is what a client author needs to know to choose:

    Destroy methods are a special case: a capnp interface can declare a `destroy` method that is handled by `ServerDestroy` instead of `ServerCall`. Rather than dispatching through the `ServerField`/`ServerRet`/`ServerCall` chain to a C++ interface method, `ServerDestroy` calls `invokeDestroy()` on the `ProxyServer`, which resets `m_impl` and runs any registered cleanup functions, giving the client a way to synchronously destroy the wrapped object on the server side. The destroy request fails if other requests on the object are running or queued, and requests sent after it fail. Clients that use Cap'n Proto directly should normally drop the capability instead, which is safe even with requests in flight: the server frees the object after they finish.
    

    xyzconstant commented at 5:53 PM on November 10, 2026:

    Done at 42fa11ef802fda0ee5b470fee2c16074054857b3. Thanks.

  11. ryanofsky approved
  12. ryanofsky commented at 5:32 PM on November 10, 2026: collaborator

    Code review ACK 0e96139da1c6b56fa8f73171bc982f5fd23d81da. 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 (https://github.com/bitcoin-core/libmultiprocess/pull/382#discussion_r4528137969). The main suggestion is a test check that destroy succeeds once the blocked call has finished (https://github.com/bitcoin-core/libmultiprocess/pull/382#discussion_r4528138135). The gnu32 and OpenBSD CI failures are fixed on master by #381 and #383, so I'm trying to rerun the CI jobs now.

  13. xyzconstant force-pushed on Nov 10, 2026
  14. proxy: Reject destroy() during active calls and calls after destroy()
    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 #378
    42fa11ef80
  15. xyzconstant force-pushed on Nov 10, 2026
  16. xyzconstant commented at 5:52 PM on November 10, 2026: contributor

    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.

  17. ryanofsky approved
  18. ryanofsky commented at 6:32 PM on November 10, 2026: collaborator

    Code review ACK 42fa11ef802fda0ee5b470fee2c16074054857b3

    Thanks for the updates! Just suggested changes since last review

  19. ryanofsky merged this on Nov 10, 2026
  20. ryanofsky closed this on Nov 10, 2026


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/libmultiprocess. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-17 18:30 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me