apache/cassandra · error · MarshalException
The data cannot be deserialized as a set
Error message
The data cannot be deserialized as a set
What it means
SetSerializer.deserialize reads a 4-byte signed element count; a negative count cannot represent a valid set, so deserialization is aborted. This typically indicates bit-level corruption of the count field or reading bytes with the wrong layout/type. The guard prevents allocating a nonsensical collection.
Source
Thrown at src/java/org/apache/cassandra/serializers/SetSerializer.java:103
if (!accessor.isEmptyFromOffset(input, offset))
throw new MarshalException("Unexpected extraneous bytes after set value");
}
catch (BufferUnderflowException | IndexOutOfBoundsException e)
{
throw new MarshalException("Not enough bytes to read a set");
}
}
@Override
public <V> Set<T> deserialize(V input, ValueAccessor<V> accessor)
{
try
{
int n = readCollectionSize(input, accessor);
int offset = sizeOfCollectionSize();
if (n < 0)
throw new MarshalException("The data cannot be deserialized as a set");
// If the received bytes are not corresponding to a set, n might be a huge number.
// In such a case we do not want to initialize the set with that initialCapacity as it can result
// in an OOM when add is called (see CASSANDRA-12618). On the other hand we do not want to have to resize
// the set if we can avoid it, so we put a reasonable limit on the initialCapacity.
Set<T> l = new LinkedHashSet<>(Math.min(n, 256));
for (int i = 0; i < n; i++)
{
V value = readNonNullValue(input, accessor, offset);
offset += sizeOfValue(value, accessor);
elements.validate(value, accessor);
l.add(elements.deserialize(value, accessor));
}
if (!accessor.isEmptyFromOffset(input, offset))
throw new MarshalException("Unexpected extraneous bytes after set value" + l + "," + accessor.toHex(input));
return l;
}View on GitHub (pinned to 88fd0f6a0e)
Solutions
- Verify the column type matches the stored value (DESCRIBE table; check type metadata).
- Re-read the value; if reproducible, treat the cell as corrupt and rewrite it (scrub/repair).
- Ensure the buffer position starts at the value's beginning, not mid-value (a misaligned position flips the sign bit).
- Catch MarshalException in read paths and skip/log the corrupt value.
Example fix
// before
Set<Integer> tags = setSerializer.deserialize(cellValue, ByteBufferAccessor.instance);
// after
try {
Set<Integer> tags = setSerializer.deserialize(cellValue.duplicate(), ByteBufferAccessor.instance);
} catch (MarshalException e) {
logger.warn("Corrupt set cell ignored: {}", e.getMessage());
} Defensive patterns
Strategy: try-catch
Validate before calling
// Java
if (buf.remaining() >= 4) {
int n = ByteBufferUtil.toInt(buf.duplicate());
if (n < 0) throw new IllegalStateException("corrupt set count");
} Try / catch
try {
Set<T> s = setSerializer.deserialize(buf.duplicate(), ByteBufferAccessor.instance);
} catch (MarshalException e) {
log.warn("Corrupt set cell: {}", e.getMessage());
} Prevention
- Start deserialization at the value's start offset, not mid-value
- Confirm column metadata type equals the encoding of stored bytes
- Treat negative counts as corruption and rewrite the cell
When it happens
Trigger: Deserializing a buffer whose first 4 bytes, interpreted as an int, are negative — e.g. corrupt storage bytes, wrong column type, or reading a non-set value as a set.
Common situations: SSTable corruption; reading data written by a tool that does not use Cassandra's collection encoding; type confusion between column metadata and stored bytes.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- The data cannot be deserialized as a list
- Not enough bytes to read a set
- Unexpected extraneous bytes after set value
- Not enough bytes to read size of %dth field %s
- Not enough bytes to read %dth field %s
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/b83474886cf05b13.
Report an issue: GitHub.