Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth, les: wait for the downloads to be complete before stopping #16200

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions eth/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ type Downloader struct {
quitCh chan struct{} // Quit channel to signal termination
quitLock sync.RWMutex // Lock to prevent double closes

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
Expand Down Expand Up @@ -398,7 +400,9 @@ func (d *Downloader) synchronise(id string, hash common.Hash, td *big.Int, mode
// 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})
Expand Down Expand Up @@ -528,6 +532,10 @@ func (d *Downloader) Terminate() {

// Cancel any pending download requests
d.Cancel()

// Wait, so external dependencies aren't destroyed
// until the download processing is done.
d.downloads.Wait()
}

// fetchHeight retrieves the head header of the remote peer to aid in estimating
Expand Down
3 changes: 3 additions & 0 deletions eth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ func (pm *ProtocolManager) Stop() {
// Quit fetcher, txsyncLoop.
close(pm.quitSync)

// 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
Expand Down
1 change: 0 additions & 1 deletion eth/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

// Wait for different events to fire synchronisation operations
forceSync := time.NewTicker(forceSyncCycle)
Expand Down
3 changes: 3 additions & 0 deletions les/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ func (pm *ProtocolManager) Stop() {

close(pm.quitSync) // quits syncer, fetcher

// 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
Expand Down
1 change: 0 additions & 1 deletion les/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func (pm *ProtocolManager) syncer() {
// Start and ensure cleanup of sync mechanisms
//pm.fetcher.Start()
//defer pm.fetcher.Stop()
defer pm.downloader.Terminate()

// Wait for different events to fire synchronisation operations
//forceSync := time.Tick(forceSyncCycle)
Expand Down