From e1f1107c2d34aeae9ed5801a8ab25f38e1cf07ee Mon Sep 17 00:00:00 2001 From: SolidityGo Date: Wed, 3 Nov 2021 16:40:14 +0800 Subject: [PATCH 1/2] fix: graceful shutdown bug that diff handshake failure caused waitDiffExtension can't receive exit signal --- eth/handler_diff.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/eth/handler_diff.go b/eth/handler_diff.go index c996105f0f..334ea08e20 100644 --- a/eth/handler_diff.go +++ b/eth/handler_diff.go @@ -33,6 +33,15 @@ func (h *diffHandler) Chain() *core.BlockChain { return h.chain } // RunPeer is invoked when a peer joins on the `diff` protocol. func (h *diffHandler) RunPeer(peer *diff.Peer, hand diff.Handler) error { if err := peer.Handshake(h.diffSync); err != nil { + // ensure that waitDiffExtension receives the exit signal normally + // otherwise, can't graceful shutdown + ps := h.peers + id := peer.ID() + + if wait, ok := ps.diffWait[id]; ok { + delete(ps.diffWait, id) + wait <- peer + } return err } defer h.chain.RemoveDiffPeer(peer.ID()) From 5c8ddafa563f918843ae48a1676fe8fade5893fd Mon Sep 17 00:00:00 2001 From: SolidityGo Date: Wed, 3 Nov 2021 18:29:39 +0800 Subject: [PATCH 2/2] fix: add lock for peerset --- eth/handler_diff.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/eth/handler_diff.go b/eth/handler_diff.go index 334ea08e20..a5fbfdb0a8 100644 --- a/eth/handler_diff.go +++ b/eth/handler_diff.go @@ -38,10 +38,13 @@ func (h *diffHandler) RunPeer(peer *diff.Peer, hand diff.Handler) error { ps := h.peers id := peer.ID() + // Ensure nobody can double connect + ps.lock.Lock() if wait, ok := ps.diffWait[id]; ok { delete(ps.diffWait, id) wait <- peer } + ps.lock.Unlock() return err } defer h.chain.RemoveDiffPeer(peer.ID())