diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-10-16 01:44:30 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-10-16 01:44:30 +0800 |
commit | cefe5c80b1cdcab606a169c0be65d9d2ba9bc941 (patch) | |
tree | dbbb116fa71ee396fdeb743a725b14007b43e845 /miner | |
parent | 2f1f2e4811a6f3094f99b55f6553fe27d83f9aad (diff) | |
parent | 402fd6e8c6a2e379351e0aae10a833fae6bcae6c (diff) | |
download | dexon-cefe5c80b1cdcab606a169c0be65d9d2ba9bc941.tar.gz dexon-cefe5c80b1cdcab606a169c0be65d9d2ba9bc941.tar.zst dexon-cefe5c80b1cdcab606a169c0be65d9d2ba9bc941.zip |
Merge pull request #1898 from karalabe/eventmux-post-race
core, eth, event, miner, xeth: fix event post / subscription race
Diffstat (limited to 'miner')
-rw-r--r-- | miner/miner.go | 2 | ||||
-rw-r--r-- | miner/worker.go | 19 |
2 files changed, 13 insertions, 8 deletions
diff --git a/miner/miner.go b/miner/miner.go index b550ed6d6..769db79d1 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -66,7 +66,7 @@ func (self *Miner) update() { events := self.mux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{}) out: for ev := range events.Chan() { - switch ev.(type) { + switch ev.Data.(type) { case downloader.StartEvent: atomic.StoreInt32(&self.canStart, 0) if self.Mining() { diff --git a/miner/worker.go b/miner/worker.go index 8be2db93e..43f6f9909 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -215,13 +215,20 @@ func (self *worker) register(agent Agent) { } func (self *worker) update() { - events := self.mux.Subscribe(core.ChainHeadEvent{}, core.ChainSideEvent{}, core.TxPreEvent{}) + eventSub := self.mux.Subscribe(core.ChainHeadEvent{}, core.ChainSideEvent{}, core.TxPreEvent{}) + defer eventSub.Unsubscribe() -out: + eventCh := eventSub.Chan() for { select { - case event := <-events.Chan(): - switch ev := event.(type) { + case event, ok := <-eventCh: + if !ok { + // Event subscription closed, set the channel to nil to stop spinning + eventCh = nil + continue + } + // A real event arrived, process interesting content + switch ev := event.Data.(type) { case core.ChainHeadEvent: self.commitNewWork() case core.ChainSideEvent: @@ -237,11 +244,9 @@ out: } } case <-self.quit: - break out + return } } - - events.Unsubscribe() } func newLocalMinedBlock(blockNumber uint64, prevMinedBlocks *uint64RingBuffer) (minedBlocks *uint64RingBuffer) { |