From 5bd6c1e5cc3e2f1b769e2583110313d7cb974f04 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 23 Dec 2020 16:13:57 +0300 Subject: [PATCH] network: fix a bug in discovery with a peer connected twice It could be the case that checks are performed simultaneosly and peers connections goes down from 2 to 0. We must take such case into account and register address as good in discovery. --- pkg/network/server.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/pkg/network/server.go b/pkg/network/server.go index caf464b56..b8befac11 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -293,7 +293,28 @@ func (s *Server) run() { addr := drop.peer.PeerAddr().String() if drop.reason == errIdenticalID { s.discovery.RegisterBadAddr(addr) - } else if drop.reason != errAlreadyConnected { + } else if drop.reason == errAlreadyConnected { + // There is a race condition when peer can be disconnected twice for the this reason + // which can lead to no connections to peer at all. Here we check for such a possibility. + stillConnected := false + s.lock.RLock() + verDrop := drop.peer.Version() + addr := drop.peer.PeerAddr().String() + if verDrop != nil { + for peer := range s.peers { + ver := peer.Version() + // Already connected, drop this connection. + if ver != nil && ver.Nonce == verDrop.Nonce && peer.PeerAddr().String() == addr { + stillConnected = true + } + } + } + s.lock.RUnlock() + if !stillConnected { + s.discovery.UnregisterConnectedAddr(addr) + s.discovery.BackFill(addr) + } + } else { s.discovery.UnregisterConnectedAddr(addr) s.discovery.BackFill(addr) }