Skip to content

Commit 3202dfa

Browse files
committed
check if channel is nil before closing it
1 parent c1bd4ec commit 3202dfa

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

server/internal/infrastructure/live-store/inmemory/tree_signing_session.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,14 @@ func (s *treeSigningSessionsStore) Get(roundId string) (*ports.MusigSigningSessi
5252
func (s *treeSigningSessionsStore) Delete(roundId string) {
5353
s.lock.Lock()
5454
defer s.lock.Unlock()
55-
close(s.nonceCollectedCh[roundId])
56-
close(s.sigsCollectedCh[roundId])
55+
56+
if ch, exists := s.nonceCollectedCh[roundId]; exists {
57+
close(ch)
58+
}
59+
if ch, exists := s.sigsCollectedCh[roundId]; exists {
60+
close(ch)
61+
}
62+
5763
delete(s.nonceCollectedCh, roundId)
5864
delete(s.sigsCollectedCh, roundId)
5965
delete(s.sessions, roundId)
@@ -76,7 +82,12 @@ func (s *treeSigningSessionsStore) AddNonces(
7682
s.sessions[roundId].Nonces[pubkey] = nonces
7783

7884
if len(s.sessions[roundId].Nonces) == s.sessions[roundId].NbCosigners-1 {
79-
s.nonceCollectedCh[roundId] <- struct{}{}
85+
if ch, exists := s.nonceCollectedCh[roundId]; exists {
86+
select {
87+
case ch <- struct{}{}:
88+
default:
89+
}
90+
}
8091
}
8192

8293
return nil
@@ -99,16 +110,31 @@ func (s *treeSigningSessionsStore) AddSignatures(
99110
s.sessions[roundId].Signatures[pubkey] = sigs
100111

101112
if len(s.sessions[roundId].Signatures) == s.sessions[roundId].NbCosigners-1 {
102-
s.sigsCollectedCh[roundId] <- struct{}{}
113+
if ch, exists := s.sigsCollectedCh[roundId]; exists {
114+
select {
115+
case ch <- struct{}{}:
116+
default:
117+
}
118+
}
103119
}
104120

105121
return nil
106122
}
107123

108124
func (s *treeSigningSessionsStore) NoncesCollected(roundId string) <-chan struct{} {
109-
return s.nonceCollectedCh[roundId]
125+
s.lock.RLock()
126+
defer s.lock.RUnlock()
127+
if ch, exists := s.nonceCollectedCh[roundId]; exists {
128+
return ch
129+
}
130+
return nil
110131
}
111132

112133
func (s *treeSigningSessionsStore) SignaturesCollected(roundId string) <-chan struct{} {
113-
return s.sigsCollectedCh[roundId]
134+
s.lock.RLock()
135+
defer s.lock.RUnlock()
136+
if ch, exists := s.sigsCollectedCh[roundId]; exists {
137+
return ch
138+
}
139+
return nil
114140
}

0 commit comments

Comments
 (0)