From 479940b7d08ff27b48d5239b948663d56c9b6ea0 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Wed, 17 Jan 2024 11:47:42 -0800 Subject: [PATCH] Deallocate huge read buffers (#18813) If a message buffer is excessively huge, release it back so it isn't kept around forever. --- internal/grid/connection.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/grid/connection.go b/internal/grid/connection.go index 30d35edf2..2203c9580 100644 --- a/internal/grid/connection.go +++ b/internal/grid/connection.go @@ -899,7 +899,6 @@ func (c *Connection) handleMessages(ctx context.Context, conn net.Conn) { } continue } - if int64(cap(dst)) < hdr.Length+1 { dst = make([]byte, 0, hdr.Length+hdr.Length>>3) } @@ -914,6 +913,10 @@ func (c *Connection) handleMessages(ctx context.Context, conn net.Conn) { cancel(ErrDisconnected) return } + if cap(msg) > readBufferSize*8 { + // Don't keep too much memory around. + msg = nil + } var err error msg, err = readDataInto(msg, conn, c.side, ws.OpBinary)