1 minor finding · 2 suggestions
Findings
[minor] · Liveness queries can destroy callbacks under the signal mutex
A liveness query racing with disconnect() can release the last callback reference under the signal mutex, deadlocking if a capture destructor re-enters that signal.
Location: src/util/btcsignals.h:113 (head).
Details and suggested fix
connected() now creates an owning shared_ptr temporary through load() != nullptr. If another thread clears m_callback and returns from disconnect() before that temporary is released, the query destroys the callback and its captures. Both empty() and the garbage-collection predicate in connect() call connected() while holding the non-recursive m_mutex. A capture destructor that re-enters the same signal through connect(), empty() or emission can therefore deadlock or trip lock debugging; waiting for another thread that needs the mutex can also deadlock. This introduces a destruction point beyond disconnect and in-flight invocation completion. Base connect() garbage collection already allowed destruction under the lock, but base liveness queries did not own callbacks, making empty() and public connection::connected() new teardown paths. Keep a separate atomic liveness flag, clear it before clearing m_callback, and let queries read only that flag while emissions retain callback ownership. Add a regression test with a re-entering capture destructor. No current caller whose capture destructor re-enters its signal or waits on its mutex was identified, so production impact is unconfirmed.
Optional improvements
[suggestion] · The lifetime wording overlooks handles that outlive the signal
A still-connected plain connection handle can keep a callback alive after signal destruction, contrary to the new header text and test comment.
Location: src/util/btcsignals.h:30 (head).
Details and suggested fix
connect() returns shared ownership of the connection_holder, whose liveness state owns the callback. The defaulted signal destructor only releases the vector's references; it does not disconnect surviving holders. A still-connected plain connection that outlives the signal therefore retains the callback and its captures until it is disconnected or the last handle is released. The first callback_destruction case discards the returned handle, so its assertions do not cover this lifetime. Qualify the header and test comments to say that never-disconnected callbacks are destroyed when the signal and all referring handles have been destroyed, and add a case retaining a handle beyond the signal's scope. This retention predates the PR; making signal teardown disconnect surviving holders would be a separate API behavior change.
[suggestion] · Caller-lifetime guidance omits in-flight calls after disconnect()
Disconnecting before destroying referenced objects is necessary but insufficient when a concurrent callback is still using them.
Location: src/util/btcsignals.h:25 (head).
Details and suggested fix
disconnect() clears the stored callback pointer without waiting for an emission that already loaded it. The concurrent_disconnect_destruction test explicitly relies on the callback continuing after disconnection. Shared ownership keeps the callback object and its owned state alive, but it does not extend the lifetime of objects captured by reference or through a raw this pointer. Extend the new caller guidance to state that disconnect() is not a quiescence barrier: callers must also ensure that no invocation can still access referenced state before destroying it, using their own synchronization where necessary. This clarifies pre-existing behavior rather than requesting a synchronization change.