Skip to content

Commit

Permalink
miner: synchronise start / stop
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
obscuren committed Oct 31, 2015
1 parent 016ad3e commit 8c38f8d
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions miner/remote_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"math/big"
"sync"
"sync/atomic"
"time"

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

hashrateMu sync.RWMutex
hashrate map[common.Hash]hashrate

running int32 // running indicates whether the agent is active. Call atomically
}

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

func (a *RemoteAgent) Start() {
if !atomic.CompareAndSwapInt32(&a.running, 0, 1) {
return
}

a.quit = make(chan struct{})
a.workCh = make(chan *Work, 1)
go a.maintainLoop()
}

func (a *RemoteAgent) Stop() {
if a.quit != nil {
close(a.quit)
}
if a.workCh != nil {
close(a.workCh)
if !atomic.CompareAndSwapInt32(&a.running, 1, 0) {
return
}

close(a.quit)
close(a.workCh)
}

// GetHashRate returns the accumulated hashrate of all identifier combined
Expand Down

0 comments on commit 8c38f8d

Please sign in to comment.