Skip to content

Commit

Permalink
fix(dot/network): re-add nil mutex check for disconnected peer (#2408)
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 authored Mar 21, 2022
1 parent 86624cf commit 9b39bd1
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions dot/network/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ func (s *Service) sendData(peer peer.ID, hs Handshake, info *notificationsProtoc
}, peer)
}

var errPeerDisconnected = errors.New("peer disconnected")

func (s *Service) sendHandshake(peer peer.ID, hs Handshake, info *notificationsProtocol) (libp2pnetwork.Stream, error) {
// multiple processes could each call this upcoming section, opening multiple streams and
// sending multiple handshakes. thus, we need to have a per-peer and per-protocol lock
Expand All @@ -293,6 +295,12 @@ func (s *Service) sendHandshake(peer peer.ID, hs Handshake, info *notificationsP
// so we cannot have a method on peersData to lock and unlock the mutex
// from the map
peerMutex := info.peersData.getMutex(peer)
if peerMutex == nil {
// Note: the only place the mutex is deleted is when the peer disconnects.
// If it doesn't exist, the peer never connected either.
return nil, fmt.Errorf("%w: peer id %s", errPeerDisconnected, peer)
}

peerMutex.Lock()
defer peerMutex.Unlock()

Expand Down

0 comments on commit 9b39bd1

Please sign in to comment.