From b627aebcb9ff57a5be4dbb7e6b502229a4d2ec24 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Mon, 23 Aug 2021 09:10:43 +0200 Subject: [PATCH 1/2] p2p: fixed data races --- p2p/dial.go | 3 ++- p2p/enode/iter_test.go | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/p2p/dial.go b/p2p/dial.go index 83ced3cb32808..60fafcc503c16 100644 --- a/p2p/dial.go +++ b/p2p/dial.go @@ -25,6 +25,7 @@ import ( mrand "math/rand" "net" "sync" + "sync/atomic" "time" "github.com/ethereum/go-ethereum/common/mclock" @@ -259,7 +260,7 @@ loop: d.dialPeers++ } id := c.node.ID() - d.peers[id] = c.flags + d.peers[id] = connFlag(atomic.LoadInt32((*int32)(&c.flags))) // Remove from static pool because the node is now connected. task := d.static[id] if task != nil && task.staticPoolIndex >= 0 { diff --git a/p2p/enode/iter_test.go b/p2p/enode/iter_test.go index 6009661f3ce6f..5014346af465f 100644 --- a/p2p/enode/iter_test.go +++ b/p2p/enode/iter_test.go @@ -268,7 +268,7 @@ func (s *genIter) Node() *Node { } func (s *genIter) Close() { - s.index = ^uint32(0) + atomic.StoreUint32(&s.index, ^uint32(0)) } func testNode(id, seq uint64) *Node { From 2de02d87b6f7cd0d76231466f5e6dc8219e8b758 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 24 Aug 2021 11:08:58 +0200 Subject: [PATCH 2/2] p2p: don't store flags in d.peers --- p2p/dial.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/p2p/dial.go b/p2p/dial.go index 60fafcc503c16..0d70e6f4a33b8 100644 --- a/p2p/dial.go +++ b/p2p/dial.go @@ -25,7 +25,6 @@ import ( mrand "math/rand" "net" "sync" - "sync/atomic" "time" "github.com/ethereum/go-ethereum/common/mclock" @@ -108,7 +107,7 @@ type dialScheduler struct { // Everything below here belongs to loop and // should only be accessed by code on the loop goroutine. dialing map[enode.ID]*dialTask // active tasks - peers map[enode.ID]connFlag // all connected peers + peers map[enode.ID]struct{} // all connected peers dialPeers int // current number of dialed peers // The static map tracks all static dial tasks. The subset of usable static dial tasks @@ -167,7 +166,7 @@ func newDialScheduler(config dialConfig, it enode.Iterator, setupFunc dialSetupF setupFunc: setupFunc, dialing: make(map[enode.ID]*dialTask), static: make(map[enode.ID]*dialTask), - peers: make(map[enode.ID]connFlag), + peers: make(map[enode.ID]struct{}), doneCh: make(chan *dialTask), nodesIn: make(chan *enode.Node), addStaticCh: make(chan *enode.Node), @@ -260,7 +259,7 @@ loop: d.dialPeers++ } id := c.node.ID() - d.peers[id] = connFlag(atomic.LoadInt32((*int32)(&c.flags))) + d.peers[id] = struct{}{} // Remove from static pool because the node is now connected. task := d.static[id] if task != nil && task.staticPoolIndex >= 0 {