Use-after-free race in `ConnectStream` when passing a disconnected socket #308

issue xyzconstant opened this issue on July 14, 2026
  1. xyzconstant commented at 3:58 PM on July 14, 2026: contributor

    While adding coverage for ConnectStream in #298, the CI job for NetBSD 9.4 was crashing in the new test "Passing a disconnected socket will throw" with:

    [ TEST ] connect_tests.cpp:94: Passing a disconnected socket will throw
    assertion "m_loop" failed: file "/home/runner/work/libmultiprocess/libmultiprocess/include/mp/proxy.h", line 59, function "mp::EventLoop& mp::EventLoopRef::operator*() const"
    *** Received signal [#6](/bitcoin-core-multiprocess/6/): Abort trap
    stack: 
    

    The race occurs on all platforms, the NetBSD runner is just slow enough that the event loop thread always wins. Asked Claude to try to reproduce it locally and came up with placing (in ConnectStream)

    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    

    between the loop.sync(...) block and the client construction to extend the race window so that the event loop thread wins the race (as it consistently did in the failing job), thereby exposing the issue.

    The sequence of events is as follows:

    1. ConnectStream runs a loop.sync(...) block (which executes the body on the event loop thread) that creates the Connection, obtains init_client, and registers an onDisconnect handler that runs delete connection_ptr. At this point, the ProxyClient instance that will own the connection does not yet exist.
    2. The RPC system hits EOF on its first read of the disconnected socket.
    3. The event loop then resolves m_network.onDisconnect(), running the handler from step 1, resulting in the Connection being deleted.
    4. Connection::~Connection resets its EventLoopRef m_loop member, and the memory is freed.
    5. The caller thread then wakes up and constructs the ProxyClient<InitInterface>, whose ProxyContext constructor reads the freed Connection: https://github.com/bitcoin-core/libmultiprocess/blob/8412fcdc659e1379f9b4dea896c26bc1c5f3afa8/src/mp/proxy.cpp#L82

    Steps 2-4 run on the event loop thread, while step 5 runs on the caller thread, with no ordering between them so an early disconnect lets the deletion win.

    The fix is already available in #298, I'm opening this issue to make it more visible and easier to review.

  2. xyzconstant referenced this in commit 7cd90aec9a on Jul 15, 2026
  3. xyzconstant referenced this in commit fecf305c2f on Jul 15, 2026
  4. xyzconstant referenced this in commit 9b93947bd0 on Jul 15, 2026
  5. xyzconstant referenced this in commit 82f7ef2c0c on Jul 15, 2026
  6. xyzconstant referenced this in commit 9078674779 on Jul 15, 2026
  7. xyzconstant referenced this in commit a2e00075e5 on Jul 21, 2026
  8. xyzconstant referenced this in commit e4530f8048 on Jul 21, 2026
  9. xyzconstant referenced this in commit f3355b5e50 on Jul 21, 2026
  10. xyzconstant referenced this in commit a8b64c203c on Aug 4, 2026
  11. xyzconstant referenced this in commit c435ef7dc5 on Aug 4, 2026
  12. xyzconstant referenced this in commit 8fb3b5bf0d on Aug 4, 2026
  13. xyzconstant referenced this in commit bb473690c9 on Aug 5, 2026
  14. ryanofsky closed this on Aug 11, 2026

  15. ryanofsky referenced this in commit 2220df68c9 on Aug 11, 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-08-26 00:30 UTC

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