From 9db41016ab8c349e362d2ca867ee2c22cabe5114 Mon Sep 17 00:00:00 2001 From: Sam Kleinman Date: Sat, 14 May 2022 08:28:09 -0400 Subject: [PATCH] p2p: avoid sending errors after context is canceled (#8548) --- internal/mempool/reactor.go | 9 +++++++++ internal/p2p/channel.go | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/internal/mempool/reactor.go b/internal/mempool/reactor.go index 8aa25bcf7..808c90a7c 100644 --- a/internal/mempool/reactor.go +++ b/internal/mempool/reactor.go @@ -153,6 +153,15 @@ func (r *Reactor) handleMempoolMessage(ctx context.Context, envelope *p2p.Envelo // problem. continue } + if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { + // Do not propagate context + // cancellation errors, but do + // not continue to check + // transactions from this + // message if we are shutting down. + return nil + } + logger.Error("checktx failed for tx", "tx", fmt.Sprintf("%X", types.Tx(tx).Hash()), "err", err) diff --git a/internal/p2p/channel.go b/internal/p2p/channel.go index e0a69a3b2..b81b261f2 100644 --- a/internal/p2p/channel.go +++ b/internal/p2p/channel.go @@ -105,6 +105,10 @@ func (ch *legacyChannel) Send(ctx context.Context, envelope Envelope) error { // SendError blocks until the given error has been sent, or ctx ends. // An error only occurs if the context ends before the send completes. func (ch *legacyChannel) SendError(ctx context.Context, pe PeerError) error { + if errors.Is(pe.Err, context.Canceled) || errors.Is(pe.Err, context.DeadlineExceeded) { + return nil + } + select { case <-ctx.Done(): return ctx.Err() @@ -414,6 +418,10 @@ func (ch *libp2pChannelImpl) Send(ctx context.Context, e Envelope) error { } func (ch *libp2pChannelImpl) SendError(ctx context.Context, pe PeerError) error { + if errors.Is(pe.Err, context.Canceled) || errors.Is(pe.Err, context.DeadlineExceeded) || ctx.Err() != nil { + return nil + } + // TODO: change handling of errors to peers. This problably // shouldn't be handled as a property of the channel, and // rather as part of some peer-info/network-management