Skip to content

Commit 28b13a4

Browse files
committed
Merge pull request #1780 from bas-vk/miner-crash
agent/miner Prevent the CpuAgent to be started multiple times
2 parents edaea69 + 652eea7 commit 28b13a4

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

miner/agent.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/ethereum/go-ethereum/logger"
2424
"github.com/ethereum/go-ethereum/logger/glog"
2525
"github.com/ethereum/go-ethereum/pow"
26+
"sync/atomic"
2627
)
2728

2829
type CpuAgent struct {
@@ -35,6 +36,8 @@ type CpuAgent struct {
3536

3637
index int
3738
pow pow.PoW
39+
40+
isMining int32 // isMining indicates whether the agent is currently mining
3841
}
3942

4043
func NewCpuAgent(index int, pow pow.PoW) *CpuAgent {
@@ -60,6 +63,10 @@ func (self *CpuAgent) Stop() {
6063
func (self *CpuAgent) Start() {
6164
self.mu.Lock()
6265
defer self.mu.Unlock()
66+
67+
if !atomic.CompareAndSwapInt32(&self.isMining, 0, 1) {
68+
return // agent already started
69+
}
6370

6471
self.quit = make(chan struct{})
6572
// creating current op ch makes sure we're not closing a nil ch
@@ -99,10 +106,11 @@ done:
99106
case <-self.workCh:
100107
default:
101108
close(self.workCh)
102-
103109
break done
104110
}
105111
}
112+
113+
atomic.StoreInt32(&self.isMining, 0)
106114
}
107115

108116
func (self *CpuAgent) mine(work *Work, stop <-chan struct{}) {

0 commit comments

Comments
 (0)