aio-libs/aiohttp · error · BadHttpMessage

Too many trailers received

Error message

Too many trailers received

What it means

BadHttpMessage('Too many trailers received') raised when the number of trailer lines accumulated after the zero-length chunk exceeds max_trailers (default 128). This protects against trailer flooding / memory exhaustion.

Source

Thrown at aiohttp/http_parser.py:1087

                                "Bad trailer line ending, expected CRLF"
                            )
                            set_exception(self.payload, exc)
                            raise exc
                        self._chunk_tail = chunk
                        return PayloadState.PAYLOAD_NEEDS_INPUT, b""

                    line = chunk[:pos]
                    chunk = chunk[pos + len(SEP) :]
                    if SEP == b"\n":  # For lax response parsing
                        line = line.rstrip(b"\r")

                    if len(line) > self._max_field_size:
                        raise LineTooLong(line[:100] + b"...", self._max_field_size)

                    self._trailer_lines.append(line)

                    if len(self._trailer_lines) > self._max_trailers:
                        raise BadHttpMessage("Too many trailers received")

                    # \r\n\r\n found, end of stream
                    if self._trailer_lines[-1] == b"":
                        # Headers and trailers are defined the same way,
                        # so we reuse the HeadersParser here.
                        try:
                            trailers, raw_trailers = self._headers_parser.parse_headers(
                                self._trailer_lines
                            )
                        finally:
                            self._trailer_lines.clear()
                        self.payload.feed_eof()
                        return PayloadState.PAYLOAD_COMPLETE, chunk

        # Read all bytes until eof
        elif self._type == ParseState.PARSE_UNTIL_EOF:
            self._more_data_available = self.payload.feed_data(chunk)
            while self._more_data_available:

View on GitHub (pinned to c0ef574e29)

Solutions

  1. Increase max_trailers if many trailers are legitimate (configure the parser).
  2. Ensure the sender terminates trailers with a blank CRLF line.
  3. Reject/drop connections sending excessive trailers at the edge.
Defensive patterns

Strategy: validation

Try / catch

from aiohttp.http_exceptions import BadHttpMessage
try:
    await request.read()
except BadHttpMessage as e:
    if 'trailer' in e.message:
        return web.Response(status=400)

Prevention

When it happens

Trigger: A chunked message includes more than max_trailers trailer lines before the terminating blank line. Each appended trailer increments the count; once it exceeds max_trailers the parser raises.

Common situations: Adversarial peer flooding trailers; buggy sender never emitting the blank terminator so lines accumulate; max_trailers configured too low for a legit use; misbehaving intermediary appending many trailers.

Related errors


AI-assisted analysis of aio-libs/aiohttp@c0ef574e29 (2026-08-04). Data as JSON: /data/errors/e7dc29c1627cea94.json. Report an issue: GitHub.