From 5503cd7ff3cb79f9c1f2d3b11822a962352f79d4 Mon Sep 17 00:00:00 2001 From: vyzo Date: Fri, 19 Apr 2019 13:03:18 +0300 Subject: [PATCH] handle misbehaving peers in identify push --- p2p/protocol/identify/id.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index d53c99eafe..cfc4d08897 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -161,11 +161,7 @@ func (ids *IDService) pushHandler(s inet.Stream) { func (ids *IDService) Push() { var wg sync.WaitGroup - // we could make this context timeout-less since we are only opening a new - // stream over an existing connection. This would avoid the need for the - // supervisory goroutine below, but timeout-less contexts in network operations - // make me nervous. - ctx, cancel := context.WithTimeout(ids.ctx, 15*time.Second) + ctx, cancel := context.WithTimeout(ids.ctx, 30*time.Second) ctx = inet.WithNoDial(ctx, "identify push") for _, p := range ids.Host.Network().Peers() { @@ -179,7 +175,18 @@ func (ids *IDService) Push() { return } - ids.requestHandler(s) + rch := make(chan struct{}, 1) + go func() { + ids.requestHandler(s) + rch <- struct{}{} + }() + + select { + case <-rch: + case <-ctx.Done(): + // this is taking too long, abort! + s.Reset() + } }(p) }