Skip to content

p2p/discover: fix expired matcher iteration in discover reply loop#20869

Merged
AskAlexSharov merged 2 commits intoerigontech:mainfrom
Sahil-4555:fix/discover-expired-matcher-iteration
Apr 30, 2026
Merged

p2p/discover: fix expired matcher iteration in discover reply loop#20869
AskAlexSharov merged 2 commits intoerigontech:mainfrom
Sahil-4555:fix/discover-expired-matcher-iteration

Conversation

@Sahil-4555
Copy link
Copy Markdown
Contributor

This update resolves the matcher cleanup loop in the discover reply handling code. When an expired matcher is removed from the list, calling Remove(el) invalidates its links, therefore invoking el.Next() subsequently may cause the loop to terminate prematurely. To avoid this, the next element is preserved before removal, allowing iteration to proceed appropriately across the entire list. This ensures that all expired matchers in the same tick are processed and that contTimeouts are not undercounted when multiple matchers expire simultaneously.

@Sahil-4555 Sahil-4555 force-pushed the fix/discover-expired-matcher-iteration branch from 515fa70 to 2eb6fdc Compare April 28, 2026 09:17
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes iteration over pending discv4 reply matchers when elements are removed during traversal, preventing premature termination of cleanup/match loops in the UDPv4 discover reply handling.

Changes:

  • Replaces direct container/list traversal in UDPv4.loop() with a removal-safe iterator helper.
  • Adds a generic iterList helper (based on iter.Seq2) that preserves the next element before yielding, so current elements can be removed safely during iteration.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
p2p/discover/v4_udp.go Uses removal-safe iteration for scheduling, reply matching, and timeout cleanup loops.
p2p/discover/common.go Adds iterList helper and required imports (container/list, iter).
Comments suppressed due to low confidence (1)

p2p/discover/v4_udp.go:510

  • This change fixes a subtle iteration/removal bug in the timeout cleanup loop. Please add a regression test (e.g., multiple matchers expiring at once) to ensure all expired matchers are processed/removed in a single timeout tick and to prevent reintroducing the premature-termination issue.
			// Notify and remove callbacks whose deadline is in the past.
			for p, el := range iterList[*replyMatcher](plist) {
				if now.After(p.deadline) || now.Equal(p.deadline) {
					p.errc <- errTimeout
					plist.Remove(el)
					contTimeouts++

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread p2p/discover/v4_udp.go
@anacrolix anacrolix self-assigned this Apr 28, 2026
@Sahil-4555 Sahil-4555 force-pushed the fix/discover-expired-matcher-iteration branch from 60a454b to 04e045f Compare April 30, 2026 04:07
@AskAlexSharov AskAlexSharov enabled auto-merge April 30, 2026 04:14
@AskAlexSharov AskAlexSharov added this pull request to the merge queue Apr 30, 2026
Merged via the queue into erigontech:main with commit fe92da2 Apr 30, 2026
36 of 37 checks passed
@Sahil-4555 Sahil-4555 deleted the fix/discover-expired-matcher-iteration branch April 30, 2026 11:26
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.

4 participants