From 419c2ac2060e4bccd7cbb9027d404b9a54f42853 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Tue, 4 Apr 2017 14:20:07 +0200 Subject: [PATCH] chainntnfs/btcdnotify: fix race condition for block epoch clients MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit fixes a race condition that was introduced while fixing a lingering bug in the logic to notify block epoch clients. The race condition would happen as by removing the default case in the select statement, it was now possible for the client’s block epoch client to be closed while the routine was attempting a send on it. We now eliminate this race condition possibility by adding a wait group to all goroutines launched to dispatch a block epoch notification. With this modification, the Stop() goroutine will now wait for all other goroutine to exit before closing the block epoch channels of all currently registered clients. --- chainntnfs/btcdnotify/btcd.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/chainntnfs/btcdnotify/btcd.go b/chainntnfs/btcdnotify/btcd.go index 8b229e5b..0bfb2fc0 100644 --- a/chainntnfs/btcdnotify/btcd.go +++ b/chainntnfs/btcdnotify/btcd.go @@ -476,7 +476,10 @@ func (b *BtcdNotifier) notifyBlockEpochs(newHeight int32, newSha *chainhash.Hash } for _, epochChan := range b.blockEpochClients { + b.wg.Add(1) go func(ntfnChan chan *chainntnfs.BlockEpoch) { + defer b.wg.Done() + select { case ntfnChan <- epoch: case <-b.quit: