Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: a deadlock caused by bsc protocol handeshake timeout #1484

Merged
merged 3 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eth/handler_bsc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (h *bscHandler) RunPeer(peer *bsc.Peer, hand bsc.Handler) error {
ps.lock.Lock()
if wait, ok := ps.bscWait[id]; ok {
delete(ps.bscWait, id)
wait <- peer
wait <- nil
}
ps.lock.Unlock()
return err
Expand Down
25 changes: 21 additions & 4 deletions eth/peerset.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const (
// extensionWaitTimeout is the maximum allowed time for the extension wait to
// complete before dropping the connection as malicious.
extensionWaitTimeout = 10 * time.Second
tryWaitTimeout = 100 * time.Millisecond
)

// peerSet represents the collection of active peers currently participating in
Expand Down Expand Up @@ -402,10 +403,26 @@ func (ps *peerSet) waitBscExtension(peer *eth.Peer) (*bsc.Peer, error) {
return peer, nil
brilliant-lx marked this conversation as resolved.
Show resolved Hide resolved

case <-time.After(extensionWaitTimeout):
ps.lock.Lock()
delete(ps.bscWait, id)
ps.lock.Unlock()
return nil, errPeerWaitTimeout
// could be deadlock, so we use TryLock to avoid it.
brilliant-lx marked this conversation as resolved.
Show resolved Hide resolved
if ps.lock.TryLock() {
delete(ps.bscWait, id)
ps.lock.Unlock()
return nil, errPeerWaitTimeout
}
// if TryLock failed, we wait for a while and try again.
for {
select {
case <-wait:
// discard the peer, even though the peer arrived.
return nil, errPeerWaitTimeout
case <-time.After(tryWaitTimeout):
if ps.lock.TryLock() {
delete(ps.bscWait, id)
ps.lock.Unlock()
return nil, errPeerWaitTimeout
}
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion p2p/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ func (p *Peer) startProtocols(writeStart <-chan struct{}, writeErr chan<- error)
p.log.Trace(fmt.Sprintf("Protocol %s/%d returned", proto.Name, proto.Version))
err = errProtocolReturned
} else if !errors.Is(err, io.EOF) {
p.log.Trace(fmt.Sprintf("Protocol %s/%d failed", proto.Name, proto.Version), "err", err)
p.log.Warn(fmt.Sprintf("Protocol %s/%d failed", proto.Name, proto.Version), "err", err)
}
p.protoErr <- err
}()
Expand Down