Skip to content

Commit de2a7bb

Browse files
rjl493456442karalabe
authored andcommitted
eth/downloader: wait for all fetcher goroutines to exit before terminating (#16509)
1 parent 6b2b328 commit de2a7bb

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

eth/downloader/downloader.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,10 @@ type Downloader struct {
135135
stateCh chan dataPack // [eth/63] Channel receiving inbound node state data
136136

137137
// Cancellation and termination
138-
cancelPeer string // Identifier of the peer currently being used as the master (cancel on drop)
139-
cancelCh chan struct{} // Channel to cancel mid-flight syncs
140-
cancelLock sync.RWMutex // Lock to protect the cancel channel and peer in delivers
138+
cancelPeer string // Identifier of the peer currently being used as the master (cancel on drop)
139+
cancelCh chan struct{} // Channel to cancel mid-flight syncs
140+
cancelLock sync.RWMutex // Lock to protect the cancel channel and peer in delivers
141+
cancelWg sync.WaitGroup // Make sure all fetcher goroutines have exited.
141142

142143
quitCh chan struct{} // Quit channel to signal termination
143144
quitLock sync.RWMutex // Lock to prevent double closes
@@ -476,12 +477,11 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td *big.I
476477
// spawnSync runs d.process and all given fetcher functions to completion in
477478
// separate goroutines, returning the first error that appears.
478479
func (d *Downloader) spawnSync(fetchers []func() error) error {
479-
var wg sync.WaitGroup
480480
errc := make(chan error, len(fetchers))
481-
wg.Add(len(fetchers))
481+
d.cancelWg.Add(len(fetchers))
482482
for _, fn := range fetchers {
483483
fn := fn
484-
go func() { defer wg.Done(); errc <- fn() }()
484+
go func() { defer d.cancelWg.Done(); errc <- fn() }()
485485
}
486486
// Wait for the first error, then terminate the others.
487487
var err error
@@ -498,12 +498,10 @@ func (d *Downloader) spawnSync(fetchers []func() error) error {
498498
}
499499
d.queue.Close()
500500
d.Cancel()
501-
wg.Wait()
502501
return err
503502
}
504503

505-
// Cancel cancels all of the operations and resets the queue. It returns true
506-
// if the cancel operation was completed.
504+
// Cancel cancels all of the operations and resets the queue.
507505
func (d *Downloader) Cancel() {
508506
// Close the current cancel channel
509507
d.cancelLock.Lock()
@@ -516,6 +514,7 @@ func (d *Downloader) Cancel() {
516514
}
517515
}
518516
d.cancelLock.Unlock()
517+
d.cancelWg.Wait()
519518
}
520519

521520
// Terminate interrupts the downloader, canceling all pending operations.

0 commit comments

Comments
 (0)