Skip to content

Commit

Permalink
Merge pull request #656 from libp2p/fix/context-cancel-bug
Browse files Browse the repository at this point in the history
fix: lookup context cancellation race condition
  • Loading branch information
Stebalien authored May 27, 2020
2 parents 3d294c7 + 1dbefd3 commit 98c5089
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,12 @@ func (dht *IpfsDHT) runLookupWithFollowup(ctx context.Context, target string, qu
}

// wait for all queries to complete before returning, aborting ongoing queries if we've been externally stopped
followupsCompleted := 0
processFollowUp:
for i := 0; i < len(queryPeers); i++ {
select {
case <-doneCh:
followupsCompleted++
if stopFn() {
cancelFollowUp()
if i < len(queryPeers)-1 {
Expand All @@ -130,10 +132,17 @@ processFollowUp:
}
case <-ctx.Done():
lookupRes.completed = false
cancelFollowUp()
break processFollowUp
}
}

if !lookupRes.completed {
for i := followupsCompleted; i < len(queryPeers); i++ {
<-doneCh
}
}

return lookupRes, nil
}

Expand Down

0 comments on commit 98c5089

Please sign in to comment.