mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 19:00:46 +00:00
p2p/discover: fix timeout loop early exit when removing expired matchers (#34743)
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. --------- Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
parent
822e7c6486
commit
51c97216c5
2 changed files with 19 additions and 7 deletions
|
|
@ -17,9 +17,11 @@
|
||||||
package discover
|
package discover
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"container/list"
|
||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
crand "crypto/rand"
|
crand "crypto/rand"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"iter"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
|
@ -143,3 +145,16 @@ func (r *reseedingRandom) Shuffle(n int, swap func(i, j int)) {
|
||||||
defer r.mu.Unlock()
|
defer r.mu.Unlock()
|
||||||
r.cur.Shuffle(n, swap)
|
r.cur.Shuffle(n, swap)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// iterList iterates over the elements of the given list.
|
||||||
|
func iterList[T any](l *list.List) iter.Seq2[T, *list.Element] {
|
||||||
|
return func(yield func(T, *list.Element) bool) {
|
||||||
|
for el := l.Front(); el != nil; {
|
||||||
|
next := el.Next()
|
||||||
|
if !yield(el.Value.(T), el) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
el = next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -446,9 +446,8 @@ func (t *UDPv4) loop() {
|
||||||
}
|
}
|
||||||
// Start the timer so it fires when the next pending reply has expired.
|
// Start the timer so it fires when the next pending reply has expired.
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
for el := plist.Front(); el != nil; el = el.Next() {
|
for p, el := range iterList[*replyMatcher](plist) {
|
||||||
nextTimeout = el.Value.(*replyMatcher)
|
if dist := p.deadline.Sub(now); dist < 2*respTimeout {
|
||||||
if dist := nextTimeout.deadline.Sub(now); dist < 2*respTimeout {
|
|
||||||
timeout.Reset(dist)
|
timeout.Reset(dist)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -478,8 +477,7 @@ func (t *UDPv4) loop() {
|
||||||
|
|
||||||
case r := <-t.gotreply:
|
case r := <-t.gotreply:
|
||||||
var matched bool // whether any replyMatcher considered the reply acceptable.
|
var matched bool // whether any replyMatcher considered the reply acceptable.
|
||||||
for el := plist.Front(); el != nil; el = el.Next() {
|
for p, el := range iterList[*replyMatcher](plist) {
|
||||||
p := el.Value.(*replyMatcher)
|
|
||||||
if p.from == r.from && p.ptype == r.data.Kind() && p.ip == r.ip {
|
if p.from == r.from && p.ptype == r.data.Kind() && p.ip == r.ip {
|
||||||
ok, requestDone := p.callback(r.data)
|
ok, requestDone := p.callback(r.data)
|
||||||
matched = matched || ok
|
matched = matched || ok
|
||||||
|
|
@ -499,8 +497,7 @@ 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 p, el := range iterList[*replyMatcher](plist) {
|
||||||
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)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue