Skip to content

Commit

Permalink
fix(dht/routing) buffer promise response to prevent resource leak
Browse files Browse the repository at this point in the history
When performing this "promise" pattern, it is important to
provide a
channel with space for one value. Otherwise the sender may
block forever
in the case of a receiver that decides to abandon the
request. A subtle
detail, but one that is important for avoiding
leaked goroutines.

cc @whyrusleeping @jbenet

License: MIT
Signed-off-by: Brian Tiger Chow
<brian@perfmode.com>

License: MIT
Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
  • Loading branch information
Brian Tiger Chow committed Dec 2, 2014
1 parent 428d766 commit dec6dad
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions routing/dht/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ func (pm *ProviderManager) AddProvider(k u.Key, val peer.Peer) {
}

func (pm *ProviderManager) GetProviders(ctx context.Context, k u.Key) []peer.Peer {
gp := new(getProv)
gp.k = k
gp.resp = make(chan []peer.Peer)
gp := &getProv{
k: k,
resp: make(chan []peer.Peer, 1), // buffered to prevent sender from blocking
}
select {
case pm.getprovs <- gp:
return <-gp.resp
Expand Down

0 comments on commit dec6dad

Please sign in to comment.