Skip to content

Commit

Permalink
Backtrace double entry to disconnectPeer (#1368)
Browse files Browse the repository at this point in the history
Signed-off-by: iceseer <[email protected]>

Signed-off-by: iceseer <[email protected]>
  • Loading branch information
iceseer authored Oct 12, 2022
1 parent 7ee0503 commit ebd9715
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
41 changes: 38 additions & 3 deletions core/network/impl/peer_manager_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "network/impl/peer_manager_impl.hpp"

#include <execinfo.h>
#include <memory>

#include <libp2p/protocol/kademlia/impl/peer_routing_table.hpp>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()) {
Expand Down
3 changes: 2 additions & 1 deletion core/network/impl/peer_manager_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<scale::PeerInfoSerializable> loadLastActivePeers();

Expand Down Expand Up @@ -194,6 +194,7 @@ namespace kagome::network {
ParachainState parachain_state_;

log::Logger log_;
std::atomic<uint64_t> entry_counter_;
};

} // namespace kagome::network
Expand Down

0 comments on commit ebd9715

Please sign in to comment.