Skip to content

Commit

Permalink
hostinfo: fix rlock reentry (apache#967)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zariel authored Sep 7, 2017
1 parent 066e974 commit 605a5e0
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions host_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,9 @@ type HostInfo struct {
}

func (h *HostInfo) Equal(host *HostInfo) bool {
h.mu.RLock()
defer h.mu.RUnlock()
//If both hosts pointers are same then lock is required only once because of below reasons:
//Reason 1: There is no point taking lock twice on same mutex variable.
//Reason 2: It may lead to deadlock e.g. if WLock is requested by other routine in between 1st & 2nd RLock
//So WLock will be blocked on 1st RLock and 2nd RLock will be blocked on requested WLock.
if h != host {
host.mu.RLock()
defer host.mu.RUnlock()
if h == host {
// prevent rlock reentry
return true
}

return h.ConnectAddress().Equal(host.ConnectAddress())
Expand Down

0 comments on commit 605a5e0

Please sign in to comment.