Skip to content

Commit

Permalink
sync: close account discovery channel before canceling sync (#87)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
beansgum authored Jan 17, 2020
1 parent 90ae17f commit d4d67d9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
7 changes: 6 additions & 1 deletion sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type activeSyncData struct {
addressDiscoveryStartTime int64
totalDiscoveryTimeSpent int64

addressDiscoveryCompleted chan bool
addressDiscoveryCompletedOrCanceled chan bool

rescanStartTime int64

Expand Down Expand Up @@ -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()
Expand Down
16 changes: 8 additions & 8 deletions syncnotification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit d4d67d9

Please sign in to comment.