From f96ad9727b72c5238d7f9e640401fbda888db566 Mon Sep 17 00:00:00 2001 From: Eugene Kim Date: Fri, 15 Feb 2019 22:01:45 -0800 Subject: [PATCH] Fix stream leak Since streams were multiplexed over connections, this wouldn't have caused file descriptor/socket leaks; however, it would have definitely caused memory leak (for the stream data structures). --- p2p/host/hostv2/hostv2.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/p2p/host/hostv2/hostv2.go b/p2p/host/hostv2/hostv2.go index f5ebe7ff5..c531aa111 100644 --- a/p2p/host/hostv2/hostv2.go +++ b/p2p/host/hostv2/hostv2.go @@ -222,6 +222,11 @@ func (host *HostV2) SendMessage(p p2p.Peer, message []byte) error { "protocolID", ProtocolID, "error", err) return p2p.ErrNewStream } + defer func() { + if err := s.Close(); err != nil { + logger.Warn("cannot close stream", "error", err) + } + }() if nw, err := s.Write(message); err != nil { logger.Error("Write() failed", "peerID", p.PeerID, "protocolID", ProtocolID, "error", err)