Skip to content

Commit

Permalink
fix dirty data in peerset
Browse files Browse the repository at this point in the history
startInitPeer before PeerSet add the peer,
once mconnection start and Receive of one
Reactor faild, will try to remove it from PeerSet
while PeerSet still not contain the peer. Fix
this by change the order.
  • Loading branch information
unclezoro committed Feb 28, 2019
1 parent 853dd34 commit fd7cecb
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions p2p/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,21 +646,21 @@ func (sw *Switch) addPeer(p Peer) error {
// Handle the shut down case where the switch has stopped but we're
// concurrently trying to add a peer.
if sw.IsRunning() {
// All good. Start peer
// Add the peer to PeerSet.
// We add peer first, if start peer failed, need remove it.
// It should not err since we already checked peers.Has().
if err := sw.peers.Add(p); err != nil {
return err
}
// All good. Start peer, startInitPeer should not panic
if err := sw.startInitPeer(p); err != nil {
sw.peers.Remove(p)
return err
}
} else {
sw.Logger.Error("Won't start a peer - switch is not running", "peer", p)
}

// Add the peer to .peers.
// We start it first so that a peer in the list is safe to Stop.
// It should not err since we already checked peers.Has().
if err := sw.peers.Add(p); err != nil {
return err
}

sw.Logger.Info("Added peer", "peer", p)
sw.metrics.Peers.Add(float64(1))

Expand Down

0 comments on commit fd7cecb

Please sign in to comment.