Skip to content

Commit

Permalink
eth: fix potential hang in waitSnapExtension (ethereum#28744)
Browse files Browse the repository at this point in the history
This should fix a rare hang in waitSnapExtension during shutdown.
  • Loading branch information
niuxiaojie81 authored Jan 15, 2024
1 parent 7596db5 commit 18e154e
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion eth/peerset.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type peerSet struct {

lock sync.RWMutex
closed bool
quitCh chan struct{} // Quit channel to signal termination
}

// newPeerSet creates a new peer set to track the active participants.
Expand All @@ -65,6 +66,7 @@ func newPeerSet() *peerSet {
peers: make(map[string]*ethPeer),
snapWait: make(map[string]chan *snap.Peer),
snapPend: make(map[string]*snap.Peer),
quitCh: make(chan struct{}),
}
}

Expand Down Expand Up @@ -129,7 +131,15 @@ func (ps *peerSet) waitSnapExtension(peer *eth.Peer) (*snap.Peer, error) {
ps.snapWait[id] = wait
ps.lock.Unlock()

return <-wait, nil
select {
case p := <-wait:
return p, nil
case <-ps.quitCh:
ps.lock.Lock()
delete(ps.snapWait, id)
ps.lock.Unlock()
return nil, errPeerSetClosed
}
}

// registerPeer injects a new `eth` peer into the working set, or returns an error
Expand Down Expand Up @@ -256,5 +266,8 @@ func (ps *peerSet) close() {
for _, p := range ps.peers {
p.Disconnect(p2p.DiscQuitting)
}
if !ps.closed {
close(ps.quitCh)
}
ps.closed = true
}

0 comments on commit 18e154e

Please sign in to comment.