You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
p2p: Switch.OnStop uses stale and already mutated PeerSet.List() which could have data race consequences due to a leaky abstraction of using .List() #2158
which returns a copy of the slice while under a lock but after handing it over, it is no longer under a lock and is modified by .Remove() which causes .OnStop() to read stale data.
I found this problem while working on the correct fix for #2157 in which the correct way to shorten a slice is to ensure you insert a nil at the last element before reslicing to ensure that there are no references to it and that the backing array can be garbage collected i.e.
endItem:=slice[len(slice)-1]
slice[len(slice)-1] =nil// nil it so that it'll be garbage collected
I can reliably reproduce this issue when I apply the fix for the referenced issue
Activity
fix(p2p): make PeerSet.Remove more efficient
fix(p2p): make PeerSet.Remove more efficient
fix(p2p): make PeerSet.Remove more efficient
fix(p2p): make PeerSet.Remove more efficient
fix(p2p): make PeerSet.Remove more efficient
odeke-em commentedon Jan 29, 2024
And indeed, the data race is exposed in https://github.com/cometbft/cometbft/actions/runs/7692838534/job/20960454814?pr=2159 in which this code uses a stale version of the peerSet's list in
cometbft/p2p/switch.go
Lines 276 to 281 in 940378c
cason commentedon Jan 29, 2024
Indeed, we need to return a copy of the list of peers here.
This method is actually called in several parts of the
p2ppackage and needs to be reconsidered.refactor(p2p)!: Refactor PeerSet to eliminate data races & improve pe…
refactor(p2p)!: Refactor PeerSet to eliminate data races & improve pe…
refactor(p2p)!: Refactor PeerSet to eliminate data races & improve pe…
refactor(p2p)!: Refactor PeerSet to eliminate data races & improve pe…