{"id":"443353b3ab81e0e4","repo":"apache/kafka","slug":"invalid-receive-size-receivesize-larger-than","errorCode":null,"errorMessage":"Invalid receive (size = ${receiveSize} larger than ${maxSize})","messagePattern":"Invalid receive \\(size = (.+?) larger than (.+?)\\)","errorType":"validation","errorClass":"InvalidReceiveException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/common/network/NetworkReceive.java","lineNumber":95,"sourceCode":"    @Override\n    public boolean complete() {\n        return !size.hasRemaining() && buffer != null && !buffer.hasRemaining();\n    }\n\n    public long readFrom(ScatteringByteChannel channel) throws IOException {\n        int read = 0;\n        if (size.hasRemaining()) {\n            int bytesRead = channel.read(size);\n            if (bytesRead < 0)\n                throw new EOFException();\n            read += bytesRead;\n            if (!size.hasRemaining()) {\n                size.rewind();\n                int receiveSize = size.getInt();\n                if (receiveSize < 0)\n                    throw new InvalidReceiveException(\"Invalid receive (size = \" + receiveSize + \")\");\n                if (maxSize != UNLIMITED && receiveSize > maxSize)\n                    throw new InvalidReceiveException(\"Invalid receive (size = \" + receiveSize + \" larger than \" + maxSize + \")\");\n                requestedBufferSize = receiveSize; // may be 0 for some payloads (SASL)\n                if (receiveSize == 0) {\n                    buffer = EMPTY_BUFFER;\n                }\n            }\n        }\n        if (buffer == null && requestedBufferSize != -1) { // we know the size we want but haven't been able to allocate it yet\n            buffer = memoryPool.tryAllocate(requestedBufferSize);\n            if (buffer == null)\n                log.trace(\"Broker low on memory - could not allocate buffer of size {} for source {}\", requestedBufferSize, source);\n        }\n        if (buffer != null) {\n            int bytesRead = channel.read(buffer);\n            if (bytesRead < 0)\n                throw new EOFException();\n            read += bytesRead;\n        }\n","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/common/network/NetworkReceive.java#L77-L113","documentation":"Thrown by NetworkReceive.readFrom as an InvalidReceiveException when maxSize is not UNLIMITED and the decoded frame size exceeds maxSize. Kafka caps inbound messages via the channel's maxReceiveSize (broker: message.max.bytes / socket.request.max.bytes; client: socket.request.max.bytes for responses) to protect the broker from oversized or hostile payloads. The check fires after the 4-byte size prefix is read but before the body is allocated, so the channel is torn down before memory is wasted.","triggerScenarios":"A producer publishing a record batch larger than the broker's message.max.bytes, or a client whose request/response exceeds its socket.request.max.bytes; also a malformed frame claiming a huge body. maxSize comes from the Selector constructor's maxReceiveSize parameter.","commonSituations":"Producer sends a large message/record-batch with max.request.size left high while the broker's message.max.bytes is lower; replication of an oversized message to a follower whose fetch.max.bytes is too small; compression producing a batch that crosses the limit.","solutions":["Compare producer max.request.size against the broker's message.max.bytes (and topic-level max.message.bytes) and align them so the producer cap is <= broker cap.","If the oversized payload is legitimate, raise message.max.bytes on the broker, the topic config, and fetch.max.bytes on consumers/replicas accordingly.","Reduce the record size (split records, raise compression, lower batch.size) if the payload should not be that large."],"exampleFix":"# before\nproducer: max.request.size=20971520\nbroker:   message.max.bytes=1048588\n\n# after (align caps)\nproducer: max.request.size=10485760\nbroker:   message.max.bytes=10485760","handlingStrategy":"validation","validationCode":"// Size maxReceiveSize so legitimate responses always fit.\n// Producer/consumer: message.max.bytes, fetch.max.bytes, max.partition.fetch.bytes\n// Broker: message.max.bytes\nlong maxExpected = Math.max(messageMaxBytes, fetchMaxBytes);\nif (maxExpected > Integer.MAX_VALUE) {\n    throw new IllegalArgumentException(\"max receive size overflows int\");\n}\nSelector selector = new Selector((int) maxExpected, /* ... */);","typeGuard":null,"tryCatchPattern":"try {\n    long read = networkReceive.readFrom(channel);\n} catch (InvalidReceiveException e) {\n    log.warn(\"Receive from {} exceeded maxReceiveSize; closing channel\", networkReceive.source(), e);\n    Utils.closeQuietly(channel, \"channel\");\n    disconnect(networkReceive.source());\n}","preventionTips":["Set client fetch.max.bytes / max.partition.fetch.bytes and broker message.max.bytes consistently so the largest legitimate message fits under maxReceiveSize.","Use compression to keep produced messages under message.max.bytes.","Remember maxReceiveSize defends the broker/client; raising it too high exposes you to memory-exhaustion DoS.","Treat oversized-receive as connection-fatal: close and reconnect rather than retrying inline."],"tags":["network","wire-protocol","invalid-receive","message-size","config"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}