diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-05-18 22:33:06 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-05-18 22:33:06 +0800 |
commit | 677796b3517782dd530b7a1a7d9dfd16b643404c (patch) | |
tree | 4224cc173d273990b3007586b4a5b73403593c99 | |
parent | 67d44519ce4344d5ee5f7cf37dd785ead70e8453 (diff) | |
parent | 60561cdca26b84843ddb4d96158c6ca7d8c32ec1 (diff) | |
download | go-tangerine-677796b3517782dd530b7a1a7d9dfd16b643404c.tar.gz go-tangerine-677796b3517782dd530b7a1a7d9dfd16b643404c.tar.zst go-tangerine-677796b3517782dd530b7a1a7d9dfd16b643404c.zip |
Merge pull request #1031 from bas-vk/issue1012
fixed race condition in miner
-rw-r--r-- | miner/agent.go | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/miner/agent.go b/miner/agent.go index da2a2008d..3ed3ba839 100644 --- a/miner/agent.go +++ b/miner/agent.go @@ -40,7 +40,6 @@ func (self *CpuAgent) Stop() { defer self.mu.Unlock() close(self.quit) - close(self.quitCurrentOp) } func (self *CpuAgent) Start() { @@ -50,7 +49,6 @@ func (self *CpuAgent) Start() { self.quit = make(chan struct{}) // creating current op ch makes sure we're not closing a nil ch // later on - self.quitCurrentOp = make(chan struct{}) self.workCh = make(chan *types.Block, 1) go self.update() @@ -62,11 +60,19 @@ out: select { case block := <-self.workCh: self.mu.Lock() - close(self.quitCurrentOp) + if self.quitCurrentOp != nil { + close(self.quitCurrentOp) + } + self.quitCurrentOp = make(chan struct{}) + go self.mine(block, self.quitCurrentOp) self.mu.Unlock() - - go self.mine(block) case <-self.quit: + self.mu.Lock() + if self.quitCurrentOp != nil { + close(self.quitCurrentOp) + self.quitCurrentOp = nil + } + self.mu.Unlock() break out } } @@ -84,16 +90,11 @@ done: } } -func (self *CpuAgent) mine(block *types.Block) { +func (self *CpuAgent) mine(block *types.Block, stop <- chan struct{}) { glog.V(logger.Debug).Infof("(re)started agent[%d]. mining...\n", self.index) - // Reset the channel - self.mu.Lock() - self.quitCurrentOp = make(chan struct{}) - self.mu.Unlock() - // Mine - nonce, mixDigest := self.pow.Search(block, self.quitCurrentOp) + nonce, mixDigest := self.pow.Search(block, stop) if nonce != 0 { block.SetNonce(nonce) block.Header().MixDigest = common.BytesToHash(mixDigest) |