From 6e539f4b0d5f41ef85f72f87736789f810a82c2e Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Thu, 11 May 2023 11:30:20 +0800 Subject: [PATCH] chore: drop bufio.Reader in BufferedConn to let gc can clean up its internal buf --- common/net/bufconn.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/common/net/bufconn.go b/common/net/bufconn.go index 2ff73c82..6da2d9d1 100644 --- a/common/net/bufconn.go +++ b/common/net/bufconn.go @@ -62,7 +62,7 @@ func (c *BufferedConn) Buffered() int { } func (c *BufferedConn) ReadBuffer(buffer *buf.Buffer) (err error) { - if c.r.Buffered() > 0 { + if c.r != nil && c.r.Buffered() > 0 { _, err = buffer.ReadOnceFrom(c.r) return } @@ -70,10 +70,11 @@ func (c *BufferedConn) ReadBuffer(buffer *buf.Buffer) (err error) { } func (c *BufferedConn) ReadCached() *buf.Buffer { // call in sing/common/bufio.Copy - if c.r.Buffered() > 0 { + if c.r != nil && c.r.Buffered() > 0 { length := c.r.Buffered() b, _ := c.r.Peek(length) _, _ = c.r.Discard(length) + c.r = nil // drop bufio.Reader to let gc can clean up its internal buf return buf.As(b) } return nil @@ -84,7 +85,7 @@ func (c *BufferedConn) Upstream() any { } func (c *BufferedConn) ReaderReplaceable() bool { - if c.r.Buffered() > 0 { + if c.r != nil && c.r.Buffered() > 0 { return false } return true