apache/cassandra · error · org.apache.cassandra.transport.ProtocolException

Cannot read value of length

Error message

Cannot read value of length %d, only %d bytes remaining in the message

What it means

CBUtil.readValueNoCopy reads a 4-byte length then, if non-negative, that many bytes as a read-only ByteBuffer view. If the declared length exceeds the remaining readable bytes, it throws ProtocolException reporting both the requested length and the bytes actually remaining. Guards value/[value] fields in CQL messages against truncation.

Solutions

  1. Reconnect to resynchronize the protocol stream
  2. Check intermediaries for frame truncation of large values (compressed frames especially)
  3. Verify the driver protocol version matches the server
  4. If building custom frames, ensure the 4-byte length equals the actual payload size
Defensive patterns

Strategy: try-catch

Try / catch

try { ... } catch (ProtocolException e) { if (e.getMessage().contains("bytes remaining in the message")) { reconnectSession(); } else throw e; }

Prevention

When it happens

Trigger: A value field in a QUERY/EXECUTE/BATCH message declares a length greater than the remaining bytes of the frame — truncated body or malformed length prefix.

Common situations: Oversized bound values cut off by intermediaries; driver bug writing wrong lengths; parsing a frame of the wrong protocol version.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/c87c7175665e5b4d. Report an issue: GitHub.

Appendix: source

Thrown at src/java/org/apache/cassandra/transport/CBUtil.java:476

        return ByteBuffer.wrap(readRawBytes(cb, length));
    }

    public static byte[] readValueAsBytes(ByteBuf cb)
    {
        int length = cb.readInt();
        if (length < 0)
            return null;

        return readRawBytes(cb, length);
    }

    public static ByteBuffer readValueNoCopy(ByteBuf cb)
    {
        int length = cb.readInt();
        if (length < 0)
            return null;
        if (length > cb.readableBytes())
            throw new ProtocolException(String.format("Cannot read value of length %d, only %d bytes remaining in the message",
                                                      length, cb.readableBytes()));

        ByteBuffer buffer = cb.nioBuffer(cb.readerIndex(), length);
        cb.skipBytes(length);
        return buffer;
    }

    public static ByteBuffer readBoundValue(ByteBuf cb, ProtocolVersion protocolVersion)
    {
        int length = cb.readInt();
        if (length < 0)
        {
            if (protocolVersion.isSmallerThan(ProtocolVersion.V4)) // backward compatibility for pre-version 4
                return null;
            if (length == -1)
                return null;
            else if (length == -2)
                return ByteBufferUtil.UNSET_BYTE_BUFFER;

View on GitHub (pinned to 88fd0f6a0e)