Skip to content

Commit 8c38f8d

Browse files
committed
miner: synchronise start / stop
This PR fixes an issue where the remote worker was stopped twice and not properly handled. This adds a synchronised running check to the start and stop methods preventing closing of a channel more than once.
1 parent 016ad3e commit 8c38f8d

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

miner/remote_agent.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"errors"
2121
"math/big"
2222
"sync"
23+
"sync/atomic"
2324
"time"
2425

2526
"github.com/ethereum/ethash"
@@ -45,6 +46,8 @@ type RemoteAgent struct {
4546

4647
hashrateMu sync.RWMutex
4748
hashrate map[common.Hash]hashrate
49+
50+
running int32 // running indicates whether the agent is active. Call atomically
4851
}
4952

5053
func NewRemoteAgent() *RemoteAgent {
@@ -70,18 +73,22 @@ func (a *RemoteAgent) SetReturnCh(returnCh chan<- *Result) {
7073
}
7174

7275
func (a *RemoteAgent) Start() {
76+
if !atomic.CompareAndSwapInt32(&a.running, 0, 1) {
77+
return
78+
}
79+
7380
a.quit = make(chan struct{})
7481
a.workCh = make(chan *Work, 1)
7582
go a.maintainLoop()
7683
}
7784

7885
func (a *RemoteAgent) Stop() {
79-
if a.quit != nil {
80-
close(a.quit)
81-
}
82-
if a.workCh != nil {
83-
close(a.workCh)
86+
if !atomic.CompareAndSwapInt32(&a.running, 1, 0) {
87+
return
8488
}
89+
90+
close(a.quit)
91+
close(a.workCh)
8592
}
8693

8794
// GetHashRate returns the accumulated hashrate of all identifier combined

0 commit comments

Comments
 (0)