From 02b7b911fb0bea1f19e7e8d3a614c2e964697bf3 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Fri, 24 Feb 2017 16:24:10 -0800 Subject: [PATCH] funding: add assurance select to ensure goroutines exit on quit --- fundingmanager.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/fundingmanager.go b/fundingmanager.go index fa5cfb3e..6105c5c3 100644 --- a/fundingmanager.go +++ b/fundingmanager.go @@ -837,7 +837,11 @@ func (f *fundingManager) handleFundingSignComplete(fmsg *fundingSignCompleteMsg) doneChan := make(chan struct{}) go f.waitForFundingConfirmation(completeChan, doneChan) - <-doneChan + select { + case <-f.quit: + return + case <-doneChan: + } // Finally give the caller a final update notifying them that // the channel is now open. @@ -865,6 +869,8 @@ func (f *fundingManager) handleFundingSignComplete(fmsg *fundingSignCompleteMsg) func (f *fundingManager) waitForFundingConfirmation( completeChan *channeldb.OpenChannel, doneChan chan struct{}) { + defer close(doneChan) + // Register with the ChainNotifier for a notification once the funding // transaction reaches `numConfs` confirmations. txid := completeChan.FundingOutpoint.Hash @@ -916,7 +922,13 @@ func (f *fundingManager) waitForFundingConfirmation( } peer.newChannels <- newChanMsg - <-newChanDone + // We pause here to wait for the peer to recognize the new channel + // before we close the channel barrier corresponding to the channel. + select { + case <-f.quit: + return + case <-newChanDone: // Fallthrough if we're not quitting. + } // Close the active channel barrier signalling the // readHandler that commitment related modifications to @@ -948,7 +960,6 @@ func (f *fundingManager) waitForFundingConfirmation( f.cfg.IDKey) f.cfg.SendToPeer(completeChan.IdentityPub, fundingLockedMsg) - close(doneChan) return }