Skip to content

Commit

Permalink
Prevent panicking when fetching without any peers (spacemeshos#4657)
Browse files Browse the repository at this point in the history
## Motivation
There was a debug panic that was not removed from spacemeshos#4374, and also `randomPeer()` would fatally exit if there were no peers.
We don't actually want to panic, when trying to service a fetch request and there are no peers, so now we check to see if we have no peers and simply return, the fetch operation is performed on a ticker and so will be re-executed after some period.

## DevOps Notes
<!-- Please uncheck these items as applicable to make DevOps aware of changes that may affect releases -->
- [x] This PR does not require configuration changes (e.g., environment variables, GitHub secrets, VM resources)
- [x] This PR does not affect public APIs
- [x] This PR does not rely on a new version of external services (PoET, elasticsearch, etc.)
- [x] This PR does not make changes to log messages (which monitoring infrastructure may rely on)
  • Loading branch information
piersy committed Jul 8, 2023
1 parent 19cedfa commit a3a8963
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ func DefaultConfig() Config {

// randomPeer returns a random peer from current peer list.
func randomPeer(peers []p2p.Peer) p2p.Peer {
if len(peers) == 0 {
log.Fatal("cannot send fetch: no peers found")
}
return peers[rand.Intn(len(peers))]
}

Expand Down Expand Up @@ -460,7 +457,9 @@ func (f *Fetch) organizeRequests(requests []RequestMessage) map[p2p.Peer][][]Req
peer2requests := make(map[p2p.Peer][]RequestMessage)
peers := f.host.GetPeers()
if len(peers) == 0 {
panic("no peers")
log.Info("cannot send fetch: no peers found")
// in loop() we will try again after the batchTimeout
return nil
}

for _, req := range requests {
Expand Down

0 comments on commit a3a8963

Please sign in to comment.