Skip to content

Commit

Permalink
p2p, p2p/enode: fix data races (ethereum#23434)
Browse files Browse the repository at this point in the history
In p2p/dial.go, conn.flags was accessed without using sync/atomic.
This race is fixed by removing the access.

In p2p/enode/iter_test.go, a similar race is resolved by writing the field atomically.

Co-authored-by: Felix Lange <fjl@twurst.com>
  • Loading branch information
2 people authored and zzyalbert committed Nov 26, 2021
1 parent 115a232 commit 63461d8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions p2p/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,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
Expand Down Expand Up @@ -166,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),
Expand Down Expand Up @@ -259,7 +259,7 @@ loop:
d.dialPeers++
}
id := c.node.ID()
d.peers[id] = 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 {
Expand Down
2 changes: 1 addition & 1 deletion p2p/enode/iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 63461d8

Please sign in to comment.