Skip to content

Commit f459a3f

Browse files
committed
eth/downloader: always send termination wakes, clean leftover
1 parent e456f27 commit f459a3f

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

eth/downloader/downloader.go

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ type Downloader struct {
154154
blockCh chan blockPack // [eth/61] Channel receiving inbound blocks
155155
headerCh chan headerPack // [eth/62] Channel receiving inbound block headers
156156
bodyCh chan bodyPack // [eth/62] Channel receiving inbound block bodies
157-
processCh chan bool // Channel to signal the block fetcher of new or finished work
157+
wakeCh chan bool // Channel to signal the block/body fetcher of new tasks
158158

159159
cancelCh chan struct{} // Channel to cancel mid-flight syncs
160160
cancelLock sync.RWMutex // Lock to protect the cancel channel in delivers
@@ -188,7 +188,7 @@ func New(mux *event.TypeMux, hasBlock hashCheckFn, getBlock blockRetrievalFn, he
188188
blockCh: make(chan blockPack, 1),
189189
headerCh: make(chan headerPack, 1),
190190
bodyCh: make(chan bodyPack, 1),
191-
processCh: make(chan bool, 1),
191+
wakeCh: make(chan bool, 1),
192192
}
193193
}
194194

@@ -282,6 +282,10 @@ func (d *Downloader) synchronise(id string, hash common.Hash, td *big.Int) error
282282
d.queue.Reset()
283283
d.peers.Reset()
284284

285+
select {
286+
case <-d.wakeCh:
287+
default:
288+
}
285289
// Create cancel channel for aborting mid-flight
286290
d.cancelLock.Lock()
287291
d.cancelCh = make(chan struct{})
@@ -633,7 +637,7 @@ func (d *Downloader) fetchHashes61(p *peer, td *big.Int, from uint64) error {
633637
glog.V(logger.Debug).Infof("%v: no available hashes", p)
634638

635639
select {
636-
case d.processCh <- false:
640+
case d.wakeCh <- false:
637641
case <-d.cancelCh:
638642
}
639643
// If no hashes were retrieved at all, the peer violated it's TD promise that it had a
@@ -664,12 +668,18 @@ func (d *Downloader) fetchHashes61(p *peer, td *big.Int, from uint64) error {
664668
return errBadPeer
665669
}
666670
// Notify the block fetcher of new hashes, but stop if queue is full
667-
cont := d.queue.Pending() < maxQueuedHashes
668-
select {
669-
case d.processCh <- cont:
670-
default:
671-
}
672-
if !cont {
671+
if d.queue.Pending() < maxQueuedHashes {
672+
// We still have hashes to fetch, send continuation wake signal (potential)
673+
select {
674+
case d.wakeCh <- true:
675+
default:
676+
}
677+
} else {
678+
// Hash limit reached, send a termination wake signal (enforced)
679+
select {
680+
case d.wakeCh <- false:
681+
case <-d.cancelCh:
682+
}
673683
return nil
674684
}
675685
// Queue not yet full, fetch the next batch
@@ -766,7 +776,7 @@ func (d *Downloader) fetchBlocks61(from uint64) error {
766776
default:
767777
}
768778

769-
case cont := <-d.processCh:
779+
case cont := <-d.wakeCh:
770780
// The hash fetcher sent a continuation flag, check if it's done
771781
if !cont {
772782
finished = true
@@ -1053,7 +1063,7 @@ func (d *Downloader) fetchHeaders(p *peer, td *big.Int, from uint64) error {
10531063
glog.V(logger.Debug).Infof("%v: no available headers", p)
10541064

10551065
select {
1056-
case d.processCh <- false:
1066+
case d.wakeCh <- false:
10571067
case <-d.cancelCh:
10581068
}
10591069
// If no headers were retrieved at all, the peer violated it's TD promise that it had a
@@ -1084,12 +1094,18 @@ func (d *Downloader) fetchHeaders(p *peer, td *big.Int, from uint64) error {
10841094
return errBadPeer
10851095
}
10861096
// Notify the block fetcher of new headers, but stop if queue is full
1087-
cont := d.queue.Pending() < maxQueuedHeaders
1088-
select {
1089-
case d.processCh <- cont:
1090-
default:
1091-
}
1092-
if !cont {
1097+
if d.queue.Pending() < maxQueuedHeaders {
1098+
// We still have headers to fetch, send continuation wake signal (potential)
1099+
select {
1100+
case d.wakeCh <- true:
1101+
default:
1102+
}
1103+
} else {
1104+
// Header limit reached, send a termination wake signal (enforced)
1105+
select {
1106+
case d.wakeCh <- false:
1107+
case <-d.cancelCh:
1108+
}
10931109
return nil
10941110
}
10951111
// Queue not yet full, fetch the next batch
@@ -1104,8 +1120,8 @@ func (d *Downloader) fetchHeaders(p *peer, td *big.Int, from uint64) error {
11041120

11051121
// Finish the sync gracefully instead of dumping the gathered data though
11061122
select {
1107-
case d.processCh <- false:
1108-
default:
1123+
case d.wakeCh <- false:
1124+
case <-d.cancelCh:
11091125
}
11101126
return nil
11111127
}
@@ -1199,7 +1215,7 @@ func (d *Downloader) fetchBodies(from uint64) error {
11991215
default:
12001216
}
12011217

1202-
case cont := <-d.processCh:
1218+
case cont := <-d.wakeCh:
12031219
// The header fetcher sent a continuation flag, check if it's done
12041220
if !cont {
12051221
finished = true

0 commit comments

Comments
 (0)