{"id":"86577a5959232fec","repo":"apache/kafka","slug":"record-for-partition-partition-topicpartition","errorCode":null,"errorMessage":"Record for partition ${partition.topicPartition()} at offset ${record.offset()} is invalid, cause: ${e.getMessage()}","messagePattern":"Record for partition (.+?) at offset (.+?) is invalid, cause: (.+?)","errorType":"exception","errorClass":"CorruptRecordException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/clients/consumer/internals/ShareCompletedFetch.java","lineNumber":431,"sourceCode":"    }\n\n    private void maybeEnsureValid(RecordBatch batch, boolean checkCrcs) {\n        if (checkCrcs && batch.magic() >= RecordBatch.MAGIC_VALUE_V2) {\n            try {\n                batch.ensureValid();\n            } catch (CorruptRecordException e) {\n                throw new CorruptRecordException(\"Record batch for partition \" + partition.topicPartition()\n                        + \" at offset \" + batch.baseOffset() + \" is invalid, cause: \" + e.getMessage());\n            }\n        }\n    }\n\n    private void maybeEnsureValid(final Record record, final boolean checkCrcs) {\n        if (checkCrcs) {\n            try {\n                record.ensureValid();\n            } catch (CorruptRecordException e) {\n                throw new CorruptRecordException(\"Record for partition \" + partition.topicPartition()\n                        + \" at offset \" + record.offset() + \" is invalid, cause: \" + e.getMessage());\n            }\n        }\n    }\n\n    private void maybeCloseRecordStream() {\n        if (records != null) {\n            records.close();\n            records = null;\n        }\n    }\n\n    private static class OffsetAndDeliveryCount {\n        final long offset;\n        final short deliveryCount;\n\n        OffsetAndDeliveryCount(long offset, short deliveryCount) {\n            this.offset = offset;","sourceCodeStart":413,"sourceCodeEnd":449,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ShareCompletedFetch.java#L413-L449","documentation":"CorruptRecordException re-thrown from ShareCompletedFetch.maybeEnsureValid when an individual Record (not the whole batch) fails record.ensureValid() under CRC checking. The message embeds the topic-partition and the record's own offset, letting you pinpoint the single offending record inside an otherwise-valid batch. It exists separately from the batch-level check because a batch header can be valid while an inner record is not.","triggerScenarios":"Polling a share consumer with check.crcs=true where the batch passes maybeEnsureValid but a subsequent record.ensureValid() throws CorruptRecordException. Happens while streaming records out of the batch iterator in ShareCompletedFetch.","commonSituations":"Same family as batch-level corruption but localized to one record: partial write that was fsync'd mid-record, decompression of a compacted/garbage-collected record that left an inconsistent size/offset, or an upstream producer that hit a serialization bug mid-batch. Also seen with custom interceptors or faulty compression codecs on the producer side.","solutions":["Retry the poll to rule out a transient transport-level bit flip.","Investigate the producer that wrote the specific offset; reproduce with kafka-console-consumer --property check.crcs=true against the same partition.","If the record is unrecoverable and blocking progress, skip it via the share consumer acknowledgement flow (acknowledge as RELEASE/ABANDON per your retry policy) and quarantine the offset for replay/reprocessing.","Audit producer serializers/compressors and broker disk for the affected segment."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// No pre-call validation: per-record CRC failure surfaces only during deserialization/iteration.\n// If you never want this exception, disable CRC checking (not recommended):\nprops.put(\"check.crcs\", \"false\");","typeGuard":null,"tryCatchPattern":"// Wrap record iteration so a single bad record does not poison the whole batch.\nimport org.apache.kafka.common.errors.CorruptRecordException;\n\nfor (ConsumerRecord<K,V> r : records) {\n    try {\n        process(r);\n    } catch (CorruptRecordException e) {\n        log.warn(\"Skipping corrupt record {}-{}: {}\",\n                 r.topic(), r.offset(), e.getMessage());\n        shareConsumer.acknowledge(...); // advance past the bad offset\n    }\n}","preventionTips":["Catch at the per-record scope, not the whole poll() — one corrupt record should not abort the batch.","Always acknowledge past a corrupt offset in share mode, otherwise the group re-fetches the same bad record forever.","Export corrupt-record counters to your alerting stack; spikes correlate with broker disk errors or producer serialization bugs.","Keep check.crcs=true (default); the CPU cost is trivial versus silent data loss."],"tags":["kafka","share-consumer","data-corruption","crc","record"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}