diff --git a/core/network/impl/peer_manager_impl.cpp b/core/network/impl/peer_manager_impl.cpp index 6375b6318f..9eb0fa168d 100644 --- a/core/network/impl/peer_manager_impl.cpp +++ b/core/network/impl/peer_manager_impl.cpp @@ -5,6 +5,7 @@ #include "network/impl/peer_manager_impl.hpp" +#include #include #include @@ -60,7 +61,8 @@ namespace kagome::network { storage_{std::move(storage)}, hasher_{std::move(hasher)}, reputation_repository_{std::move(reputation_repository)}, - log_(log::createLogger("PeerManager", "network")) { + log_(log::createLogger("PeerManager", "network")), + entry_counter_{0ull} { BOOST_ASSERT(app_state_manager_ != nullptr); BOOST_ASSERT(identify_ != nullptr); BOOST_ASSERT(kademlia_ != nullptr); @@ -402,12 +404,45 @@ namespace kagome::network { kTimeoutForConnecting); } - void PeerManagerImpl::disconnectFromPeer(PeerId peer_id) { + inline std::string createBT() { + static constexpr size_t kStackSize = 30ull; + void *a[kStackSize]; + auto const count = backtrace(a, kStackSize); + + /// TODO (iceseer): it's a test code and we doesnt care about allocations + char **names = backtrace_symbols(a, count); + auto cleaner = gsl::finally([names] { free(names); }); + + std::string data; + data.reserve(64 * kStackSize); + for (auto ix = 0; ix < count; ++ix) { + data += names[ix]; + data += '\n'; + } + + return data; + } + + void PeerManagerImpl::disconnectFromPeer(const PeerId &peer_id) { if (peer_id == own_peer_info_.id) { return; } - SL_DEBUG(log_, "Disconnect from peer {}", peer_id); + ++entry_counter_; + auto locker = gsl::finally([&] { --entry_counter_; }); + auto entry_counter = entry_counter_.load(); + SL_INFO(log_, + "Disconnect from peer {}, entry {}, thread {}", + peer_id, + entry_counter, + std::this_thread::get_id()); + if (entry_counter > 1) { + SL_INFO(log_, + "Found double {} entry: the second one came from\n{}", + __func__, + createBT()); + } + auto it = active_peers_.find(peer_id); host_.disconnect(peer_id); if (it != active_peers_.end()) { diff --git a/core/network/impl/peer_manager_impl.hpp b/core/network/impl/peer_manager_impl.hpp index a03a5d308b..a428c528e5 100644 --- a/core/network/impl/peer_manager_impl.hpp +++ b/core/network/impl/peer_manager_impl.hpp @@ -151,7 +151,7 @@ namespace kagome::network { void connectToPeer(const PeerId &peer_id); /// Closes all streams of provided peer - void disconnectFromPeer(PeerId peer_id); + void disconnectFromPeer(const PeerId &peer_id); std::vector loadLastActivePeers(); @@ -194,6 +194,7 @@ namespace kagome::network { ParachainState parachain_state_; log::Logger log_; + std::atomic entry_counter_; }; } // namespace kagome::network