Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

network: fix publicKeyIdentTracker data race in hybrid mode #6110

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions network/netidentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/protocol"
"github.com/algorand/go-deadlock"
)

// netidentity.go implements functionality to participate in an "Identity Challenge Exchange"
Expand Down Expand Up @@ -461,12 +462,14 @@ func (noopIdentityTracker) removeIdentity(p *wsPeer) {}
// mapping from PublicKeys exchanged in identity challenges to a peer
// this structure is not thread-safe; it is protected by wn.peersLock or p2p.wsPeersLock
type publicKeyIdentTracker struct {
mu deadlock.Mutex
peersByID map[crypto.PublicKey]*wsPeer
}

// NewIdentityTracker returns a new publicKeyIdentTracker
func NewIdentityTracker() *publicKeyIdentTracker {
return &publicKeyIdentTracker{
mu: deadlock.Mutex{},
peersByID: make(map[crypto.PublicKey]*wsPeer),
}
}
Expand All @@ -475,6 +478,8 @@ func NewIdentityTracker() *publicKeyIdentTracker {
// returns false if it was unable to load the peer into the given identity
// or true otherwise (if the peer was already there, or if it was added)
func (t *publicKeyIdentTracker) setIdentity(p *wsPeer) bool {
t.mu.Lock()
defer t.mu.Unlock()
existingPeer, exists := t.peersByID[p.identity]
if !exists {
// the identity is not occupied, so set it and return true
Expand All @@ -489,6 +494,8 @@ func (t *publicKeyIdentTracker) setIdentity(p *wsPeer) bool {
// removeIdentity removes the entry in the peersByID map if it exists
// and is occupied by the given peer
func (t *publicKeyIdentTracker) removeIdentity(p *wsPeer) {
t.mu.Lock()
defer t.mu.Unlock()
if t.peersByID[p.identity] == p {
delete(t.peersByID, p.identity)
}
Expand Down
Loading