From d4363111b968a4c29131f413be03cc3315cd0eda Mon Sep 17 00:00:00 2001 From: selsta Date: Sat, 11 Jul 2026 16:22:25 +0200 Subject: [PATCH] epee: avoid bad_weak_ptr during connection teardown foreach_connection() can race with connection destruction while the protocol handler remains in the raw-pointer map. Use the non-throwing weak_from_this().lock() in add_ref() and treat expired connections as unavailable. This is an interim mitigation pending the broader connection lifetime fix in PR 7345. --- contrib/epee/include/net/abstract_tcp_server2.inl | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/contrib/epee/include/net/abstract_tcp_server2.inl b/contrib/epee/include/net/abstract_tcp_server2.inl index bdb70d9df..2317a6d01 100644 --- a/contrib/epee/include/net/abstract_tcp_server2.inl +++ b/contrib/epee/include/net/abstract_tcp_server2.inl @@ -1221,16 +1221,13 @@ namespace net_utils template bool connection::add_ref() { - try { - auto self = connection::shared_from_this(); - std::lock_guard guard(m_state.lock); - this->self = std::move(self); - ++m_state.protocol.reference_counter; - return true; - } - catch (boost::bad_weak_ptr &exception) { + auto self = connection::weak_from_this().lock(); + if (!self) return false; - } + std::lock_guard guard(m_state.lock); + this->self = std::move(self); + ++m_state.protocol.reference_counter; + return true; } template