funding: add assurance select to ensure goroutines exit on quit

This commit is contained in:
Olaoluwa Osuntokun 2017-02-24 16:24:10 -08:00
parent cef81a6adf
commit 02b7b911fb
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2

@ -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
}