Skip to content

Commit

Permalink
Fix race condition in consensus.State code (#673) (#690)
Browse files Browse the repository at this point in the history
* Repro in e2e tests

* Change something in the code

* Fix race condition in `SwitchToConsensus`

* Revert "Repro in e2e tests"

This reverts commit 4f441f8ebac8f245d5a641c25ccdfa3b382ca063.

* RAII lock

(cherry picked from commit 6a96eca67fb810aa823e9de4da41cc8028637a65)

Co-authored-by: Sergio Mena <[email protected]>
  • Loading branch information
mergify[bot] and sergio-mena authored Apr 11, 2023
1 parent 2eb6994 commit e0b2aae
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions consensus/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,19 @@ func (conR *Reactor) OnStop() {
func (conR *Reactor) SwitchToConsensus(state sm.State, skipWAL bool) {
conR.Logger.Info("SwitchToConsensus")

// We have no votes, so reconstruct LastCommit from SeenCommit.
if state.LastBlockHeight > 0 {
conR.conS.reconstructLastCommit(state)
}
func() {
// We need to lock, as we are not entering consensus state from State's `handleMsg` or `handleTimeout`
conR.conS.mtx.Lock()
defer conR.conS.mtx.Unlock()
// We have no votes, so reconstruct LastCommit from SeenCommit
if state.LastBlockHeight > 0 {
conR.conS.reconstructLastCommit(state)
}

// NOTE: The line below causes broadcastNewRoundStepRoutine() to broadcast a
// NewRoundStepMessage.
conR.conS.updateToState(state)
// NOTE: The line below causes broadcastNewRoundStepRoutine() to broadcast a
// NewRoundStepMessage.
conR.conS.updateToState(state)
}()

conR.mtx.Lock()
conR.waitSync = false
Expand Down

0 comments on commit e0b2aae

Please sign in to comment.