mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-12 01:10:36 +00:00
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.
This commit is contained in:
parent
0d2bc8f4a6
commit
edf587bbf1
1 changed files with 22 additions and 1 deletions
|
@ -266,7 +266,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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue