{"record":{"id":"7ade04010d55cd13","repo":"apache/flink","slug":"illegal-count-must-be-non-negative-s","errorCode":null,"errorMessage":"Illegal count (must be non-negative): %s","messagePattern":"Illegal count \\(must be non-negative\\): (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"flink-formats/flink-avro/src/main/java/org/apache/flink/formats/avro/utils/DataOutputEncoder.java","lineNumber":166,"sourceCode":"        out.write(0);\n    }\n\n    // --------------------------------------------------------------------------------------------\n    // union\n    // --------------------------------------------------------------------------------------------\n\n    @Override\n    public void writeIndex(int unionIndex) throws IOException {\n        out.writeInt(unionIndex);\n    }\n\n    // --------------------------------------------------------------------------------------------\n    // utils\n    // --------------------------------------------------------------------------------------------\n\n    public static void writeVarLongCount(DataOutput out, long val) throws IOException {\n        if (val < 0) {\n            throw new IOException(\"Illegal count (must be non-negative): \" + val);\n        }\n\n        while ((val & ~0x7FL) != 0) {\n            out.write(((int) val) | 0x80);\n            val >>>= 7;\n        }\n        out.write((int) val);\n    }\n}\n","sourceCodeStart":148,"sourceCodeEnd":176,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-formats/flink-avro/src/main/java/org/apache/flink/formats/avro/utils/DataOutputEncoder.java#L148-L176","documentation":"IOException from DataOutputEncoder.writeVarLongCount when asked to encode a negative length/count. The Avro encoder writes collection/array/map counts as zigzag-free non-negative varints; a negative count means the object being serialized reported a negative size, which indicates a bug or corrupted in-memory data, not a user config problem.","triggerScenarios":"Serializing an Avro record through DataOutputEncoder where an array/map/bytes field's size() returns a negative number; custom collection implementations with broken size(); concurrent mutation of the record during serialization.","commonSituations":"User-supplied collection classes whose size() overflows or is uninitialized; a record mutated by another thread while being checkpointed.","solutions":["Inspect the record being serialized at failure time — which field and what its collection size reports.","Use standard java.util collections and do not mutate records concurrently with serialization.","If the record looks valid, capture a minimal reproducer and report as a Flink/Avro bug."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    outEncoder.writeArrayStart();\n    outEncoder.setItemCount(items.size());\n    ...\n} catch (IOException e) {\n    if (e.getMessage().startsWith(\"Illegal count\")) {\n        // negative collection size: dump the record being serialized for debugging\n        log.error(\"Negative collection size on field; record={}\", record, e);\n    }\n    throw e;\n}","preventionTips":["Use standard java.util collections in Avro records; never hand-roll size().","Make records effectively immutable before checkpointing to avoid concurrent mutation."],"tags":["avro","serialization","internal","flink"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}