From 6147bf452a7fe41ea28c3b19cc6bd398bd5ce2f4 Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Fri, 23 Feb 2024 12:25:15 +0300 Subject: [PATCH] tcp_peer: Fix possible goroutine leak Scenario: 1. Two messages were read from the connection `p.conn` 2. The first message has started to be processed 3. The second message was queued to be added to the channel `p.incoming` 4. Processing of the first message failed with an error 5. TCP peer is closed, but processing of the second message continues Signed-off-by: Dmitrii Stepanov --- pkg/network/tcp_peer.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/network/tcp_peer.go b/pkg/network/tcp_peer.go index e989c4b66..496935d94 100644 --- a/pkg/network/tcp_peer.go +++ b/pkg/network/tcp_peer.go @@ -164,6 +164,7 @@ func (p *TCPPeer) handleConn() { err = p.SendVersion() if err == nil { r := io.NewBinReaderFromIO(p.conn) + loop: for { msg := &Message{StateRootInHeader: p.server.config.StateRootInHeader} err = msg.Decode(r) @@ -174,7 +175,11 @@ func (p *TCPPeer) handleConn() { } else if err != nil { break } - p.incoming <- msg + select { + case p.incoming <- msg: + case <-p.done: + break loop + } } } p.Disconnect(err)