fix: prevent `panic: send on closed channel`

This commit is contained in:
Patryk Osmaczko 2024-02-25 17:35:54 +01:00 committed by osmaczko
parent 5aed0d178f
commit bafb0a71d0
1 changed files with 12 additions and 1 deletions

View File

@ -82,7 +82,8 @@ type MessageSender struct {
ephemeralKeysMutex sync.Mutex
// messageEventsSubscriptions contains all the subscriptions for message events
messageEventsSubscriptions []chan<- *MessageEvent
messageEventsSubscriptions []chan<- *MessageEvent
messageEventsSubscriptionsMutex sync.Mutex
featureFlags FeatureFlags
@ -114,6 +115,9 @@ func NewMessageSender(
}
func (s *MessageSender) Stop() {
s.messageEventsSubscriptionsMutex.Lock()
defer s.messageEventsSubscriptionsMutex.Unlock()
for _, c := range s.messageEventsSubscriptions {
close(c)
}
@ -1184,6 +1188,10 @@ func (s *MessageSender) notifyOnSentMessage(sentMessage *SentMessage) {
Type: MessageSent,
SentMessage: sentMessage,
}
s.messageEventsSubscriptionsMutex.Lock()
defer s.messageEventsSubscriptionsMutex.Unlock()
// Publish on channels, drop if buffer is full
for _, c := range s.messageEventsSubscriptions {
select {
@ -1202,6 +1210,9 @@ func (s *MessageSender) notifyOnScheduledMessage(recipient *ecdsa.PublicKey, mes
RawMessage: message,
}
s.messageEventsSubscriptionsMutex.Lock()
defer s.messageEventsSubscriptionsMutex.Unlock()
// Publish on channels, drop if buffer is full
for _, c := range s.messageEventsSubscriptions {
select {