From 9a15755f1014a7371a9a1036a67502c221a5d2e8 Mon Sep 17 00:00:00 2001 From: ucwong Date: Sun, 21 Apr 2024 00:19:42 +0800 Subject: [PATCH] eth/downloader : avoid timer leak --- eth/downloader/downloader.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 941f575aa898..ff58535c4a76 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -1015,11 +1015,16 @@ func (d *Downloader) fetchHeaders(p *peerConnection, from uint64, head uint64) e // Start pulling the header chain skeleton until all is done var ( - skeleton = true // Skeleton assembly phase or finishing up - pivoting = false // Whether the next request is pivot verification - ancestor = from - mode = d.getMode() + skeleton = true // Skeleton assembly phase or finishing up + pivoting = false // Whether the next request is pivot verification + ancestor = from + mode = d.getMode() + headerCheckTimer = time.NewTimer(fsHeaderContCheck) + headerDelayedTimer = time.NewTimer(fsHeaderContCheck) ) + defer headerCheckTimer.Stop() + defer headerDelayedTimer.Stop() + for { // Pull the next batch of headers, it either: // - Pivot check to see if the chain moved too far @@ -1124,8 +1129,9 @@ func (d *Downloader) fetchHeaders(p *peerConnection, from uint64, head uint64) e // Don't abort header fetches while the pivot is downloading if !d.committed.Load() && pivot <= from { p.log.Debug("No headers, waiting for pivot commit") + headerCheckTimer.Reset(fsHeaderContCheck) select { - case <-time.After(fsHeaderContCheck): + case <-headerCheckTimer.C: continue case <-d.cancelCh: return errCanceled @@ -1195,8 +1201,9 @@ func (d *Downloader) fetchHeaders(p *peerConnection, from uint64, head uint64) e // skeleton filling if len(headers) == 0 && !progressed { p.log.Trace("All headers delayed, waiting") + headerDelayedTimer.Reset(fsHeaderContCheck) select { - case <-time.After(fsHeaderContCheck): + case <-headerDelayedTimer.C: continue case <-d.cancelCh: return errCanceled