valyala/fasthttp · error

error when reading request headers: %w

Error message

error when reading request headers: %w

What it means

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.

Source

Thrown at header.go:2356

		if err == nil {
			panic("bufio.Reader.Peek() returned nil, nil")
		}

		// This is for go 1.6 bug. See https://github.com/golang/go/issues/14121 .
		if err == bufio.ErrBufferFull {
			return &ErrSmallBuffer{
				error: fmt.Errorf("error when reading request headers: %w (n=%d, reader buffered=%d)", ErrSmallReadBuffer, n, r.Buffered()),
			}
		}

		// n == 1 on the first read for the request.
		if n == 1 {
			// We didn't read a single byte.
			return ErrNothingRead{error: err}
		}

		return fmt.Errorf("error when reading request headers: %w", err)
	}
	b = mustPeekBuffered(r)
	headersLen, errParse := h.parse(b)
	if errParse != nil {
		return headerError("request", err, errParse, b, h.secureErrorLogMessage)
	}
	if errValidate := h.validate(); errValidate != nil {
		return headerError("request", err, errValidate, b, h.secureErrorLogMessage)
	}
	mustDiscard(r, headersLen)
	return nil
}

func (h *RequestHeader) validate() error {
	// Host header is mandatory in HTTP/1.1 requests.
	if h.IsHTTP11() && len(h.Host()) == 0 {
		h.connectionClose = true
		return errRequestHostRequired

View on GitHub (pinned to c96f600972)

Solutions

  1. Treat it as a transient connection-level failure — close the connection and let the client retry
  2. Enable keep-alive timeout tuning (Server.ReadTimeout/IdleTimeout) to reap dead sockets sooner
  3. Log at debug level; these are usually client-side disconnects, not server bugs
  4. Check for intermediate proxies/LBs with shorter idle timeouts than your server
Defensive patterns

Strategy: retry

Type guard

func isHeaderIOError(err error) bool {
    return err != nil && strings.Contains(err.Error(), "error when reading request headers")
}

Try / catch

err := client.Do(req, resp)
if err != nil && strings.Contains(err.Error(), "error when reading request headers") {
    // transient I/O during header read; safe to retry with a fresh request
    return retryWithBackoff(doRequest, 3)
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of valyala/fasthttp@c96f600972 (2026-08-31). Data as JSON: /api/errors/84ab1ae1025d919f. Report an issue: GitHub.