libp2p / go-libp2p-kad-dht

A Kademlia DHT implementation on go-libp2p
https://github.com/libp2p/specs/tree/master/kad-dht
MIT License
524 stars 224 forks source link

fix: context cancel causes providers not to be returned in `findProvidersAsync` #799

Open noot opened 1 year ago

noot commented 1 year ago

I found an issue when testing the double-hashed prefix-lookup DHT; I noticed providers were being returned in the queryFn in findProvidersAsync, but they weren't being entered into the peerOut channel because the context was cancelled, resulting in no providers being returned.

Previously, context being passed to the queryFn gets cancelled when the query terminates in query.Run; however since the queryFn is run in a goroutine (query.go line 325) it's possible for the context passed to it to be cancelled before providers are put into the peerOut channel, resulting in no/less providers than expected being returned.

It seems to me like the query.ctx (which is passed by the user essentially) should be what stops values being put in the peerOut channel, which is what the PR now does (and also fixes the above issue).