dotnet/aspnetcore · error · RuntimeException

Error reading length header.

Error message

Error reading length header.

What it means

Wraps an IOException raised while reading the varint length prefix via Utils.readLengthHeader. It indicates an I/O-level failure at the very start of message framing, before the message body is read.

Source

Thrown at src/SignalR/clients/java/signalr/messagepack/src/main/java/com/microsoft/signalr/messagepack/MessagePackHubProtocol.java:86

         // MessagePack library can't handle read-only ByteBuffer - copy into an array-backed ByteBuffer if this is the case
        if (payload.isReadOnly()) {
            byte[] payloadBytes = new byte[payload.remaining()];
            payload.get(payloadBytes, 0, payloadBytes.length);
            payload = ByteBuffer.wrap(payloadBytes);
        }

        List<HubMessage> hubMessages = new ArrayList<>();

        while (payload.hasRemaining()) {
            int length;
            try {
                length = Utils.readLengthHeader(payload);
                // Throw if remaining buffer is shorter than length header
                if (payload.remaining() < length) {
                    throw new RuntimeException(String.format("MessagePack message was length %d but claimed to be length %d.", payload.remaining(), length));
                }
            } catch (IOException ex) {
                throw new RuntimeException("Error reading length header.", ex);
            }
            // Instantiate MessageUnpacker
            try(MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(payload)) {

                int itemCount = unpacker.unpackArrayHeader();
                HubMessageType messageType = HubMessageType.values()[unpacker.unpackInt() - 1];

                switch (messageType) {
                    case INVOCATION:
                        hubMessages.add(createInvocationMessage(unpacker, binder, itemCount, payload));
                        break;
                    case STREAM_ITEM:
                        hubMessages.add(createStreamItemMessage(unpacker, binder, payload));
                        break;
                    case COMPLETION:
                        hubMessages.add(createCompletionMessage(unpacker, binder, payload));
                        break;
                    case STREAM_INVOCATION:

View on GitHub (pinned to 294cab2f9b)

Solutions

  1. Verify the connection is healthy and not being torn down during reads.
  2. Ensure payloads handed to the protocol are well-formed MessagePack-with-length-prefix frames.
  3. Check that the transport fully reassembles frames before delivery.
Defensive patterns

Strategy: try-catch

Try / catch

// Java
try {
    hubConnection.start().blockingAwait();
} catch (RuntimeException ex) {
    if (ex.getMessage() != null && ex.getMessage().contains("length header")) {
        // connection likely torn down mid-frame; reconnect or inspect transport
    }
}

Prevention

When it happens

Trigger: IOException from the buffer/unpacker while reading the length-prefix bytes; a connection torn down mid-frame delivering only partial bytes.

Common situations: Connection dropped while a frame was in flight; malformed framing where the prefix is incomplete.

Related errors


AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06). Data as JSON: /api/errors/c7e0c53c072fbddd. Report an issue: GitHub.