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.
This commit is contained in:
eugene 2022-09-06 17:59:25 -04:00
parent 49eeabf2a4
commit 2def3fc542
No known key found for this signature in database
GPG key ID: 118759E83439A9B1

View file

@ -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
}