From 2fd8c61d5b31ad1527a2c7cb346d6ad0333af421 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Mon, 21 Mar 2022 11:35:09 +0000 Subject: [PATCH 1/2] fix(dot/network): ugly fix nil mutex for peer --- dot/network/notifications.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dot/network/notifications.go b/dot/network/notifications.go index 55a278bbeb..8b99ce7f77 100644 --- a/dot/network/notifications.go +++ b/dot/network/notifications.go @@ -293,6 +293,10 @@ 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 { + return nil, fmt.Errorf("got a nil mutex for peer id %s (TODO)", peer) + } + peerMutex.Lock() defer peerMutex.Unlock() From 7d64b92bf0ab952a783c31822277e9996a687adb Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Mon, 21 Mar 2022 11:57:27 +0000 Subject: [PATCH 2/2] Add comments --- dot/network/notifications.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/dot/network/notifications.go b/dot/network/notifications.go index 8b99ce7f77..266ecc6018 100644 --- a/dot/network/notifications.go +++ b/dot/network/notifications.go @@ -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 @@ -294,7 +296,9 @@ func (s *Service) sendHandshake(peer peer.ID, hs Handshake, info *notificationsP // from the map peerMutex := info.peersData.getMutex(peer) if peerMutex == nil { - return nil, fmt.Errorf("got a nil mutex for peer id %s (TODO)", peer) + // 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()