From b8404957951753318202b093582e45cb8344fce6 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Mon, 19 Jun 2023 18:58:14 +0200 Subject: [PATCH] identify: set stream deadlines for Identify and Identify Push streams (#2382) --- p2p/protocol/identify/id.go | 11 ++++++----- p2p/protocol/identify/id_test.go | 12 ++++++------ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index b122697ec9..54645e5295 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -48,8 +48,7 @@ const ServiceName = "libp2p.identify" const maxPushConcurrency = 32 -// StreamReadTimeout is the read timeout on all incoming Identify family streams. -var StreamReadTimeout = 60 * time.Second +var Timeout = 60 * time.Second // timeout on all incoming Identify interactions const ( legacyIDSize = 2 * 1024 // 2k Bytes @@ -416,11 +415,14 @@ func (ids *idService) IdentifyWait(c network.Conn) <-chan struct{} { } func (ids *idService) identifyConn(c network.Conn) error { - s, err := c.NewStream(network.WithUseTransient(context.TODO(), "identify")) + ctx, cancel := context.WithTimeout(context.Background(), Timeout) + defer cancel() + s, err := c.NewStream(network.WithUseTransient(ctx, "identify")) if err != nil { log.Debugw("error opening identify stream", "peer", c.RemotePeer(), "error", err) return err } + s.SetDeadline(time.Now().Add(Timeout)) if err := s.SetProtocol(ID); err != nil { log.Warnf("error setting identify protocol for stream: %s", err) @@ -439,6 +441,7 @@ func (ids *idService) identifyConn(c network.Conn) error { // handlePush handles incoming identify push streams func (ids *idService) handlePush(s network.Stream) { + s.SetDeadline(time.Now().Add(Timeout)) ids.handleIdentifyResponse(s, true) } @@ -500,8 +503,6 @@ func (ids *idService) handleIdentifyResponse(s network.Stream, isPush bool) erro } defer s.Scope().ReleaseMemory(signedIDSize) - _ = s.SetReadDeadline(time.Now().Add(StreamReadTimeout)) - c := s.Conn() r := pbio.NewDelimitedReader(s, signedIDSize) diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go index 0bdaae033e..feb9c36db8 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -804,10 +804,10 @@ func TestLargePushMessage(t *testing.T) { } func TestIdentifyResponseReadTimeout(t *testing.T) { - timeout := identify.StreamReadTimeout - identify.StreamReadTimeout = 100 * time.Millisecond + timeout := identify.Timeout + identify.Timeout = 100 * time.Millisecond defer func() { - identify.StreamReadTimeout = timeout + identify.Timeout = timeout }() ctx, cancel := context.WithCancel(context.Background()) @@ -850,10 +850,10 @@ func TestIdentifyResponseReadTimeout(t *testing.T) { } func TestIncomingIDStreamsTimeout(t *testing.T) { - timeout := identify.StreamReadTimeout - identify.StreamReadTimeout = 100 * time.Millisecond + timeout := identify.Timeout + identify.Timeout = 100 * time.Millisecond defer func() { - identify.StreamReadTimeout = timeout + identify.Timeout = timeout }() ctx, cancel := context.WithCancel(context.Background())