From 5cbd1f85bf217f59d3465ff8a471883600795ea0 Mon Sep 17 00:00:00 2001 From: Jonathan Gillham Date: Wed, 10 Feb 2016 14:23:36 +0000 Subject: [PATCH] peer: Remove potential race when calling Connect. --- peer/peer.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/peer/peer.go b/peer/peer.go index 259371f1..7db2b586 100644 --- a/peer/peer.go +++ b/peer/peer.go @@ -1953,7 +1953,7 @@ func (p *Peer) QueueInventory(invVect *wire.InvVect) { // the peer is already connected will have no effect. func (p *Peer) Connect(conn net.Conn) error { // Already connected? - if atomic.LoadInt32(&p.connected) != 0 { + if !atomic.CompareAndSwapInt32(&p.connected, 0, 1) { return nil } @@ -1963,7 +1963,6 @@ func (p *Peer) Connect(conn net.Conn) error { p.conn = conn p.timeConnected = time.Now() - atomic.AddInt32(&p.connected, 1) return p.start() }