Merge bitcoin/bitcoin#36011: btcsignals: destroy callbacks on disconnect
What changed, and why it matters
This change fixes a bug in Bitcoin Core's internal signal/callback system where callback functions and the resources they own were not released when disconnected. Previously, disconnected callbacks lingered until the signal itself was destroyed or new connections triggered garbage collection. This caused a real-world deadlock during GUI shutdown because node-to-GUI callbacks held references that prevented the node process from exiting. The patch makes callbacks get destroyed immediately on disconnect (or after any in-flight call finishes), matching the behavior of the older Boost signals library.
Apply the patch. It is a defensive correctness fix that prevents resource leaks and shutdown deadlocks. No immediate incident response is required beyond normal merge/verification, but downstream users running GUI builds should update to include this fix.
Security signals we found
Resource lifetime bug in custom signal/slot implementation
Deadlock on GUI shutdown due to unreleased callback-owned references
Behavioral divergence from boost::signals2 callback lifetime semantics
Thread-safe callback destruction with concurrent invocation handling
Evidence from the diff
The patch modifies src/util/btcsignals.h, a lightweight replacement for boost::signals2. It changes the connection liveness object from a simple atomic bool to an atomic shared_ptr to a type-erased callback. disconnect() now nulls this shared_ptr, destroying the callback object. During signal emission, the code holds a shared_ptr to the callback for the duration of the call, ensuring callbacks disconnected concurrently remain valid until the call returns. A fallback atomic_shared_ptr implementation is provided for standard libraries lacking C++20 std::atomic
Changed components
src/util/btcsignals.hsrc/test/btcsignals_tests.cppBitcoin Core GUI shutdown pathNode-to-GUI signal callbacks (e.g., InitMessage)Inspect captured patch +140 / −13
### src/test/btcsignals_tests.cpp
@@ -7,7 +7,10 @@
#include <boost/test/unit_test.hpp>
+#include <future>
+#include <memory>
#include <semaphore>
+#include <thread>
namespace {
@@ -81,6 +84,78 @@ BOOST_AUTO_TEST_CASE(disconnects)
BOOST_CHECK_EQUAL(val, 6);
}
+/* Characterize when a callback object and the state it owns are destroyed:
+ * eagerly on disconnect(), or with the signal if never disconnected.
+ * Connection handles do not keep the callback alive.
+ */
+BOOST_AUTO_TEST_CASE(callback_destruction)
+{
+ // With no disconnect, callbacks are destroyed with the signal.
+ std::weak_ptr<int> weak0;
+ {
+ btcsignals::signal<void()> sig0;
+ auto state0{std::make_shared<int>(0)};
+ weak0 = state0;
+ sig0.connect([state0 = std::move(state0)] {});
+ BOOST_CHECK(!weak0.expired());
+ }
+ BOOST_CHECK(weak0.expired());
+
+ // disconnect() destroys the callback immediately, even while a connection
+ // handle is still held.
+ btcsignals::signal<void()> sig1;
+ auto state1{std::make_shared<int>(0)};
+ std::weak_ptr<int> weak1{state1};
+ auto conn1{sig1.connect([state1 = std::move(state1)] {})};
+ BOOST_CHECK(!weak1.expired());
+ conn1.disconnect();
+ BOOST_CHECK(weak1.expired());
+}
+
+/* A callback that disconnects itself during emission remains valid until its
+ * invocation returns, and is destroyed as soon as it does.
+ */
+BOOST_AUTO_TEST_CASE(self_disconnect_destruction)
+{
+ btcsignals::signal<void()> sig0;
+ auto state0{std::make_shared<int>(0)};
+ std::weak_ptr<int> weak0{state0};
+ btcsignals::connection conn0;
+ conn0 = sig0.connect([&conn0, weak0, state0 = std::move(state0)] {
+ conn0.disconnect();
+ // The callback and the state it owns stay valid while it executes.
+ BOOST_CHECK(!weak0.expired());
+ *state0 += 1;
+ });
+ sig0();
+ BOOST_CHECK(weak0.expired());
+}
+
+/* A callback disconnected on another thread while it is running stays valid
+ * until the call returns, and is destroyed when it does.
+ */
+BOOST_AUTO_TEST_CASE(concurrent_disconnect_destruction)
+{
+ btcsignals::signal<void()> sig0;
+ auto state0{std::make_shared<int>(0)};
+ std::weak_ptr<int> weak0{state0};
+ std::promise<void> entered;
+ std::promise<void> release;
+ auto conn0{sig0.connect([&entered, release_future = release.get_future().share(), state0 = std::move(state0)] {
+ entered.set_value();
+ release_future.wait();
+ *state0 += 1;
+ })};
+ std::thread thread{[&sig0] { sig0(); }};
+ entered.get_future().wait();
+ conn0.disconnect();
+ BOOST_CHECK(!conn0.connected());
+ BOOST_CHECK(!weak0.expired());
+ release.set_value();
+ thread.join();
+ BOOST_CHECK(weak0.expired());
+}
+
BOOST_AUTO_TEST_CASE(any_of_combiner)
{
btcsignals::signal<bool(), btcsignals::any_of> sig0;
### src/util/btcsignals.h
@@ -21,8 +21,14 @@
* It is api-compatible with a minimal subset of boost::signals2.
*
* Rather than using a custom slot type, and the features/complexity that they
- * imply, std::function is used to store the callbacks. Lifetime management of
- * the callbacks is left up to the user.
+ * imply, std::function is used to store the callbacks. There is no automatic
+ * tracking of objects the callbacks refer to, so callers need to disconnect
+ * callbacks before those objects are destroyed.
+ *
+ * Like boost::signals2, disconnecting a connection destroys its callback
+ * object, or if the callback is running at the time, destroys it when the
+ * call returns. Callbacks that are never disconnected are destroyed with the
+ * signal.
*
* All usage is thread-safe except for interacting with a connection while
* copying/moving it on another thread.
@@ -49,6 +55,31 @@ class any_of
template <typename Signature, typename Combiner = null_value>
class signal;
+namespace detail {
+#ifdef __cpp_lib_atomic_shared_ptr
+template <typename T>
+using atomic_shared_ptr = std::atomic<std::shared_ptr<T>>;
+#else
+//! Substitute for the C++20 std::atomic<std::shared_ptr<T>> specialization on
+//! standard libraries that do not provide it yet (feature-test macro
+//! __cpp_lib_atomic_shared_ptr), implemented with the C++11 atomic_load and
+//! atomic_store overloads for shared_ptr. Only the members used here are
+//! provided.
+template <typename T>
+class atomic_shared_ptr
+{
+ std::shared_ptr<T> m_ptr;
+
+public:
+ explicit atomic_shared_ptr(std::shared_ptr<T> ptr) noexcept : m_ptr{std::move(ptr)} {}
+ atomic_shared_ptr(const atomic_shared_ptr&) = delete;
+ atomic_shared_ptr& operator=(const atomic_shared_ptr&) = delete;
+ std::shared_ptr<T> load() const noexcept { return std::atomic_load(&m_ptr); }
+ void store(std::shared_ptr<T> ptr) noexcept { std::atomic_store(&m_ptr, std::move(ptr)); }
+};
+#endif
+} // namespace detail
+
/*
* State object representing the liveness of a registered callback.
* signal::connect() returns an enabled connection which can be held and
@@ -64,11 +95,22 @@ class connection
class liveness
{
friend class connection;
- std::atomic_bool m_connected{true};
- void disconnect() { m_connected.store(false); }
+ //! Callback object, type-erased so connection does not depend on the
+ //! signal type, and set to null by disconnect(). Signal invocation
+ //! holds a reference to it while calling it, so a callback
+ //! disconnected during a call is destroyed when the call returns.
+ detail::atomic_shared_ptr<const void> m_callback;
+
+ void disconnect() { m_callback.store(nullptr); }
+
+ protected:
+ explicit liveness(std::shared_ptr<const void> callback) : m_callback{std::move(callback)} {}
+
+ std::shared_ptr<const void> load() const { return m_callback.load(); }
+
public:
- bool connected() const { return m_connected.load(); }
+ bool connected() const { return load() != nullptr; }
};
/**
@@ -94,8 +136,10 @@ class connection
* If a connection is disabled as part of a signal's callback function, it
* will _not_ be executed in the current signal invocation.
*
- * Note that disconnected callbacks are not removed from their owning
- * signals here. They are garbage collected in signal::connect().
+ * The callback object is destroyed here (or when the last concurrently
+ * executing invocation of it completes). The empty holder entry is not
+ * removed from the owning signal; it is garbage collected in
+ * signal::connect().
*/
void disconnect()
{
@@ -157,11 +201,17 @@ class signal
*/
struct connection_holder : connection::liveness {
template <typename Callable>
- connection_holder(Callable&& callback) : m_callback{std::forward<Callable>(callback)}
+ explicit connection_holder(Callable&& callback)
+ : liveness{std::make_shared<const function_type>(std::forward<Callable>(callback))}
{
}
- const function_type m_callback;
+ //! Return the callback, or null if disconnected. The callback stays
+ //! alive while the returned pointer is held.
+ std::shared_ptr<const function_type> callback() const
+ {
+ return std::static_pointer_cast<const function_type>(load());
+ }
};
mutable Mutex m_mutex;
@@ -209,8 +259,10 @@ class signal
static_assert(std::is_same_v<result_type, typename function_type::result_type>,
"Callback result type must be equal to the combiner result type (void).");
for (const auto& connection : connections) {
- if (connection->connected()) {
- connection->m_callback(args...);
+ // Holding the callback pointer keeps the callback alive until
+ // the call returns, even if it is disconnected concurrently.
+ if (const auto callback{connection->callback()}) {
+ (*callback)(args...);
}
}
} else {
@@ -220,8 +272,8 @@ class signal
"Callback result type must be equal to the combiner result type (bool).");
result_type ret{false};
for (const auto& connection : connections) {
- if (connection->connected()) {
- ret |= connection->m_callback(args...);
+ if (const auto callback{connection->callback()}) {
+ ret |= (*callback)(args...);
}
}
return ret;Why this scored 29/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.