From 1db41663c7025a6c87b698108239c9ceb081a15c Mon Sep 17 00:00:00 2001 From: Sam Kleinman Date: Fri, 18 Mar 2022 09:54:40 -0400 Subject: [PATCH] consensus: avoid race in accessing channel (#8149) --- internal/consensus/reactor.go | 4 +++- internal/consensus/state.go | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/consensus/reactor.go b/internal/consensus/reactor.go index 822e8c627..52e547bf6 100644 --- a/internal/consensus/reactor.go +++ b/internal/consensus/reactor.go @@ -348,6 +348,8 @@ 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 +358,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() diff --git a/internal/consensus/state.go b/internal/consensus/state.go index 7b48aa3bc..109e90683 100644 --- a/internal/consensus/state.go +++ b/internal/consensus/state.go @@ -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 cs.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) }