2018-08-27 08:22:21 +00:00
|
|
|
diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go
|
|
|
|
index fbde9c6c..c0013a11 100644
|
|
|
|
--- a/eth/downloader/downloader.go
|
|
|
|
+++ b/eth/downloader/downloader.go
|
2018-05-04 05:00:55 +00:00
|
|
|
@@ -143,6 +143,8 @@ type Downloader struct {
|
2018-03-05 20:36:32 +00:00
|
|
|
quitCh chan struct{} // Quit channel to signal termination
|
|
|
|
quitLock sync.RWMutex // Lock to prevent double closes
|
2018-04-26 12:35:03 +00:00
|
|
|
|
2018-03-05 20:36:32 +00:00
|
|
|
+ downloads sync.WaitGroup // Keeps track of the currently active downloads
|
|
|
|
+
|
|
|
|
// Testing hooks
|
|
|
|
syncInitHook func(uint64, uint64) // Method to call upon initiating a new sync run
|
|
|
|
bodyFetchHook func([]*types.Header) // Method to call upon starting a block body fetch
|
2018-05-04 05:00:55 +00:00
|
|
|
@@ -403,7 +405,9 @@ func (d *Downloader) synchronise(id string, hash common.Hash, td *big.Int, mode
|
2018-03-05 20:36:32 +00:00
|
|
|
// specified peer and head hash.
|
|
|
|
func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td *big.Int) (err error) {
|
|
|
|
d.mux.Post(StartEvent{})
|
|
|
|
+ d.downloads.Add(1)
|
|
|
|
defer func() {
|
|
|
|
+ d.downloads.Done()
|
|
|
|
// reset on error
|
|
|
|
if err != nil {
|
|
|
|
d.mux.Post(FailedEvent{err})
|
2018-05-04 05:00:55 +00:00
|
|
|
@@ -471,14 +475,22 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td *big.I
|
2018-04-27 09:09:55 +00:00
|
|
|
} else if d.mode == FullSync {
|
|
|
|
fetchers = append(fetchers, d.processFullSyncContent)
|
|
|
|
}
|
|
|
|
- return d.spawnSync(fetchers)
|
|
|
|
+ return d.spawnSync(errCancelHeaderFetch, fetchers)
|
|
|
|
}
|
|
|
|
|
|
|
|
// spawnSync runs d.process and all given fetcher functions to completion in
|
|
|
|
// separate goroutines, returning the first error that appears.
|
|
|
|
-func (d *Downloader) spawnSync(fetchers []func() error) error {
|
|
|
|
+func (d *Downloader) spawnSync(errCancel error, fetchers []func() error) error {
|
2018-05-04 05:00:55 +00:00
|
|
|
+ d.cancelLock.Lock()
|
2018-04-27 09:09:55 +00:00
|
|
|
+ select {
|
|
|
|
+ case <-d.cancelCh:
|
2018-05-04 05:00:55 +00:00
|
|
|
+ d.cancelLock.Unlock()
|
2018-04-27 09:09:55 +00:00
|
|
|
+ return errCancel
|
|
|
|
+ default:
|
|
|
|
+ }
|
|
|
|
errc := make(chan error, len(fetchers))
|
|
|
|
d.cancelWg.Add(len(fetchers))
|
2018-05-04 05:00:55 +00:00
|
|
|
+ d.cancelLock.Unlock()
|
2018-04-27 09:09:55 +00:00
|
|
|
for _, fn := range fetchers {
|
2018-05-04 05:00:55 +00:00
|
|
|
fn := fn
|
|
|
|
go func() { defer d.cancelWg.Done(); errc <- fn() }()
|
|
|
|
@@ -539,6 +551,10 @@ func (d *Downloader) Terminate() {
|
2018-04-26 12:35:03 +00:00
|
|
|
|
2018-03-05 20:36:32 +00:00
|
|
|
// Cancel any pending download requests
|
|
|
|
d.Cancel()
|
|
|
|
+
|
|
|
|
+ // Wait, so external dependencies aren't destroyed
|
|
|
|
+ // until the download processing is done.
|
|
|
|
+ d.downloads.Wait()
|
|
|
|
}
|
2018-04-26 12:35:03 +00:00
|
|
|
|
2018-03-05 20:36:32 +00:00
|
|
|
// fetchHeight retrieves the head header of the remote peer to aid in estimating
|
2018-08-27 08:22:21 +00:00
|
|
|
diff --git a/eth/handler.go b/eth/handler.go
|
|
|
|
index f89f68c9..5522b0d9 100644
|
|
|
|
--- a/eth/handler.go
|
|
|
|
+++ b/eth/handler.go
|
2018-03-05 20:36:32 +00:00
|
|
|
@@ -230,6 +230,9 @@ func (pm *ProtocolManager) Stop() {
|
|
|
|
// Quit fetcher, txsyncLoop.
|
|
|
|
close(pm.quitSync)
|
2018-05-04 05:00:55 +00:00
|
|
|
|
2018-03-05 20:36:32 +00:00
|
|
|
+ // Stop downloader and make sure that all the running downloads are complete.
|
|
|
|
+ pm.downloader.Terminate()
|
|
|
|
+
|
|
|
|
// Disconnect existing sessions.
|
|
|
|
// This also closes the gate for any new registrations on the peer set.
|
|
|
|
// sessions which are already established but not added to pm.peers yet
|
2018-08-27 08:22:21 +00:00
|
|
|
diff --git a/eth/sync.go b/eth/sync.go
|
|
|
|
index e49e4008..4367434a 100644
|
|
|
|
--- a/eth/sync.go
|
|
|
|
+++ b/eth/sync.go
|
2018-03-05 20:36:32 +00:00
|
|
|
@@ -135,7 +135,6 @@ func (pm *ProtocolManager) syncer() {
|
|
|
|
// Start and ensure cleanup of sync mechanisms
|
|
|
|
pm.fetcher.Start()
|
|
|
|
defer pm.fetcher.Stop()
|
|
|
|
- defer pm.downloader.Terminate()
|
2018-05-04 05:00:55 +00:00
|
|
|
|
2018-03-05 20:36:32 +00:00
|
|
|
// Wait for different events to fire synchronisation operations
|
|
|
|
forceSync := time.NewTicker(forceSyncCycle)
|
2018-08-27 08:22:21 +00:00
|
|
|
diff --git a/les/backend.go b/les/backend.go
|
|
|
|
index 00025ba6..38c36da6 100644
|
|
|
|
--- a/les/backend.go
|
|
|
|
+++ b/les/backend.go
|
2018-05-04 05:00:55 +00:00
|
|
|
@@ -20,7 +20,6 @@ package les
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
- "time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-08-27 08:22:21 +00:00
|
|
|
@@ -253,7 +252,6 @@ func (s *LightEthereum) Stop() error {
|
2018-05-04 05:00:55 +00:00
|
|
|
|
|
|
|
s.eventMux.Stop()
|
|
|
|
|
|
|
|
- time.Sleep(time.Millisecond * 200)
|
|
|
|
s.chainDb.Close()
|
|
|
|
close(s.shutdownChan)
|
|
|
|
|
2018-08-27 08:22:21 +00:00
|
|
|
diff --git a/les/handler.go b/les/handler.go
|
|
|
|
index ca40eaab..cc15d68c 100644
|
|
|
|
--- a/les/handler.go
|
|
|
|
+++ b/les/handler.go
|
|
|
|
@@ -194,6 +194,9 @@ func (pm *ProtocolManager) Stop() {
|
|
|
|
pm.clientPool.stop()
|
|
|
|
}
|
2018-05-04 05:00:55 +00:00
|
|
|
|
2018-03-05 20:36:32 +00:00
|
|
|
+ // Stop downloader and make sure that all the running downloads are complete.
|
|
|
|
+ pm.downloader.Terminate()
|
|
|
|
+
|
|
|
|
// Disconnect existing sessions.
|
|
|
|
// This also closes the gate for any new registrations on the peer set.
|
|
|
|
// sessions which are already established but not added to pm.peers yet
|
2018-08-27 08:22:21 +00:00
|
|
|
diff --git a/les/sync.go b/les/sync.go
|
|
|
|
index 1ac64558..eb155377 100644
|
|
|
|
--- a/les/sync.go
|
|
|
|
+++ b/les/sync.go
|
2018-05-04 05:00:55 +00:00
|
|
|
@@ -31,7 +31,6 @@ func (pm *ProtocolManager) syncer() {
|
2018-03-05 20:36:32 +00:00
|
|
|
// Start and ensure cleanup of sync mechanisms
|
|
|
|
//pm.fetcher.Start()
|
|
|
|
//defer pm.fetcher.Stop()
|
|
|
|
- defer pm.downloader.Terminate()
|
2018-05-04 05:00:55 +00:00
|
|
|
|
2018-03-05 20:36:32 +00:00
|
|
|
// Wait for different events to fire synchronisation operations
|
|
|
|
//forceSync := time.Tick(forceSyncCycle)
|