Skip to content

Commit

Permalink
storage: avoid deadlock due to reentrant RLock
Browse files Browse the repository at this point in the history
Happened to run into this during some unrelated work.

Release note: None
  • Loading branch information
tbg committed Apr 5, 2019
1 parent a04c831 commit d74f8c8
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/storage/replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,9 +864,15 @@ func (r *Replica) raftStatusRLocked() *raft.Status {
// State returns a copy of the internal state of the Replica, along with some
// auxiliary information.
func (r *Replica) State() storagepb.RangeInfo {
var ri storagepb.RangeInfo

// NB: this acquires an RLock(). Reentrant RLocks are deadlock prone, so do
// this first before RLocking below. Performance of this extra lock
// acquisition is not a concern.
ri.ActiveClosedTimestamp = r.maxClosed(context.Background())

r.mu.RLock()
defer r.mu.RUnlock()
var ri storagepb.RangeInfo
ri.ReplicaState = *(protoutil.Clone(&r.mu.state)).(*storagepb.ReplicaState)
ri.LastIndex = r.mu.lastIndex
ri.NumPending = uint64(len(r.mu.proposals))
Expand All @@ -893,7 +899,6 @@ func (r *Replica) State() storagepb.RangeInfo {
})
}
}
ri.ActiveClosedTimestamp = r.maxClosed(context.Background())
return ri
}

Expand Down

0 comments on commit d74f8c8

Please sign in to comment.