Skip to content

Commit

Permalink
perf(consensus): Fix some peer messages taking write locks on the cs …
Browse files Browse the repository at this point in the history
…mtx (cometbft#3159)

Some clear, read-only spots in receive routine took Write locks instead
of read locks. Seems like a bug.

This PR fixes it.

---

#### PR checklist

- [x] Tests written/updated - N/A imo?
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [x] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [x] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec
  • Loading branch information
ValarDragon committed Jun 3, 2024
1 parent 20e26bd commit a0a6a63
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- [`consensus`] Fix some reactor messages taking write locks instead of read locks.
([\#3159](https://github.com/cometbft/cometbft/issues/3159)
12 changes: 6 additions & 6 deletions consensus/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ func (conR *Reactor) ReceiveEnvelope(e p2p.Envelope) {
case StateChannel:
switch msg := msg.(type) {
case *NewRoundStepMessage:
conR.conS.mtx.Lock()
conR.conS.mtx.RLock()
initialHeight := conR.conS.state.InitialHeight
conR.conS.mtx.Unlock()
conR.conS.mtx.RUnlock()
if err = msg.ValidateHeight(initialHeight); err != nil {
conR.Logger.Error("Peer sent us invalid msg", "peer", e.Src, "msg", msg, "err", err)
conR.Switch.StopPeerForError(e.Src, err)
Expand All @@ -277,9 +277,9 @@ func (conR *Reactor) ReceiveEnvelope(e p2p.Envelope) {
ps.ApplyHasVoteMessage(msg)
case *VoteSetMaj23Message:
cs := conR.conS
cs.mtx.Lock()
cs.mtx.RLock()
height, votes := cs.Height, cs.Votes
cs.mtx.Unlock()
cs.mtx.RUnlock()
if height != msg.Height {
return
}
Expand Down Expand Up @@ -364,9 +364,9 @@ func (conR *Reactor) ReceiveEnvelope(e p2p.Envelope) {
switch msg := msg.(type) {
case *VoteSetBitsMessage:
cs := conR.conS
cs.mtx.Lock()
cs.mtx.RLock()
height, votes := cs.Height, cs.Votes
cs.mtx.Unlock()
cs.mtx.RUnlock()

if height == msg.Height {
var ourVotes *bits.BitArray
Expand Down

0 comments on commit a0a6a63

Please sign in to comment.