Skip to content

Commit

Permalink
Merge pull request #2232 from niuxiaojie89/feature/bump-version-to-1.5.0
Browse files Browse the repository at this point in the history
fix hang in waitSnapExtension
  • Loading branch information
benbaley authored Dec 28, 2023
2 parents c94ac59 + 0fe6bae commit b67ea9c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ func (s *Ethereum) Stop() error {
s.ethDialCandidates.Close()
s.snapDialCandidates.Close()
s.p2pServer.CloseConsensusDial()
s.p2pServer.CloseDiscovery()
s.handler.Stop()

// Then stop everything else.
Expand Down
26 changes: 25 additions & 1 deletion eth/peerset.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ import (
"errors"
"math/big"
"sync"
"time"

"github.com/PlatONnetwork/PlatON-Go/common"
"github.com/PlatONnetwork/PlatON-Go/eth/protocols/eth"
"github.com/PlatONnetwork/PlatON-Go/eth/protocols/snap"
"github.com/PlatONnetwork/PlatON-Go/p2p"
)

const (
// snapWaitTimeout is the amount of time to wait for the snap protocol to be started.
snapWaitTimeout = 5 * time.Second
)

var (
// errPeerSetClosed is returned if a peer is attempted to be added or removed
// from the peer set after it has been terminated.
Expand All @@ -43,6 +49,9 @@ var (
// errSnapWithoutEth is returned if a peer attempts to connect only on the
// snap protocol without advertizing the eth main protocol.
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
Expand Down Expand Up @@ -128,7 +137,22 @@ func (ps *peerSet) waitSnapExtension(peer *eth.Peer) (*snap.Peer, error) {
ps.snapWait[id] = wait
ps.lock.Unlock()

return <-wait, nil
snapWaitTicker := time.NewTicker(snapWaitTimeout)
for {
select {
case p := <-wait:
snapWaitTicker.Stop()
return p, nil
case <-snapWaitTicker.C:
if ps.closed {
snapWaitTicker.Stop()
ps.lock.Lock()
delete(ps.snapWait, id)
ps.lock.Unlock()
return nil, errSnapTimeout
}
}
}
}

// registerPeer injects a new `eth` peer into the working set, or returns an error
Expand Down
8 changes: 7 additions & 1 deletion p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func (srv *Server) RemoveConsensusPeer(node *enode.Node) {

func (srv *Server) CloseConsensusDial() {
srv.dialsched.closeConsensusDial()
srv.log.Info("close consensus Dial")
srv.log.Info("Close consensus Dial")
}

// AddTrustedPeer adds the given node to a reserved whitelist which allows the
Expand Down Expand Up @@ -1262,6 +1262,12 @@ func (srv *Server) PeersInfo() []*PeerInfo {
return infos
}

func (srv *Server) CloseDiscovery() {
srv.ntab.Close()
srv.discmix.Close()
srv.log.Info("Close ntab and discmix")
}

func (srv *Server) StartWatching(eventMux *event.TypeMux) {
srv.eventMux = eventMux
go srv.watching()
Expand Down

0 comments on commit b67ea9c

Please sign in to comment.