Skip to content

Commit eb8ee67

Browse files
committed
eth: fix hang in waitSnapExtension
When a peer announcing snap support connects without actually starting the snap protocol, waitSnapExtension() will wait forever. Add a timer to stop waiting after 5s.
1 parent 4b9c307 commit eb8ee67

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

eth/peerset.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@ import (
2020
"errors"
2121
"math/big"
2222
"sync"
23+
"time"
2324

2425
"github.com/ethereum/go-ethereum/common"
2526
"github.com/ethereum/go-ethereum/eth/protocols/eth"
2627
"github.com/ethereum/go-ethereum/eth/protocols/snap"
2728
"github.com/ethereum/go-ethereum/p2p"
2829
)
2930

31+
const (
32+
// snapWaitTimeout is the amount of time to wait for the snap protocol to be started.
33+
snapWaitTimeout = 5 * time.Second
34+
)
35+
3036
var (
3137
// errPeerSetClosed is returned if a peer is attempted to be added or removed
3238
// from the peer set after it has been terminated.
@@ -43,6 +49,10 @@ var (
4349
// errSnapWithoutEth is returned if a peer attempts to connect only on the
4450
// snap protocol without advertising the eth main protocol.
4551
errSnapWithoutEth = errors.New("peer connected on snap without compatible eth support")
52+
53+
// errSnapTimeout is returned if the peer takes too long to start the snap
54+
// protocol.
55+
errSnapTimeout = errors.New("peer timeout starting snap protococol")
4656
)
4757

4858
// peerSet represents the collection of active peers currently participating in
@@ -128,7 +138,18 @@ func (ps *peerSet) waitSnapExtension(peer *eth.Peer) (*snap.Peer, error) {
128138
ps.snapWait[id] = wait
129139
ps.lock.Unlock()
130140

131-
return <-wait, nil
141+
t := time.NewTimer(snapWaitTimeout)
142+
select {
143+
case p := <-wait:
144+
return p, nil
145+
case <-t.C:
146+
ps.lock.Lock()
147+
if _, ok := ps.snapWait[id]; ok {
148+
delete(ps.snapWait, id)
149+
}
150+
ps.lock.Unlock()
151+
return nil, errSnapTimeout
152+
}
132153
}
133154

134155
// registerPeer injects a new `eth` peer into the working set, or returns an error

0 commit comments

Comments
 (0)