From 556aa409d99e3d33b5fe17ddf56429b9caa7b908 Mon Sep 17 00:00:00 2001 From: Kevin McConnell Date: Mon, 23 Sep 2024 10:56:55 +0100 Subject: [PATCH] Ensure failed request buffers are cleaned up When using `NewBufferedReadCloser`, it's normally the client's responsibility to `Close()` the returned buffer. However, if there's an error when initially populating the buffer, we don't return the failed buffer to the client, and we weren't closing it ourselves. This leads to stale buffers being left behind. Instead, if we can't return a properly populated buffer, we should close and discard it before returning nil. --- internal/server/buffer.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/server/buffer.go b/internal/server/buffer.go index 2d7a321..fd05f0e 100644 --- a/internal/server/buffer.go +++ b/internal/server/buffer.go @@ -35,6 +35,7 @@ func NewBufferedReadCloser(r io.ReadCloser, maxBytes, maxMemBytes int64) (io.Rea _, err := io.Copy(buf, r) if err != nil { + buf.Close() return nil, err }