{"record":{"id":"e31d5fb5817032ee","repo":"dotnet/aspnetcore","slug":"messagepack-message-was-length-d-but-claimed-to-b","errorCode":null,"errorMessage":"MessagePack message was length %d but claimed to be length %d.","messagePattern":"MessagePack message was length (.+?) but claimed to be length (.+?)\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/java/signalr/messagepack/src/main/java/com/microsoft/signalr/messagepack/MessagePackHubProtocol.java","lineNumber":83,"sourceCode":"            return null;\n        }\n\n         // MessagePack library can't handle read-only ByteBuffer - copy into an array-backed ByteBuffer if this is the case\n        if (payload.isReadOnly()) {\n            byte[] payloadBytes = new byte[payload.remaining()];\n            payload.get(payloadBytes, 0, payloadBytes.length);\n            payload = ByteBuffer.wrap(payloadBytes);\n        }\n\n        List<HubMessage> hubMessages = new ArrayList<>();\n\n        while (payload.hasRemaining()) {\n            int length;\n            try {\n                length = Utils.readLengthHeader(payload);\n                // Throw if remaining buffer is shorter than length header\n                if (payload.remaining() < length) {\n                    throw new RuntimeException(String.format(\"MessagePack message was length %d but claimed to be length %d.\", payload.remaining(), length));\n                }\n            } catch (IOException ex) {\n                throw new RuntimeException(\"Error reading length header.\", ex);\n            }\n            // Instantiate MessageUnpacker\n            try(MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(payload)) {\n\n                int itemCount = unpacker.unpackArrayHeader();\n                HubMessageType messageType = HubMessageType.values()[unpacker.unpackInt() - 1];\n\n                switch (messageType) {\n                    case INVOCATION:\n                        hubMessages.add(createInvocationMessage(unpacker, binder, itemCount, payload));\n                        break;\n                    case STREAM_ITEM:\n                        hubMessages.add(createStreamItemMessage(unpacker, binder, payload));\n                        break;\n                    case COMPLETION:","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/SignalR/clients/java/signalr/messagepack/src/main/java/com/microsoft/signalr/messagepack/MessagePackHubProtocol.java#L65-L101","documentation":"In MessagePackHubProtocol.parseMessages, after reading the length-prefix header the code checks payload.remaining() >= length. If the remaining bytes in the buffer are fewer than the length the header claimed, the message is truncated/corrupt and the throw fires with (remaining, claimed). This is a wire-format integrity check.","triggerScenarios":"Receiving a MessagePack frame whose prefix declares more bytes than the transport actually delivered: truncated TCP/WebSocket frame, buffer underflow from a partial read, or a server/client MessagePack version mismatch producing a bad header.","commonSituations":"Network layer truncating large frames; a buggy proxy not honoring WebSocket frame boundaries; mixed MessagePack libraries on server vs client disagreeing on the length-header encoding; partial buffer handed to parseMessages.","solutions":["Increase the transport's max message size on both server (MaximumReceiveMessageSize) and client so frames are not split unexpectedly.","Ensure the server and client use the same MessagePack protocol (both default or both custom).","Verify intermediate proxies/load balancers preserve WebSocket frame integrity; bypass them to isolate.","Capture a packet/WebSocket-frame trace to confirm the length header matches the payload."],"exampleFix":"// before - server sends large messages, default caps truncate\nservices.AddSignalR()\n    .AddMessagePackProtocol();\n\n// after - raise caps on both ends\nservices.AddSignalR(o => o.MaximumReceiveMessageSize = 256 * 1024)\n    .AddMessagePackProtocol();\n// client side, ensure the transport buffer can hold the frame","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"hub.onClosed(ex -> {\n  if (ex != null && ex.getMessage().contains(\"MessagePack message was length\")) {\n    // framing integrity failure; raise MaximumReceiveMessageSize and reconnect\n    log.error(\"MessagePack frame truncated; check server/client size caps\");\n  }\n});","preventionTips":["Set MaximumReceiveMessageSize generously on the server and match the client buffer.","Keep server and client MessagePack protocol versions aligned.","Avoid sending very large messages; chunk or use a separate channel."],"tags":["signalr","java","messagepack","protocol","network","framing"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}