From d4d67d9b370539a6c0557685bf2319cecaecf5b4 Mon Sep 17 00:00:00 2001 From: Olanrewaju Collins Date: Fri, 17 Jan 2020 09:32:55 +0100 Subject: [PATCH] sync: close account discovery channel before canceling sync (#87) Account discovery thread runs while sync is canceling and it eventually causes a crash. This pull request fixes it by canceling the account discovery thread before canceling sync. --- sync.go | 7 ++++++- syncnotification.go | 16 ++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/sync.go b/sync.go index dc087a1f..65bcbd25 100644 --- a/sync.go +++ b/sync.go @@ -53,7 +53,7 @@ type activeSyncData struct { addressDiscoveryStartTime int64 totalDiscoveryTimeSpent int64 - addressDiscoveryCompleted chan bool + addressDiscoveryCompletedOrCanceled chan bool rescanStartTime int64 @@ -252,6 +252,11 @@ func (mw *MultiWallet) CancelSync() { if mw.syncData.cancelSync != nil { log.Info("Canceling sync. May take a while for sync to fully cancel.") + if mw.syncData.addressDiscoveryCompletedOrCanceled != nil { + close(mw.syncData.activeSyncData.addressDiscoveryCompletedOrCanceled) + mw.syncData.activeSyncData.addressDiscoveryCompletedOrCanceled = nil + } + // Cancel the context used for syncer.Run in spvSync(). mw.syncData.cancelSync() mw.syncData.mu.Lock() diff --git a/syncnotification.go b/syncnotification.go index 2fc78b6e..c635ebe3 100644 --- a/syncnotification.go +++ b/syncnotification.go @@ -190,7 +190,7 @@ func (mw *MultiWallet) discoverAddressesStarted(walletID int) { mw.syncData.mu.Lock() defer mw.syncData.mu.Unlock() - if !mw.syncData.syncing || mw.syncData.activeSyncData.addressDiscoveryCompleted != nil { + if !mw.syncData.syncing || mw.syncData.activeSyncData.addressDiscoveryCompletedOrCanceled != nil { // ignore if sync is not in progress i.e. !mw.syncData.syncing // or already started address discovery i.e. mw.syncData.activeSyncData.addressDiscoveryCompleted != nil return @@ -220,7 +220,7 @@ func (mw *MultiWallet) updateAddressDiscoveryProgress() { var lastTimeRemaining int64 var lastTotalPercent int32 = -1 - mw.syncData.addressDiscoveryCompleted = make(chan bool) + mw.syncData.addressDiscoveryCompletedOrCanceled = make(chan bool) go func() { for { @@ -290,7 +290,7 @@ func (mw *MultiWallet) updateAddressDiscoveryProgress() { } } mw.syncData.mu.Unlock() - case <-mw.syncData.addressDiscoveryCompleted: + case <-mw.syncData.addressDiscoveryCompletedOrCanceled: mw.syncData.mu.RLock() // stop updating time taken and progress for address discovery everySecondTicker.Stop() @@ -328,8 +328,8 @@ func (mw *MultiWallet) discoverAddressesFinished(walletID int) { addressDiscoveryFinishTime := time.Now().Unix() mw.syncData.activeSyncData.totalDiscoveryTimeSpent = addressDiscoveryFinishTime - mw.syncData.addressDiscoveryStartTime - close(mw.syncData.activeSyncData.addressDiscoveryCompleted) - mw.syncData.activeSyncData.addressDiscoveryCompleted = nil + close(mw.syncData.activeSyncData.addressDiscoveryCompletedOrCanceled) + mw.syncData.activeSyncData.addressDiscoveryCompletedOrCanceled = nil loadedWallet, loaded := mw.wallets[walletID].loader.LoadedWallet() if loaded { // loaded should always be through @@ -355,9 +355,9 @@ func (mw *MultiWallet) rescanStarted(walletID int) { return } - if mw.syncData.activeSyncData.addressDiscoveryCompleted != nil { - close(mw.syncData.activeSyncData.addressDiscoveryCompleted) - mw.syncData.activeSyncData.addressDiscoveryCompleted = nil + if mw.syncData.activeSyncData.addressDiscoveryCompletedOrCanceled != nil { + close(mw.syncData.activeSyncData.addressDiscoveryCompletedOrCanceled) + mw.syncData.activeSyncData.addressDiscoveryCompletedOrCanceled = nil } mw.syncData.activeSyncData.syncStage = HeadersRescanSyncStage