p2p/discover: fix expired matcher iteration in discover reply loop#20869
Merged
AskAlexSharov merged 2 commits intoerigontech:mainfrom Apr 30, 2026
Merged
Conversation
515fa70 to
2eb6fdc
Compare
AskAlexSharov
approved these changes
Apr 28, 2026
Contributor
There was a problem hiding this comment.
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/listtraversal inUDPv4.loop()with a removal-safe iterator helper. - Adds a generic
iterListhelper (based oniter.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.
60a454b to
04e045f
Compare
AskAlexSharov
approved these changes
Apr 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 invokingel.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 thatcontTimeoutsare not undercounted when multiple matchers expire simultaneously.