Skip to content

Commit 8dbf261

Browse files
p2p, p2p/enode: fix data races (ethereum#23434)
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 <[email protected]>
1 parent 79bb930 commit 8dbf261

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

p2p/dial.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ type dialScheduler struct {
107107
// Everything below here belongs to loop and
108108
// should only be accessed by code on the loop goroutine.
109109
dialing map[enode.ID]*dialTask // active tasks
110-
peers map[enode.ID]connFlag // all connected peers
110+
peers map[enode.ID]struct{} // all connected peers
111111
dialPeers int // current number of dialed peers
112112

113113
// The static map tracks all static dial tasks. The subset of usable static dial tasks
@@ -166,7 +166,7 @@ func newDialScheduler(config dialConfig, it enode.Iterator, setupFunc dialSetupF
166166
setupFunc: setupFunc,
167167
dialing: make(map[enode.ID]*dialTask),
168168
static: make(map[enode.ID]*dialTask),
169-
peers: make(map[enode.ID]connFlag),
169+
peers: make(map[enode.ID]struct{}),
170170
doneCh: make(chan *dialTask),
171171
nodesIn: make(chan *enode.Node),
172172
addStaticCh: make(chan *enode.Node),
@@ -259,7 +259,7 @@ loop:
259259
d.dialPeers++
260260
}
261261
id := c.node.ID()
262-
d.peers[id] = c.flags
262+
d.peers[id] = struct{}{}
263263
// Remove from static pool because the node is now connected.
264264
task := d.static[id]
265265
if task != nil && task.staticPoolIndex >= 0 {

p2p/enode/iter_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (s *genIter) Node() *Node {
268268
}
269269

270270
func (s *genIter) Close() {
271-
s.index = ^uint32(0)
271+
atomic.StoreUint32(&s.index, ^uint32(0))
272272
}
273273

274274
func testNode(id, seq uint64) *Node {

0 commit comments

Comments
 (0)