cmd/devp2p: close the iterator in discv4 LookupRandom (#35206)
Some checks are pending
/ Linux Build (push) Waiting to run
/ Linux Build (arm) (push) Waiting to run
/ Keeper Build (push) Waiting to run
/ Windows Build (push) Waiting to run
/ Docker Image (push) Waiting to run

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:
ozpool 2026-07-02 21:50:25 +05:30 committed by GitHub
parent f9417bb279
commit 7aa7806c09
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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())
}