Skip to content

Commit

Permalink
fix:deadlock between CommonDiscoveryService.Stop and CommonDiscoveryS…
Browse files Browse the repository at this point in the history
…ervice.PushToChan
  • Loading branch information
qfrank committed Apr 10, 2024
1 parent dd81e1d commit e66538e
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions waku/v2/service/common_discovery_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,29 @@ func (sp *CommonDiscoveryService) GetListeningChan() <-chan PeerData {
return sp.channel
}
func (sp *CommonDiscoveryService) PushToChan(data PeerData) bool {
sp.RLock()
defer sp.RUnlock()
if err := sp.ErrOnNotRunning(); err != nil {
return false
}
done := make(chan bool, 1)
go func() {
sp.RLock()
defer sp.RUnlock()
if err := sp.ErrOnNotRunning(); err != nil {
done <- false
return
}
select {
case sp.channel <- data:
done <- true
case <-sp.Context().Done():
done <- false
}
}()

select {
case sp.channel <- data:
return true
case <-sp.Context().Done():
// should always check if the context is done, otherwise there is a potential for a deadlock.
// see https://github.com/status-im/status-go/issues/4527#issuecomment-2047231127
return false
case result := <-done:
return result
}
}

Expand Down

0 comments on commit e66538e

Please sign in to comment.