Skip to content

Commit fb62272

Browse files
authored
Merge pull request #329 from libp2p/fix/goroutine-leak-empty-routing
query: fix a goroutine leak when the routing table is empty
2 parents 691b1e5 + 1cccee0 commit fb62272

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

query.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
todoctr "github.com/ipfs/go-todocounter"
1010
process "github.com/jbenet/goprocess"
1111
ctxproc "github.com/jbenet/goprocess/context"
12+
kb "github.com/libp2p/go-libp2p-kbucket"
1213
inet "github.com/libp2p/go-libp2p-net"
1314
peer "github.com/libp2p/go-libp2p-peer"
1415
pset "github.com/libp2p/go-libp2p-peer/peerset"
@@ -58,6 +59,11 @@ type queryFunc func(context.Context, peer.ID) (*dhtQueryResult, error)
5859

5960
// Run runs the query at hand. pass in a list of peers to use first.
6061
func (q *dhtQuery) Run(ctx context.Context, peers []peer.ID) (*dhtQueryResult, error) {
62+
if len(peers) == 0 {
63+
logger.Warning("Running query with no peers!")
64+
return nil, kb.ErrLookupFailure
65+
}
66+
6167
select {
6268
case <-ctx.Done():
6369
return nil, ctx.Err()
@@ -121,11 +127,6 @@ func (r *dhtQueryRunner) Run(ctx context.Context, peers []peer.ID) (*dhtQueryRes
121127
r.log = logger
122128
r.runCtx = ctx
123129

124-
if len(peers) == 0 {
125-
logger.Warning("Running query with no peers!")
126-
return nil, nil
127-
}
128-
129130
// setup concurrency rate limiting
130131
for i := 0; i < r.query.concurrency; i++ {
131132
r.rateLimit <- struct{}{}

routing.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,15 @@ func (dht *IpfsDHT) findProvidersAsyncRoutine(ctx context.Context, key cid.Cid,
495495
}
496496
}
497497

498+
peers := dht.routingTable.NearestPeers(kb.ConvertKey(key.KeyString()), AlphaValue)
499+
if len(peers) == 0 {
500+
notif.PublishQueryEvent(ctx, &notif.QueryEvent{
501+
Type: notif.QueryError,
502+
Extra: kb.ErrLookupFailure.Error(),
503+
})
504+
return
505+
}
506+
498507
// setup the Query
499508
parent := ctx
500509
query := dht.newQuery(key.KeyString(), func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
@@ -545,7 +554,6 @@ func (dht *IpfsDHT) findProvidersAsyncRoutine(ctx context.Context, key cid.Cid,
545554
return &dhtQueryResult{closerPeers: clpeers}, nil
546555
})
547556

548-
peers := dht.routingTable.NearestPeers(kb.ConvertKey(key.KeyString()), AlphaValue)
549557
_, err := query.Run(ctx, peers)
550558
if err != nil {
551559
logger.Debugf("Query error: %s", err)

0 commit comments

Comments
 (0)