Skip to content

Commit 111a1b7

Browse files
authored
Merge pull request #24652 from karalabe/block-fetcher-timeouts
eth/fetcher: if peers never respond, drop them
2 parents fb3a081 + 7e2bbb9 commit 111a1b7

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

eth/fetcher/block_fetcher.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,21 @@ func (f *BlockFetcher) loop() {
477477
}
478478
defer req.Close()
479479

480-
res := <-resCh
481-
res.Done <- nil
482-
483-
f.FilterHeaders(peer, *res.Res.(*eth.BlockHeadersPacket), time.Now().Add(res.Time))
480+
timeout := time.NewTimer(2 * fetchTimeout) // 2x leeway before dropping the peer
481+
defer timeout.Stop()
482+
483+
select {
484+
case res := <-resCh:
485+
res.Done <- nil
486+
f.FilterHeaders(peer, *res.Res.(*eth.BlockHeadersPacket), time.Now().Add(res.Time))
487+
488+
case <-timeout.C:
489+
// The peer didn't respond in time. The request
490+
// was already rescheduled at this point, we were
491+
// waiting for a catchup. With an unresponsive
492+
// peer however, it's a protocol violation.
493+
f.dropPeer(peer)
494+
}
484495
}(hash)
485496
}
486497
}(peer)
@@ -523,11 +534,23 @@ func (f *BlockFetcher) loop() {
523534
}
524535
defer req.Close()
525536

526-
res := <-resCh
527-
res.Done <- nil
537+
timeout := time.NewTimer(2 * fetchTimeout) // 2x leeway before dropping the peer
538+
defer timeout.Stop()
539+
540+
select {
541+
case res := <-resCh:
542+
res.Done <- nil
528543

529-
txs, uncles := res.Res.(*eth.BlockBodiesPacket).Unpack()
530-
f.FilterBodies(peer, txs, uncles, time.Now())
544+
txs, uncles := res.Res.(*eth.BlockBodiesPacket).Unpack()
545+
f.FilterBodies(peer, txs, uncles, time.Now())
546+
547+
case <-timeout.C:
548+
// The peer didn't respond in time. The request
549+
// was already rescheduled at this point, we were
550+
// waiting for a catchup. With an unresponsive
551+
// peer however, it's a protocol violation.
552+
f.dropPeer(peer)
553+
}
531554
}(peer, hashes)
532555
}
533556
// Schedule the next fetch if blocks are still pending

0 commit comments

Comments
 (0)