mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-03 03:31:14 +00:00
cmd/devp2p: close the iterator in discv4 LookupRandom (#35206)
The LookupRandom RPC handler obtains a node iterator from RandomNodes() but never closes it. RandomNodes() returns a lookupIterator backed by a cancelable context derived from the listener's lifetime context (newLookupIterator -> context.WithCancel). The iterator's Close() is what calls the cancel func, so each LookupRandom call that never closes leaks the cancel func (and any in-flight lookup goroutine) until the discv4 listener shuts down. When the listener serves the RPC API (discv4 --rpc) it is long-lived, so repeated LookupRandom calls accumulate the leak. Close the iterator when the handler returns. This matches how the crawler already releases the same RandomNodes() iterators (crawl.go closes every iterator it consumes).
This commit is contained in:
parent
f9417bb279
commit
7aa7806c09
1 changed files with 1 additions and 0 deletions
|
|
@ -430,6 +430,7 @@ type discv4API struct {
|
|||
|
||||
func (api *discv4API) LookupRandom(n int) (ns []*enode.Node) {
|
||||
it := api.host.RandomNodes()
|
||||
defer it.Close()
|
||||
for len(ns) < n && it.Next() {
|
||||
ns = append(ns, it.Node())
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue