{"record":{"id":"e772f0442dafd4ee","repo":"dotnet/aspnetcore","slug":"error-writing-messagepack-data","errorCode":null,"errorMessage":"Error writing MessagePack data.","messagePattern":"Error writing 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":189,"sourceCode":"                default:\n                    throw new RuntimeException(String.format(\"Unexpected message type: %d\", messageType.value));\n            }\n            int length = message.length;\n            List<Byte> header = Utils.getLengthHeader(length);\n            byte[] messageWithHeader = new byte[header.size() + length];\n            int headerSize = header.size();\n\n            // Write the length header, then all of the bytes of the original message\n            for (int i = 0; i < headerSize; i++) {\n                messageWithHeader[i] = header.get(i);\n            }\n            for (int i = 0; i < length; i++) {\n                messageWithHeader[i + headerSize] = message[i];\n            }\n\n            return ByteBuffer.wrap(messageWithHeader);\n        } catch (MessagePackException | IOException ex) {\n            throw new RuntimeException(\"Error writing MessagePack data.\", ex);\n        }\n    }\n\n    private HubMessage createInvocationMessage(MessageUnpacker unpacker, InvocationBinder binder, int itemCount, ByteBuffer payload) throws IOException {\n        Map<String, String> headers = readHeaders(unpacker);\n\n        // invocationId may be nil\n        String invocationId = null;\n        if (!unpacker.tryUnpackNil()) {\n            invocationId = unpacker.unpackString();\n        }\n\n        // For MsgPack, we represent an empty invocation ID as an empty string,\n        // so we need to normalize that to \"null\", which is what indicates a non-blocking invocation.\n        if (invocationId == null || invocationId.isEmpty()) {\n            invocationId = null;\n        }\n","sourceCodeStart":171,"sourceCodeEnd":207,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/SignalR/clients/java/signalr/messagepack/src/main/java/com/microsoft/signalr/messagepack/MessagePackHubProtocol.java#L171-L207","documentation":"writeMessage wraps any MessagePackException or IOException raised while serializing a hub message's bytes into a RuntimeException with this message. It is a generic catch-all for an underlying serialization/packing failure of an InvocationMessage, StreamItem, Completion, etc. The original exception is attached as the cause, so the real reason (e.g. Jackson cannot serialize a type) is in ex.getCause().","triggerScenarios":"Any I/O or msgpack-core failure inside the try at MessagePackHubProtocol.java:147-188: a MessageBufferPacker write error, a Jackson ObjectMapper failure encoding an argument that has no MessagePack serializer, or a null field the packer cannot handle.","commonSituations":"Passing a non-serializable argument (e.g. a type with no default constructor / Jackson-incompatible), version drift in the msgpack/jackson-databind dependencies, or a corrupted ObjectMapper instance.","solutions":["Inspect ex.getCause() in the debugger/logs to find the underlying MessagePackException/IOException.","Replace the offending argument type with one Jackson can serialize to MessagePack (POJO with default constructor, primitives, collections).","Pin compatible versions of org.msgpack:msgpack-core and org.msgpack:msgpack-jackson-dataformat."],"exampleFix":"// before\nprotocol.writeMessage(new InvocationMessage(headers, id, target, new Object[] { unsupportedType }));\n// after - serialize a POJO Jackson can handle\npublic class MyArg { public String a; public int b; }\nprotocol.writeMessage(new InvocationMessage(headers, id, target, new Object[] { new MyArg() }));","handlingStrategy":"try-catch","validationCode":"// Verify arguments are Jackson-MessagePack-serializable before sending\nObjectMapper mp = new ObjectMapper(new MessagePackFactory());\nfor (Object arg : args) {\n    try { mp.writeValueAsBytes(arg); }\n    catch (Exception e) { throw new IllegalArgumentException(\"Unserializable argument \" + arg, e); }\n}","typeGuard":"// POJO with no-arg constructor and Jackson-friendly fields is the safe shape\nstatic boolean isLikelySerializable(Object o) {\n    return o == null || o instanceof Number || o instanceof CharSequence || o instanceof Boolean\n        || o instanceof Collection || o instanceof Map\n        || (o.getClass().getDeclaredConstructors().length > 0); // rough\n}","tryCatchPattern":"try {\n    protocol.writeMessage(message);\n} catch (RuntimeException ex) {\n    Throwable cause = ex.getCause();\n    // log cause (MessagePackException / IOException) to find the offending field\n    log.error(\"writeMessage failed: {}\", cause, ex);\n    throw ex;\n}","preventionTips":["Keep hub method argument types as simple POJOs with a default constructor.","Pin compatible msgpack-core / msgpack-jackson-dataform versions.","Always inspect getCause() when this generic message appears."],"tags":["java","signalr","messagepack","serialization","jackson"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}