Skip to content

Commit 86493f9

Browse files
karalabeobscuren
authored andcommitted
[release/1.4.11] eth/downloader: abort sync if master drops (timeout prev)
(cherry picked from commit 8f0a4a2)
1 parent 6c672a5 commit 86493f9

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

eth/downloader/downloader.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,10 @@ type Downloader struct {
147147
stateWakeCh chan bool // [eth/63] Channel to signal the state fetcher of new tasks
148148
headerProcCh chan []*types.Header // [eth/62] Channel to feed the header processor new tasks
149149

150+
// Cancellation and termination
151+
cancelPeer string // Identifier of the peer currently being used as the master (cancel on drop)
150152
cancelCh chan struct{} // Channel to cancel mid-flight syncs
151-
cancelLock sync.RWMutex // Lock to protect the cancel channel in delivers
153+
cancelLock sync.RWMutex // Lock to protect the cancel channel and peer in delivers
152154

153155
quitCh chan struct{} // Quit channel to signal termination
154156
quitLock sync.RWMutex // Lock to prevent double closes
@@ -254,12 +256,22 @@ func (d *Downloader) RegisterPeer(id string, version int, currentHead currentHea
254256
// the specified peer. An effort is also made to return any pending fetches into
255257
// the queue.
256258
func (d *Downloader) UnregisterPeer(id string) error {
259+
// Unregister the peer from the active peer set and revoke any fetch tasks
257260
glog.V(logger.Detail).Infoln("Unregistering peer", id)
258261
if err := d.peers.Unregister(id); err != nil {
259262
glog.V(logger.Error).Infoln("Unregister failed:", err)
260263
return err
261264
}
262265
d.queue.Revoke(id)
266+
267+
// If this peer was the master peer, abort sync immediately
268+
d.cancelLock.RLock()
269+
master := id == d.cancelPeer
270+
d.cancelLock.RUnlock()
271+
272+
if master {
273+
d.cancel()
274+
}
263275
return nil
264276
}
265277

@@ -332,9 +344,10 @@ func (d *Downloader) synchronise(id string, hash common.Hash, td *big.Int, mode
332344
empty = true
333345
}
334346
}
335-
// Create cancel channel for aborting mid-flight
347+
// Create cancel channel for aborting mid-flight and mark the master peer
336348
d.cancelLock.Lock()
337349
d.cancelCh = make(chan struct{})
350+
d.cancelPeer = id
338351
d.cancelLock.Unlock()
339352

340353
defer d.cancel() // No matter what, we can't leave the cancel channel open

0 commit comments

Comments
 (0)