valyala/fasthttp · error · ErrSmallBuffer

error when reading response trailer: %w

Error message

error when reading response trailer: %w

What it means

ReadTrailer failed while reading the trailer part of a chunked response after the body. When the failure is a small-buffer condition, fasthttp wraps ErrSmallReadBuffer into an ErrSmallBuffer with this trailer-specific message.

Source

Thrown at header.go:2258

	if len(b) == 0 {
		// Return ErrTimeout on any timeout.
		if x, ok := err.(interface{ Timeout() bool }); ok && x.Timeout() {
			return ErrTimeout
		}

		if n == 1 || err == io.EOF {
			return io.EOF
		}

		// This is for go 1.6 bug. See https://github.com/golang/go/issues/14121 .
		if err == bufio.ErrBufferFull {
			if h.secureErrorLogMessage {
				return &ErrSmallBuffer{
					error: ErrReadingResponseTrailer,
				}
			}
			return &ErrSmallBuffer{
				error: fmt.Errorf("error when reading response trailer: %w", ErrSmallReadBuffer),
			}
		}

		return fmt.Errorf("error when reading response trailer: %w", err)
	}
	b = mustPeekBuffered(r)
	hh, headersLen, errParse := parseTrailer(b, h.h, h.disableNormalizing)
	h.h = hh
	if errParse != nil {
		if err == io.EOF {
			return err
		}
		return headerError("response", err, errParse, b, h.secureErrorLogMessage)
	}
	mustDiscard(r, headersLen)
	return nil
}

View on GitHub (pinned to c96f600972)

Solutions

  1. Increase Client.ReadBufferSize
  2. Reduce trailer size sent by the server
  3. Disable trailers on the server side if not needed

Example fix

// before
client := &fasthttp.Client{ReadBufferSize: 2048}
// after
client := &fasthttp.Client{ReadBufferSize: 16384}
Defensive patterns

Strategy: type-guard

Validate before calling

// ensure server trailers fit: trailerBytes <= client.ReadBufferSize

Type guard

func isErrSmallBuffer(err error) bool { var e *fasthttp.ErrSmallBuffer; return errors.As(err, &e) }

Try / catch

if err := client.Do(req, resp); err != nil { if isErrSmallBuffer(err) && strings.Contains(err.Error(), "trailer") { /* enlarge ReadBufferSize */ } }

Prevention

When it happens

Trigger: Client reading a chunked HTTP response with trailers whose trailer section exceeds the remaining buffer space (ReadBufferSize too small for the trailers).

Common situations: Servers sending large trailers (e.g. Integrity-Digest, checksums) to a client with a small ReadBufferSize.

Related errors


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