{"id":"d7cc43004215ea5d","repo":"gofiber/fiber","slug":"internal-server-error","errorCode":null,"errorMessage":"Internal Server Error","messagePattern":"Internal Server Error","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"middleware/adaptor/adaptor.go","lineNumber":521,"sourceCode":"\t\t\tvar n int64\n\t\t\t// http.NoBody never yields any bytes, so skip the copy machinery\n\t\t\t// entirely for the (very common) bodyless request.\n\t\t\tif r.Body != http.NoBody {\n\t\t\t\tlimit := maxBodySize\n\t\t\t\tif limit < math.MaxInt64 {\n\t\t\t\t\tlimit++\n\t\t\t\t}\n\t\t\t\t// The LimitedReader lives in the pooled ctx and the copy\n\t\t\t\t// buffer comes from the shared pool: io.Copy would otherwise\n\t\t\t\t// allocate a fresh 32 KiB buffer on every single request.\n\t\t\t\tpctx.lr.R = r.Body\n\t\t\t\tpctx.lr.N = limit\n\n\t\t\t\tvar err error\n\t\t\t\tn, err = copyBody(req.BodyWriter(), &pctx.lr)\n\t\t\t\tpctx.lr.R = nil // don't keep the request body alive in the pool\n\t\t\t\tif err != nil {\n\t\t\t\t\thttp.Error(w, utils.StatusMessage(fiber.StatusInternalServerError), fiber.StatusInternalServerError)\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tif n > maxBodySize {\n\t\t\t\t\thttp.Error(w, utils.StatusMessage(fiber.StatusRequestEntityTooLarge), fiber.StatusRequestEntityTooLarge)\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treq.Header.SetContentLength(int(n))\n\t\t}\n\t\treq.Header.SetMethod(r.Method)\n\t\treq.SetRequestURI(r.RequestURI)\n\t\treq.SetHost(r.Host)\n\t\treq.Header.SetHost(r.Host)\n\t\t// Propagate the real protocol version so protocol-dependent behavior\n\t\t// (e.g. skipping interim 1xx responses for non-HTTP/1.1 requests,\n\t\t// RFC 9110 Section 15.2) sees the truth instead of fasthttp's","sourceCodeStart":503,"sourceCodeEnd":539,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/middleware/adaptor/adaptor.go#L503-L539","documentation":"Returns HTTP 500 'Internal Server Error' from middleware/adaptor/adaptor.go:521 when copyBody fails while streaming the net/http request body into the fasthttp request buffer. copyBody uses a pooled buffer with io.CopyBuffer (adaptor.go:132-141); any read error from r.Body (client disconnect, network reset, decompression fault) surfaces here. The error is mapped to a 500 because the body is already partially consumed and the request cannot be reliably processed.","triggerScenarios":"Client closes the connection mid-upload; a TLS reset or proxy timeout; a request body whose stream returns an io error; chained middleware that wraps r.Body incorrectly.","commonSituations":"Flaky mobile clients dropping connections; reverse proxy timeouts on slow uploads; misbehaving request body wrappers; very slow producers hitting network idle timeouts.","solutions":["Verify the client is not disconnecting; check upstream proxy read/connect timeouts and raise them for slow uploads.","Ensure any custom wrapping of http.Request.Body preserves io.ReadCloser semantics.","Log the underlying error (instrument copyBody's caller) to distinguish client disconnects from real server faults."],"exampleFix":"// before\n// proxy read timeout too tight for uploads\nsrv := &http.Server{ReadTimeout: 2 * time.Second}\n\n// after\nsrv := &http.Server{\n    ReadTimeout:  60 * time.Second,\n    WriteTimeout: 60 * time.Second,\n}","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"// The adaptor returns a 500 internally; recover at the handler boundary\n// for downstream errors, but body-stream faults must be prevented upstream.\nfunc recoverer(c fiber.Ctx) error {\n    defer func() {\n        if r := recover(); r != nil {\n            log.Printf(\"adaptor body error: %v\", r)\n        }\n    }()\n    return c.Next()\n}","preventionTips":["Set proxy/server ReadTimeout and WriteTimeout generously for upload routes.","Do not wrap http.Request.Body with readers that can error spuriously.","Instrument the adaptor to log client disconnects distinctly from real I/O faults."],"tags":["adaptor","net-http","body-stream","http-500","io-error","middleware"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}