From 1abdae415726d7a3ffea7d409a2002f690689458 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Mon, 19 Jun 2023 17:58:49 +0200 Subject: [PATCH] identify: set stream deadlines for Identify and Identify Push streams --- 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 099166d4e1..e997060ab9 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -47,8 +47,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 @@ -410,11 +409,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) @@ -433,6 +435,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) } @@ -494,8 +497,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 cd5118b5e4..8def30b1dd 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -742,10 +742,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()) @@ -788,10 +788,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())