eth: avoid blocking when geth is closed, fix hang in waitSnapExtension

This commit is contained in:
niuxiaojie81 2024-01-10 11:46:23 +08:00
parent 488c5d27d8
commit 14a2915930

View file

@ -21,7 +21,6 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"sync" "sync"
"time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/eth/protocols/eth" "github.com/ethereum/go-ethereum/eth/protocols/eth"
@ -29,11 +28,6 @@ import (
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
) )
const (
// snapWaitTimeout is the amount of time to wait for the snap protocol to be started.
snapWaitTimeout = 5 * time.Second
)
var ( var (
// errPeerSetClosed is returned if a peer is attempted to be added or removed // errPeerSetClosed is returned if a peer is attempted to be added or removed
// from the peer set after it has been terminated. // from the peer set after it has been terminated.
@ -50,9 +44,6 @@ var (
// errSnapWithoutEth is returned if a peer attempts to connect only on the // errSnapWithoutEth is returned if a peer attempts to connect only on the
// snap protocol without advertising the eth main protocol. // snap protocol without advertising the eth main protocol.
errSnapWithoutEth = errors.New("peer connected on snap without compatible eth support") errSnapWithoutEth = errors.New("peer connected on snap without compatible eth support")
// errSnapTimeout is returned if the peer takes too long to start the snap protocol.
errSnapTimeout = errors.New("peer timeout starting snap protocol")
) )
// peerSet represents the collection of active peers currently participating in // peerSet represents the collection of active peers currently participating in
@ -66,6 +57,7 @@ type peerSet struct {
lock sync.RWMutex lock sync.RWMutex
closed bool closed bool
quitCh chan struct{} // Quit channel to signal termination
} }
// newPeerSet creates a new peer set to track the active participants. // newPeerSet creates a new peer set to track the active participants.
@ -74,6 +66,7 @@ func newPeerSet() *peerSet {
peers: make(map[string]*ethPeer), peers: make(map[string]*ethPeer),
snapWait: make(map[string]chan *snap.Peer), snapWait: make(map[string]chan *snap.Peer),
snapPend: make(map[string]*snap.Peer), snapPend: make(map[string]*snap.Peer),
quitCh: make(chan struct{}),
} }
} }
@ -138,19 +131,15 @@ func (ps *peerSet) waitSnapExtension(peer *eth.Peer) (*snap.Peer, error) {
ps.snapWait[id] = wait ps.snapWait[id] = wait
ps.lock.Unlock() ps.lock.Unlock()
t := time.NewTicker(snapWaitTimeout)
defer t.Stop()
for { for {
select { select {
case p := <-wait: case p := <-wait:
return p, nil return p, nil
case <-t.C: case <-ps.quitCh:
if ps.closed { ps.lock.Lock()
ps.lock.Lock() delete(ps.snapWait, id)
delete(ps.snapWait, id) ps.lock.Unlock()
ps.lock.Unlock() return nil, errPeerSetClosed
return nil, errSnapTimeout
}
} }
} }
} }
@ -279,5 +268,6 @@ func (ps *peerSet) close() {
for _, p := range ps.peers { for _, p := range ps.peers {
p.Disconnect(p2p.DiscQuitting) p.Disconnect(p2p.DiscQuitting)
} }
close(ps.quitCh)
ps.closed = true ps.closed = true
} }