diff options
author | wmin0 <wmin0@hotmail.com> | 2019-01-04 11:30:42 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-01-04 11:30:42 +0800 |
commit | c5bfa29c2ad5d51147d0a5c1b725369b75da3cd8 (patch) | |
tree | 48e1f738a0a3b409176cd2212303bbb88c4d2e13 | |
parent | 4bf8e9c1cbf4b9892ff2718673735bb370e97dc5 (diff) | |
download | dexon-consensus-c5bfa29c2ad5d51147d0a5c1b725369b75da3cd8.tar.gz dexon-consensus-c5bfa29c2ad5d51147d0a5c1b725369b75da3cd8.tar.zst dexon-consensus-c5bfa29c2ad5d51147d0a5c1b725369b75da3cd8.zip |
core: agreement mgr safe spawn go routine (#396)
Set waitGroup.Add inner go routine is not safe.
You can see the example here https://play.golang.org/p/AexsKUD-4WK
-rw-r--r-- | core/agreement-mgr.go | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/core/agreement-mgr.go b/core/agreement-mgr.go index 2b5c4bc..e8cafbd 100644 --- a/core/agreement-mgr.go +++ b/core/agreement-mgr.go @@ -148,7 +148,11 @@ func (mgr *agreementMgr) run() { } mgr.isRunning = true for i := uint32(0); i < uint32(len(mgr.baModules)); i++ { - go mgr.runBA(mgr.initRound, i) + mgr.waitGroup.Add(1) + go func(idx uint32) { + defer mgr.waitGroup.Done() + mgr.runBA(mgr.initRound, idx) + }(i) } } @@ -186,7 +190,11 @@ func (mgr *agreementMgr) appendConfig( recv.agreementModule = agrModule mgr.baModules = append(mgr.baModules, agrModule) if mgr.isRunning { - go mgr.runBA(round, i) + mgr.waitGroup.Add(1) + go func(idx uint32) { + defer mgr.waitGroup.Done() + mgr.runBA(round, idx) + }(i) } } return nil @@ -277,8 +285,6 @@ func (mgr *agreementMgr) stop() { } func (mgr *agreementMgr) runBA(initRound uint64, chainID uint32) { - mgr.waitGroup.Add(1) - defer mgr.waitGroup.Done() // Acquire agreement module. agr, recv := func() (*agreement, *consensusBAReceiver) { mgr.lock.RLock() |