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 <dima-stepan@yandex.ru>
This commit is contained in:
Dmitrii Stepanov 2024-02-23 12:25:15 +03:00
parent 327e766cd9
commit 6147bf452a

View file

@ -164,6 +164,7 @@ func (p *TCPPeer) handleConn() {
err = p.SendVersion() err = p.SendVersion()
if err == nil { if err == nil {
r := io.NewBinReaderFromIO(p.conn) r := io.NewBinReaderFromIO(p.conn)
loop:
for { for {
msg := &Message{StateRootInHeader: p.server.config.StateRootInHeader} msg := &Message{StateRootInHeader: p.server.config.StateRootInHeader}
err = msg.Decode(r) err = msg.Decode(r)
@ -174,7 +175,11 @@ func (p *TCPPeer) handleConn() {
} else if err != nil { } else if err != nil {
break break
} }
p.incoming <- msg select {
case p.incoming <- msg:
case <-p.done:
break loop
}
} }
} }
p.Disconnect(err) p.Disconnect(err)