{"record":{"id":"fef1a0efd130c3db","repo":"microsoft/aspire","slug":"message-too-large-content-length-bytes-limit-max-message","errorCode":null,"errorMessage":"Message too large: {content_length} bytes (limit {_MAX_MESSAGE_SIZE} bytes)","messagePattern":"Message too large: (.+?) bytes \\(limit (.+?) bytes\\)","errorType":"exception","errorClass":"ConnectionError","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.CodeGeneration.Python/PythonModuleBuilder.cs","lineNumber":958,"sourceCode":"                    # 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:\n                            continue\n\n                        if content_length > _MAX_MESSAGE_SIZE:\n                            raise ConnectionError(\n                                f\"Message too large: {content_length} bytes \"\n                                f\"(limit {_MAX_MESSAGE_SIZE} bytes)\"\n                            )\n\n                        # Read message content\n                        message_bytes = self._recv_exactly(content_length)\n                        message_str = message_bytes.decode(\"utf-8\")\n                        message = json.loads(message_str)\n                        if self.debug:\n                            if message.get(\"result\") == \"pong\":\n                                _logger.debug(\"<- %s\", message)\n                            else:\n                                _logger.info(\"<- %s\", message)\n\n                        # Handle response or request\n                        if \"method\" in message:\n                            # This is a request from the server (callback invocation)\n                            self._handle_server_request(message)","sourceCodeStart":940,"sourceCodeEnd":976,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.CodeGeneration.Python/PythonModuleBuilder.cs#L940-L976","documentation":"After parsing response headers, the client reads content-length and rejects messages larger than _MAX_MESSAGE_SIZE (64 MB) with a ConnectionError before attempting to read the body. This prevents unbounded memory allocation from a malformed or hostile response. It indicates protocol corruption or a peer sending data that violates the AppHost wire contract.","triggerScenarios":"Any request/response exchange where the server (or whatever is on the socket) advertises a content-length greater than 64 MB.","commonSituations":"Connecting to the wrong service that streams large bodies; a desynchronized stream misreading body bytes as a header content-length; an AppHost/server version mismatch producing oversized frames; proxy interference mangling the framing.","solutions":["Confirm the endpoint is the actual AppHost socket, not a proxy or unrelated HTTP server.","Recreate the client connection to resynchronize the socket stream.","Match client module and AppHost versions (regenerate/rebuild the Python module).","If a legitimate payload exceeds 64 MB, reduce the request size (e.g., smaller capability arguments) or report the scenario."],"exampleFix":"// before\nresult = client.invoke_capability(\"export\", {\"data\": huge_blob})  # >64MB args\n// after\nresult = client.invoke_capability(\"export\", {\"data\": chunk})  # send in chunks <64MB","handlingStrategy":"try-catch","validationCode":"import sys\ndef payload_within_limit(args):\n    return sys.getsizeof(repr(args)) < 64 * 1024 * 1024","typeGuard":null,"tryCatchPattern":"try:\n    result = client.invoke_capability(cap, args)\nexcept ConnectionError as e:\n    if 'Message too large' in str(e):\n        raise ValueError('Capability payload exceeds 64MB wire limit') from e\n    raise","preventionTips":["Keep capability arguments well under 64MB; chunk large data","Avoid routing the socket through proxies that alter framing","Match generated module and server versions"],"tags":["python","connection","message-size","protocol"],"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"}