Skip to content

Commit 93142e5

Browse files
authored
eth: don't block if transaction broadcast loop fails (#21255)
* eth: don't block if transaction broadcast loop is returned * eth: kick out peer if we failed to send message * eth: address comment
1 parent 23f1a0b commit 93142e5

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

‎eth/handler.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func (pm *ProtocolManager) handle(p *peer) error {
325325
}
326326

327327
// Register the peer locally
328-
if err := pm.peers.Register(p); err != nil {
328+
if err := pm.peers.Register(p, pm.removePeer); err != nil {
329329
p.Log().Error("Ethereum peer registration failed", "err", err)
330330
return err
331331
}

‎eth/peer.go‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,19 @@ func newPeer(version int, p *p2p.Peer, rw p2p.MsgReadWriter, getPooledTx func(ha
129129
// broadcastBlocks is a write loop that multiplexes blocks and block accouncements
130130
// to the remote peer. The goal is to have an async writer that does not lock up
131131
// node internals and at the same time rate limits queued data.
132-
func (p *peer) broadcastBlocks() {
132+
func (p *peer) broadcastBlocks(removePeer func(string)) {
133133
for {
134134
select {
135135
case prop := <-p.queuedBlocks:
136136
if err := p.SendNewBlock(prop.block, prop.td); err != nil {
137+
removePeer(p.id)
137138
return
138139
}
139140
p.Log().Trace("Propagated block", "number", prop.block.Number(), "hash", prop.block.Hash(), "td", prop.td)
140141

141142
case block := <-p.queuedBlockAnns:
142143
if err := p.SendNewBlockHashes([]common.Hash{block.Hash()}, []uint64{block.NumberU64()}); err != nil {
144+
removePeer(p.id)
143145
return
144146
}
145147
p.Log().Trace("Announced block", "number", block.Number(), "hash", block.Hash())
@@ -153,7 +155,7 @@ func (p *peer) broadcastBlocks() {
153155
// broadcastTransactions is a write loop that schedules transaction broadcasts
154156
// to the remote peer. The goal is to have an async writer that does not lock up
155157
// node internals and at the same time rate limits queued data.
156-
func (p *peer) broadcastTransactions() {
158+
func (p *peer) broadcastTransactions(removePeer func(string)) {
157159
var (
158160
queue []common.Hash // Queue of hashes to broadcast as full transactions
159161
done chan struct{} // Non-nil if background broadcaster is running
@@ -204,6 +206,7 @@ func (p *peer) broadcastTransactions() {
204206
done = nil
205207

206208
case <-fail:
209+
removePeer(p.id)
207210
return
208211

209212
case <-p.term:
@@ -215,7 +218,7 @@ func (p *peer) broadcastTransactions() {
215218
// announceTransactions is a write loop that schedules transaction broadcasts
216219
// to the remote peer. The goal is to have an async writer that does not lock up
217220
// node internals and at the same time rate limits queued data.
218-
func (p *peer) announceTransactions() {
221+
func (p *peer) announceTransactions(removePeer func(string)) {
219222
var (
220223
queue []common.Hash // Queue of hashes to announce as transaction stubs
221224
done chan struct{} // Non-nil if background announcer is running
@@ -266,6 +269,7 @@ func (p *peer) announceTransactions() {
266269
done = nil
267270

268271
case <-fail:
272+
removePeer(p.id)
269273
return
270274

271275
case <-p.term:
@@ -706,7 +710,7 @@ func newPeerSet() *peerSet {
706710
// Register injects a new peer into the working set, or returns an error if the
707711
// peer is already known. If a new peer it registered, its broadcast loop is also
708712
// started.
709-
func (ps *peerSet) Register(p *peer) error {
713+
func (ps *peerSet) Register(p *peer, removePeer func(string)) error {
710714
ps.lock.Lock()
711715
defer ps.lock.Unlock()
712716

@@ -718,10 +722,10 @@ func (ps *peerSet) Register(p *peer) error {
718722
}
719723
ps.peers[p.id] = p
720724

721-
go p.broadcastBlocks()
722-
go p.broadcastTransactions()
725+
go p.broadcastBlocks(removePeer)
726+
go p.broadcastTransactions(removePeer)
723727
if p.version >= eth65 {
724-
go p.announceTransactions()
728+
go p.announceTransactions(removePeer)
725729
}
726730
return nil
727731
}

0 commit comments

Comments
 (0)