Skip to content

cmd/devp2p: close the iterator in discv4 LookupRandom - #35206

Merged
fjl merged 1 commit into
ethereum:masterfrom
ozpool:fix/discv4-lookuprandom-iterator-leak
Jul 2, 2026
Merged

fjl merged 1 commit into
ethereum:masterfrom
ozpool:fix/discv4-lookuprandom-iterator-leak

Conversation

@ozpool

@ozpool ozpool commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

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).

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, so each
call leaks the cancel func (and any in-flight lookup) until the discv4
listener shuts down. When the listener serves the RPC API it is
long-lived, so repeated LookupRandom calls accumulate the leak.

Close the iterator when the handler returns, matching how the crawler
releases the same RandomNodes iterators.
@fjl fjl self-assigned this Jun 30, 2026
@fjl
fjl merged commit 7aa7806 into ethereum:master Jul 2, 2026
9 checks passed
@fjl fjl added this to the 1.17.5 milestone Jul 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants