{"record":{"id":"fe35e90eda2cc287","repo":"dotnet/aspnetcore","slug":"error-reading-messagepack-data","errorCode":null,"errorMessage":"Error reading MessagePack data.","messagePattern":"Error reading MessagePack data\\.","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":137,"sourceCode":"                // Make sure that we actually read the right number of bytes\n                int readBytes = (int) unpacker.getTotalReadBytes();\n                if (readBytes != length) {\n                    // Check what the last message was\n                    // If it was an invocation binding failure, we have to correct the position of the buffer\n                    if (hubMessages.get(hubMessages.size() - 1).getMessageType() == HubMessageType.INVOCATION_BINDING_FAILURE) {\n                        // Cast to a Buffer to avoid the Java 9+ behavior where ByteBuffer.position(int) overrides Buffer.position(int),\n                        // Returning a ByteBuffer rather than a Buffer. This causes issues on Android - see https://github.com/dotnet/aspnetcore/pull/26614\n                        ((Buffer) payload).position(payload.position() + (length - readBytes));\n                    } else {\n                        throw new RuntimeException(String.format(\"MessagePack message was length %d but claimed to be length %d.\", readBytes, length));\n                    }\n                }\n                unpacker.close();\n                // Cast to a Buffer to avoid the Java 9+ behavior where ByteBuffer.position(int) overrides Buffer.position(int),\n                // Returning a ByteBuffer rather than a Buffer. This causes issues on Android - see https://github.com/dotnet/aspnetcore/pull/26614\n                ((Buffer) payload).position(payload.position() + readBytes);\n            } catch (MessagePackException | IOException ex) {\n                throw new RuntimeException(\"Error reading MessagePack data.\", ex);\n            }\n        }\n        return hubMessages;\n    }\n\n    @Override\n    public ByteBuffer writeMessage(HubMessage hubMessage) {\n        HubMessageType messageType = hubMessage.getMessageType();\n\n        try {\n            byte[] message;\n            switch (messageType) {\n                case INVOCATION:\n                    message = writeInvocationMessage((InvocationMessage) hubMessage);\n                    break;\n                case STREAM_ITEM:\n                    message = writeStreamItemMessage((StreamItem) hubMessage);\n                    break;","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/SignalR/clients/java/signalr/messagepack/src/main/java/com/microsoft/signalr/messagepack/MessagePackHubProtocol.java#L119-L155","documentation":"The outer try in parseMessages catches MessagePackException (from the msgpack-core library) and IOException raised while unpacking the array header, message type, and message body. Any such failure - bad varint, unexpected token, truncated array - is wrapped as 'Error reading MessagePack data.' This is the general parse-failure sink for MessagePack message bodies.","triggerScenarios":"The MessagePack payload is structurally invalid: wrong array arity, unknown message-type int, missing fields for the declared message type, or a stream that ended mid-message after the length header passed.","commonSituations":"Protocol version mismatch (server sends a newer message shape the client cannot unpack); MessagePack library version skew; payload corrupted in transit but the length header happened to be valid; a server hub argument type that has no symmetric MessagePack representation on the client.","solutions":["Align server and client SignalR versions so the MessagePack message contracts match.","Ensure all types used as hub method arguments are serializable by the MessagePack protocol on both sides (register custom formatters identically).","Capture the failing payload (log the ByteBuffer before parse) and validate it with the MessagePack library standalone to localize the bad field.","Temporarily switch the hub protocol to JSON to confirm the issue is MessagePack-specific, then fix the contract."],"exampleFix":"// before - argument type not MessagePack-serializable on client\nhub.invoke(MyCustomType.class, \"Send\", new MyCustomType());\n\n// after - register symmetric formatters, or use JSON\n// server: services.AddSignalR().AddMessagePackProtocol();\n// client: ensure MyCustomType is MessagePack-compatible,\n// or\nHubConnectionBuilder.create(url)\n    .build(); // default JSON protocol on both ends","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"hub.onClosed(ex -> {\n  if (ex != null && ex.getMessage().contains(\"Error reading MessagePack data\")) {\n    // body parse failure; capture the failing payload, align contracts\n    log.error(\"MessagePack parse failure\", ex);\n  }\n});","preventionTips":["Ensure every hub argument type is MessagePack-serializable on both ends.","Register custom MessagePack formatters symmetrically or not at all.","Fall back to JSON protocol temporarily to isolate MessagePack-specific corruption."],"tags":["signalr","java","messagepack","protocol","serialization","parsing"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}