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 2 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
27 changes: 23 additions & 4 deletions eth/peerset.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/eth/protocols/eth"
"github.com/ethereum/go-ethereum/eth/protocols/snap"
"github.com/ethereum/go-ethereum/eth/protocols/trust"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p"
)

Expand Down Expand Up @@ -70,6 +71,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 +404,27 @@ 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
// once timeout is reached, bsc protocol won't be enabled on this peer.
log.Warn("waitBscExtension", "peer", id, "bsc protocol timeout, it won't be enabled.")
// 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:
return nil, errPeerWaitTimeout
case <-time.After(tryWaitTimeout):
if ps.lock.TryLock() {
delete(ps.bscWait, id)
ps.lock.Unlock()
return nil, errPeerWaitTimeout
}
}
}
}
}

Expand Down