{"record":{"id":"a7beac768b5ca889","repo":"microsoft/aspire","slug":"too-many-headers-limit-max-header-count","errorCode":null,"errorMessage":"Too many headers (limit {_MAX_HEADER_COUNT})","messagePattern":"Too many headers \\(limit (.+?)\\)","errorType":"exception","errorClass":"ConnectionError","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.CodeGeneration.Python/PythonModuleBuilder.cs","lineNumber":936,"sourceCode":"                    if line.endswith(b\"\\r\\n\"):\n                        return line[:-2]  # Remove \\r\\n\n\n            def _read_headers(self) -> dict[str, str]:\n                '''Read HTTP-style headers until empty line.\n\n                Enforces limits on header count and total size to prevent\n                memory exhaustion from a malicious peer.\n                '''\n                headers: dict[str, str] = {}\n                total_bytes = 0\n                while True:\n                    line = self._read_line()\n                    if not line:  # Empty line signals end of headers\n                        break\n\n                    total_bytes += len(line) + 2  # account for \\r\\n\n                    if len(headers) >= _MAX_HEADER_COUNT:\n                        raise ConnectionError(f\"Too many headers (limit {_MAX_HEADER_COUNT})\")\n                    if total_bytes > _MAX_HEADER_BYTES:\n                        raise ConnectionError(f\"Headers too large (limit {_MAX_HEADER_BYTES} bytes)\")\n\n                    # Parse \"Header-Name: value\"\n                    if b\":\" in line:\n                        name, value = line.split(b\":\", 1)\n                        headers[name.decode(\"utf-8\").strip().lower()] = value.decode(\"utf-8\").strip()\n                return headers\n\n            def _receive_loop(self) -> None:\n                '''Receive and process messages from the server'''\n                try:\n                    while self._connected and self._socket:\n                        # Read HTTP-style headers (HeaderDelimitedMessageHandler format)\n                        headers = self._read_headers()\n                        content_length = int(headers.get(\"content-length\", \"0\"))\n\n                        if content_length == 0:","sourceCodeStart":918,"sourceCodeEnd":954,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.CodeGeneration.Python/PythonModuleBuilder.cs#L918-L954","documentation":"Raised by _read_headers in the generated Python client when a response contains more HTTP-style headers than the client's maximum (_MAX_HEADER_COUNT). The client enforces a hard cap to protect against malformed or malicious peer responses and raises ConnectionError with the limit in the message.","triggerScenarios":"A host (or whatever is on the other end of the pipe) emits a response with an excessive number of headers; framing corruption causing ordinary body bytes to be parsed as headers; proxying through something that injects many headers.","commonSituations":"Desynchronized framing after a previous partial read, so arbitrary bytes are interpreted as header lines; an intermediate tool adding debug headers; a protocol mismatch between client and server versions where the server includes far more metadata headers.","solutions":["Ensure reads are not desynchronized: never read raw from the socket outside the framing helpers.","After any ConnectionError, discard the connection and reconnect rather than continuing on the same stream.","Check whether the host is behind a proxy or wrapper adding headers; strip or bypass it.","If the host legitimately needs more headers, update the generated module so client/server protocol limits match."],"exampleFix":"// before\n# continued reading on a connection after a prior timeout\ndata = client.invoke(...)\n\n// after\n# on any framing error, discard and reconnect\ntry:\n    data = client.invoke(...)\nexcept ConnectionError:\n    client.reconnect()\n    data = client.invoke(...)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    data = client.invoke(...)\nexcept ConnectionError as e:\n    if \"Too many headers\" in str(e):\n        client.reconnect()\n        data = client.invoke(...)\n    else:\n        raise","preventionTips":["Never read raw bytes outside the framing helpers.","Always reconnect rather than continue after framing errors.","Keep client and generated module versions in sync with the host protocol."],"tags":["protocol","headers","limit-exceeded","python"],"backgroundTag":"payload-too-large","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}