ipc: hold event loop references when using it in `CapnpProtocol` by xyzconstant · Pull Request #36572 · bitcoin/bitcoin · GitHub
Skip to content

ipc: hold event loop references when using it in CapnpProtocol - #36572

Open
xyzconstant wants to merge 1 commit into
bitcoin:masterfrom
xyzconstant:2026-10-16-fix-capnp-protocol-races
Open

xyzconstant wants to merge 1 commit into
bitcoin:masterfrom
xyzconstant:2026-10-16-fix-capnp-protocol-races

Conversation

@xyzconstant

@xyzconstant xyzconstant commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

EventLoop::loop() exits when its reference count reaches zero. In serve() mode, the parent connection is the only reference keeping the loop running. Other CapnpProtocol methods called from threads not handling parent IPC requests do not hold their own references and therefore depend on the parent connection remaining alive.

Add loopRef() (code suggested by @ryanofsky in bitcoin-core/libmultiprocess#376 (comment)), which returns a new reference to the event loop or throws if the loop is not running or is shutting down, and use it in the methods that need the loop. Protect m_loop with a new m_loop_mutex to prevent serve() from destroying the loop while loopRef() is accessing it.

EventLoop::loop() exits when its reference count reaches zero. In serve()
mode, the parent connection is the only reference keeping the loop running.
Other CapnpProtocol methods called from threads not handling parent IPC
requests do not hold their own references and therefore depend on the
parent connection remaining alive.

Add loopRef(), which returns a new reference to the event loop or throws
if the loop is not running or is shutting down, and use it in the methods
that need the loop. Protect m_loop with a new m_loop_mutex to prevent
serve() from destroying the loop while loopRef() is accessing it.

Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
@DrahtBot

DrahtBot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Comment on lines +80 to 81
assert(WITH_LOCK(m_loop_mutex, return !m_loop));
};

@Husteem Husteem Oct 20, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A tiny Nit: redundant trailing semicolon.

@Hamza1610

Hamza1610 commented Oct 20, 2026 •

Copy link
Copy Markdown

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.

4 participants