p2p/discover: fix timeout loop early exit when removing expired matchers

Save `el.Next()` before calling `plist.Remove(el)` so iteration continues correctly. Previously the loop exited after removing the first expired matcher because `Remove` invalidates the element's links.
This commit is contained in:
Tanvir 2026-04-17 03:23:22 +08:00
parent fc43170cdd
commit a37824d1f8

View file

@ -499,13 +499,15 @@ func (t *UDPv4) loop() {
nextTimeout = nil nextTimeout = nil
// Notify and remove callbacks whose deadline is in the past. // Notify and remove callbacks whose deadline is in the past.
for el := plist.Front(); el != nil; el = el.Next() { for el := plist.Front(); el != nil; {
next := el.Next()
p := el.Value.(*replyMatcher) p := el.Value.(*replyMatcher)
if now.After(p.deadline) || now.Equal(p.deadline) { if now.After(p.deadline) || now.Equal(p.deadline) {
p.errc <- errTimeout p.errc <- errTimeout
plist.Remove(el) plist.Remove(el)
contTimeouts++ contTimeouts++
} }
el = next
} }
// If we've accumulated too many timeouts, do an NTP time sync check // If we've accumulated too many timeouts, do an NTP time sync check
if contTimeouts > ntpFailureThreshold { if contTimeouts > ntpFailureThreshold {