{"record":{"id":"84ab1ae1025d919f","repo":"valyala/fasthttp","slug":"error-when-reading-request-headers-w","errorCode":null,"errorMessage":"error when reading request headers: %w","messagePattern":"error when reading request headers: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"header.go","lineNumber":2356,"sourceCode":"\n\t\tif err == nil {\n\t\t\tpanic(\"bufio.Reader.Peek() returned nil, nil\")\n\t\t}\n\n\t\t// This is for go 1.6 bug. See https://github.com/golang/go/issues/14121 .\n\t\tif err == bufio.ErrBufferFull {\n\t\t\treturn &ErrSmallBuffer{\n\t\t\t\terror: fmt.Errorf(\"error when reading request headers: %w (n=%d, reader buffered=%d)\", ErrSmallReadBuffer, n, r.Buffered()),\n\t\t\t}\n\t\t}\n\n\t\t// n == 1 on the first read for the request.\n\t\tif n == 1 {\n\t\t\t// We didn't read a single byte.\n\t\t\treturn ErrNothingRead{error: err}\n\t\t}\n\n\t\treturn fmt.Errorf(\"error when reading request headers: %w\", err)\n\t}\n\tb = mustPeekBuffered(r)\n\theadersLen, errParse := h.parse(b)\n\tif errParse != nil {\n\t\treturn headerError(\"request\", err, errParse, b, h.secureErrorLogMessage)\n\t}\n\tif errValidate := h.validate(); errValidate != nil {\n\t\treturn headerError(\"request\", err, errValidate, b, h.secureErrorLogMessage)\n\t}\n\tmustDiscard(r, headersLen)\n\treturn nil\n}\n\nfunc (h *RequestHeader) validate() error {\n\t// Host header is mandatory in HTTP/1.1 requests.\n\tif h.IsHTTP11() && len(h.Host()) == 0 {\n\t\th.connectionClose = true\n\t\treturn errRequestHostRequired","sourceCodeStart":2338,"sourceCodeEnd":2374,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/header.go#L2338-L2374","documentation":"Generic wrapper for any error (other than small-buffer or nothing-read) that occurred while reading request headers, such as an I/O failure or connection reset mid-headers. fasthttp throws it to indicate the request headers could not be fully read from the connection.","triggerScenarios":"RequestHeader.Read encounters an I/O error from the underlying connection after at least one byte and more than one byte were read (n>1, not bufio.ErrBufferFull).","commonSituations":"Client abruptly closes the connection mid-header-write; network timeouts or resets; TLS termination layers dropping connections; keep-alive reuse of a socket the peer already closed.","solutions":["Treat it as a transient connection-level failure — close the connection and let the client retry","Enable keep-alive timeout tuning (Server.ReadTimeout/IdleTimeout) to reap dead sockets sooner","Log at debug level; these are usually client-side disconnects, not server bugs","Check for intermediate proxies/LBs with shorter idle timeouts than your server"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":"func isHeaderIOError(err error) bool {\n    return err != nil && strings.Contains(err.Error(), \"error when reading request headers\")\n}","tryCatchPattern":"err := client.Do(req, resp)\nif err != nil && strings.Contains(err.Error(), \"error when reading request headers\") {\n    // transient I/O during header read; safe to retry with a fresh request\n    return retryWithBackoff(doRequest, 3)\n}","preventionTips":["Tune ReadTimeout/IdleTimeout to reap dead connections","Avoid reusing connections across mismatched timeouts with proxies","Retry idempotent requests on this error class","Log at debug level — usually client disconnects, not server bugs"],"tags":["http","network","fasthttp"],"backgroundTag":"connection-reset-by-peer","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}