@@ -135,9 +135,10 @@ type Downloader struct {
135
135
stateCh chan dataPack // [eth/63] Channel receiving inbound node state data
136
136
137
137
// 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.
141
142
142
143
quitCh chan struct {} // Quit channel to signal termination
143
144
quitLock sync.RWMutex // Lock to prevent double closes
@@ -476,12 +477,11 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td *big.I
476
477
// spawnSync runs d.process and all given fetcher functions to completion in
477
478
// separate goroutines, returning the first error that appears.
478
479
func (d * Downloader ) spawnSync (fetchers []func () error ) error {
479
- var wg sync.WaitGroup
480
480
errc := make (chan error , len (fetchers ))
481
- wg .Add (len (fetchers ))
481
+ d . cancelWg .Add (len (fetchers ))
482
482
for _ , fn := range fetchers {
483
483
fn := fn
484
- go func () { defer wg .Done (); errc <- fn () }()
484
+ go func () { defer d . cancelWg .Done (); errc <- fn () }()
485
485
}
486
486
// Wait for the first error, then terminate the others.
487
487
var err error
@@ -498,12 +498,10 @@ func (d *Downloader) spawnSync(fetchers []func() error) error {
498
498
}
499
499
d .queue .Close ()
500
500
d .Cancel ()
501
- wg .Wait ()
502
501
return err
503
502
}
504
503
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.
507
505
func (d * Downloader ) Cancel () {
508
506
// Close the current cancel channel
509
507
d .cancelLock .Lock ()
@@ -516,6 +514,7 @@ func (d *Downloader) Cancel() {
516
514
}
517
515
}
518
516
d .cancelLock .Unlock ()
517
+ d .cancelWg .Wait ()
519
518
}
520
519
521
520
// Terminate interrupts the downloader, canceling all pending operations.
0 commit comments