Browse Source

consensus: avoid race in accessing channel

pull/8149/head
tycho garen 3 years ago
parent
commit
59d5f9cae5
2 changed files with 10 additions and 2 deletions
  1. +2
    -1
      internal/consensus/reactor.go
  2. +8
    -1
      internal/consensus/state.go

+ 2
- 1
internal/consensus/reactor.go View File

@ -348,6 +348,7 @@ func (r *Reactor) broadcastHasVoteMessage(ctx context.Context, vote *types.Vote)
// internal pubsub defined in the consensus state to broadcast them to peers
// upon receiving.
func (r *Reactor) subscribeToBroadcastEvents() {
onStopCh := r.state.getOnStopCh()
err := r.state.evsw.AddListenerForEvent(
listenerIDConsensus,
types.EventNewRoundStepValue,
@ -356,7 +357,7 @@ func (r *Reactor) subscribeToBroadcastEvents() {
return err
}
select {
case r.state.onStopCh <- data.(*cstypes.RoundState):
case onStopCh <- data.(*cstypes.RoundState):
return nil
case <-ctx.Done():
return ctx.Err()


+ 8
- 1
internal/consensus/state.go View File

@ -493,12 +493,19 @@ func (cs *State) loadWalFile(ctx context.Context) error {
return nil
}
func (cs *State) getOnStopCh() chan *cstypes.RoundState {
cs.mtx.RLock()
defer cs.mtx.RUnlock()
return r.state.onStopCh
}
// OnStop implements service.Service.
func (cs *State) OnStop() {
// If the node is committing a new block, wait until it is finished!
if cs.GetRoundState().Step == cstypes.RoundStepCommit {
select {
case <-cs.onStopCh:
case <-cs.getOnStopCh():
case <-time.After(cs.config.TimeoutCommit):
cs.logger.Error("OnStop: timeout waiting for commit to finish", "time", cs.config.TimeoutCommit)
}


Loading…
Cancel
Save