fix(pools): return pooled gzip reader on Reset error

GetGzipReader got a reader from the pool, Reset it, and on error returned a
fresh gzip.NewReader while never returning the pooled reader to the pool —
one leaked reader per reset error (non-gzip input), silently draining the
pool. Return the pooled reader without Close (Close panics because the
decompressor is nil until a header parses); the next caller's Reset
reinitialises it.

Test: TestGetGzipReader_ResetErrorPath (error path leaves the pool usable
for subsequent valid gzip reads).
This commit is contained in:
2026-08-13 03:10:21 +01:00
parent bbd43bfe9e
commit 546ded1d9c
2 changed files with 34 additions and 1 deletions
+6 -1
View File
@@ -78,7 +78,12 @@ func PutGzipWriter(gz *gzip.Writer) {
func GetGzipReader(r io.Reader) (*gzip.Reader, error) {
gr := gzipReaderPool.Get().(*gzip.Reader)
if err := gr.Reset(r); err != nil {
// If reset fails, create a new reader
// Reset failed (e.g. non-gzip data): return the pooled reader so it
// isn't leaked, and hand back a fresh one for this call. We must not
// call gr.Close() here — the decompressor is nil until a header
// parses, so Close would panic. The next GetGzipReader's Reset
// reinitialises it.
gzipReaderPool.Put(gr)
return gzip.NewReader(r)
}
return gr, nil