From 2def3fc54259beb462426c9d276a3f9a6d0e4cb8 Mon Sep 17 00:00:00 2001 From: eugene Date: Tue, 6 Sep 2022 17:59:25 -0400 Subject: [PATCH] server.go: add peerChan to peerConnectedListeners in NotifyWhenOnline This fixes a bug where a caller would: - call NotifyWhenOnline and pass in a channel - the server's mutex is held and the pubStr is checked in peersByPub - the peer object is in the peersByPub map - the peer object has its quit channel closed - QuitSignal is called in the select statement, and the function returns - the caller is still waiting for the channel to be sent on forever since it was never sent on or added to the peerConnectedListeners slice. This patch fixes the above bug by adding the channel to the peerConnectedListeners slice if the QuitSignal select case is called. --- server.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/server.go b/server.go index ad34055d5..3d4a3d640 100644 --- a/server.go +++ b/server.go @@ -3195,7 +3195,13 @@ func (s *server) NotifyWhenOnline(peerKey [33]byte, select { case <-peer.ActiveSignal(): case <-peer.QuitSignal(): - // The peer quit so we'll just return. + // The peer quit, so we'll add the channel to the slice + // and return. + s.mu.Lock() + s.peerConnectedListeners[pubStr] = append( + s.peerConnectedListeners[pubStr], peerChan, + ) + s.mu.Unlock() return }