Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

query: fix a goroutine leak when the routing table is empty #329

Merged
merged 1 commit into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
todoctr "github.com/ipfs/go-todocounter"
process "github.com/jbenet/goprocess"
ctxproc "github.com/jbenet/goprocess/context"
kb "github.com/libp2p/go-libp2p-kbucket"
inet "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
pset "github.com/libp2p/go-libp2p-peer/peerset"
Expand Down Expand Up @@ -58,6 +59,11 @@ type queryFunc func(context.Context, peer.ID) (*dhtQueryResult, error)

// Run runs the query at hand. pass in a list of peers to use first.
func (q *dhtQuery) Run(ctx context.Context, peers []peer.ID) (*dhtQueryResult, error) {
if len(peers) == 0 {
logger.Warning("Running query with no peers!")
return nil, kb.ErrLookupFailure
}

select {
case <-ctx.Done():
return nil, ctx.Err()
Expand Down Expand Up @@ -121,11 +127,6 @@ func (r *dhtQueryRunner) Run(ctx context.Context, peers []peer.ID) (*dhtQueryRes
r.log = logger
r.runCtx = ctx

if len(peers) == 0 {
logger.Warning("Running query with no peers!")
return nil, nil
}

// setup concurrency rate limiting
for i := 0; i < r.query.concurrency; i++ {
r.rateLimit <- struct{}{}
Expand Down
10 changes: 9 additions & 1 deletion routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,15 @@ func (dht *IpfsDHT) findProvidersAsyncRoutine(ctx context.Context, key cid.Cid,
}
}

peers := dht.routingTable.NearestPeers(kb.ConvertKey(key.KeyString()), AlphaValue)
if len(peers) == 0 {
notif.PublishQueryEvent(ctx, &notif.QueryEvent{
Type: notif.QueryError,
Extra: kb.ErrLookupFailure.Error(),
})
return
}

// setup the Query
parent := ctx
query := dht.newQuery(key.KeyString(), func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
Expand Down Expand Up @@ -545,7 +554,6 @@ func (dht *IpfsDHT) findProvidersAsyncRoutine(ctx context.Context, key cid.Cid,
return &dhtQueryResult{closerPeers: clpeers}, nil
})

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