1 minor finding · 1 suggestion
Findings
[minor] · loopRef() can call done() with a disengaged async queue
The new guard can dereference a disengaged optional during startup or teardown; this incompletely fixes pre-existing lifetime races, and no current caller producing these overlaps was identified.
Location: src/ipc/capnp/protocol.cpp:185 (head).
Details and suggested fix
EventLoop::done() evaluates m_num_refs == 0 && m_async_fns->empty(), but the queue is engaged only while loop() runs. After serve() returns from loop() and before it resets m_loop, a non-request thread can acquire both locks and reach this check with zero references and no queue. That dereferences a disengaged std::optional, causing undefined behavior rather than the intended ipc::Exception; the catch in disconnectIncoming() cannot recover from it. A concurrent startup caller can reach the same state because startLoop() publishes m_loop before constructing m_loop_ref. Normal setup remains safe because positive reference counts short-circuit the queue access.
Make done() tolerate a disengaged queue, for example with m_num_refs == 0 && (!m_async_fns || m_async_fns->empty()), and construct m_loop_ref in the same critical section that publishes m_loop. This preserves legitimate pre-loop callbacks while closing the zero-reference publication window.
Optional improvements
[suggestion] · Tests miss operation lifetime across parent disconnect
Existing tests do not check that a per-operation loopRef() keeps the serve-mode loop alive after the parent disconnects.
Location: src/ipc/capnp/protocol.cpp:86 (head).
Details and suggested fix
IpcSocketPairTest finishes connect() and echo() before releasing remote_init. The socket-listening tests retain the background loop's m_loop_ref, so they cannot isolate the new temporary references either. Add a deterministic serve-mode test that pauses a non-request operation immediately after acquiring its reference, disconnects the parent, and verifies that the loop remains running until the operation releases that reference, then exits. A small test hook may be needed. Pause before entering sync(), which currently takes its own reference, and avoid retaining other client or connection references that would mask the lifetime being tested.