From 343cd7779fc4631247221135c4012c1d584dd2d1 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 15 Dec 2016 13:55:15 -0800 Subject: [PATCH] utxonursery: use a two value read from the epoch chan for graceful shutdown Within the utoxNursery we now use a two value read fro the block epoch channel in order to exit early if/when the ChainNotifier begins the shutdown process before the signal reaches the utxoNursery. --- utxonursery.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/utxonursery.go b/utxonursery.go index fd166bfa7..f09ecba23 100644 --- a/utxonursery.go +++ b/utxonursery.go @@ -96,6 +96,8 @@ func (u *utxoNursery) Stop() error { // Once an output is mature, it will be swept into the wallet at the earlier // possible height. func (u *utxoNursery) incubator() { + defer u.wg.Done() + // Register with the notifier to receive notifications for each newly // connected block. newBlocks, err := u.notifier.RegisterBlockEpochNtfn() @@ -163,7 +165,15 @@ out: utxnLog.Infof("Outpoint %v now mid-stage, will mature "+ "at height %v (delay of %v)", midUtxo.outPoint, maturityHeight, midUtxo.blocksToMaturity) - case epoch := <-newBlocks.Epochs: + case epoch, ok := <-newBlocks.Epochs: + // If the epoch channel has been closed, then the + // ChainNotifier is exiting which means the daemon is + // as well. Therefore, we exit early also in order to + // ensure the daemon shutsdown gracefully, yet swiftly. + if !ok { + return + } + // A new block has just been connected, check to see if // we have any new outputs that can be swept into the // wallet. @@ -210,8 +220,6 @@ out: break out } } - - u.wg.Done() } // createSweepTx creates a final sweeping transaction with all witnesses in