From f05266923ba5ea58305a2b284e8bb284f0740e4d Mon Sep 17 00:00:00 2001 From: Mohammed Al Sahaf Date: Wed, 1 Nov 2023 20:00:33 +0300 Subject: [PATCH] Revert "caddyhttp: Use sync.Pool to reduce lengthReader allocations (#5848)" This reverts commit c8559c448537969376623be9d352949b59907b0e. Resolving the following panic: ``` panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x122f9b5] goroutine 222573 [running]: github.com/caddyserver/caddy/v2/modules/caddyhttp.(*lengthReader).Close(0x0?) github.com/caddyserver/caddy/v2@v2.7.6-0.20231024195719-67e567b7ecec/modules/caddyhttp/server.go:931 +0x15 golang.org/x/net/http2.(*clientStream).cleanupWriteRequest(0xc000e49980, {0x0, 0x0}) golang.org/x/net@v0.17.0/http2/transport.go:1550 +0x1a2 golang.org/x/net/http2.(*clientStream).doRequest(0xc000e58f00?, 0x0?) golang.org/x/net@v0.17.0/http2/transport.go:1327 +0x28 created by golang.org/x/net/http2.(*ClientConn).RoundTrip in goroutine 222537 golang.org/x/net@v0.17.0/http2/transport.go:1232 +0x308 ``` --- modules/caddyhttp/server.go | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/modules/caddyhttp/server.go b/modules/caddyhttp/server.go index b9861918900..d060738f1dc 100644 --- a/modules/caddyhttp/server.go +++ b/modules/caddyhttp/server.go @@ -318,8 +318,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { // so we can track the number of bytes read from it var bodyReader *lengthReader if r.Body != nil { - bodyReader = getLengthReader(r.Body) - defer putLengthReader(bodyReader) + bodyReader = &lengthReader{Source: r.Body} r.Body = bodyReader } @@ -903,24 +902,6 @@ type lengthReader struct { Length int } -var lengthReaderPool = sync.Pool{ - New: func() interface{} { - return &lengthReader{} - }, -} - -func getLengthReader(source io.ReadCloser) *lengthReader { - reader := lengthReaderPool.Get().(*lengthReader) - reader.Source = source - return reader -} - -func putLengthReader(reader *lengthReader) { - reader.Source = nil - reader.Length = 0 - lengthReaderPool.Put(reader) -} - func (r *lengthReader) Read(b []byte) (int, error) { n, err := r.Source.Read(b) r.Length += n