Skip to content

Commit

Permalink
Avoided RLock being taken twice for same mutex variable of same host …
Browse files Browse the repository at this point in the history
…which leads to deadlock if any WLock is requested inbetween two RLock by other routine (apache#945)

* Avoided RLock being taken twice for same mutex variable of same host which leads to deadlock if any WLock is requested inbetween two RLock by other routine

* Author list updated

* enberg code review comments: comments corrected
  • Loading branch information
parsaila authored and Zariel committed Aug 4, 2017
1 parent db941d9 commit b96c067
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,4 @@ Raphael Gavache <raphael.gavache@gmail.com>
Yasser Abdolmaleki <yasser@yasser.ca>
Krishnanand Thommandra <devtkrishna@gmail.com>
Blake Atkinson <me@blakeatkinson.com>
Dharmendra Parsaila <d4dharmu@gmail.com>
10 changes: 8 additions & 2 deletions host_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,14 @@ type HostInfo struct {
func (h *HostInfo) Equal(host *HostInfo) bool {
h.mu.RLock()
defer h.mu.RUnlock()
host.mu.RLock()
defer host.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()
}

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

0 comments on commit b96c067

Please sign in to comment.