{"id":"9dab223b96642793","repo":"apache/kafka","slug":"record-batch-for-partition-at-offset-is-inva-9dab22","errorCode":null,"errorMessage":"Record batch for partition {} at offset {} is invalid, cause: {}","messagePattern":"Record batch 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":420,"sourceCode":"                    lastRecord = record;\n                    break;\n                }\n            }\n        }\n\n        return records != null && records.hasNext();\n    }\n\n    private Optional<Integer> maybeLeaderEpoch(final int leaderEpoch) {\n        return leaderEpoch == RecordBatch.NO_PARTITION_LEADER_EPOCH ? Optional.empty() : Optional.of(leaderEpoch);\n    }\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) {","sourceCodeStart":402,"sourceCodeEnd":438,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ShareCompletedFetch.java#L402-L438","documentation":"CorruptRecordException re-thrown from ShareCompletedFetch.maybeEnsureValid when a fetched RecordBatch fails batch.ensureValid() with checkCrcs enabled. The message decorates the underlying cause with the topic-partition and the batch baseOffset so operators can localize the corruption. It indicates the on-wire batch did not pass CRC/size validation, i.e. the bytes the consumer received are not the bytes the broker stored.","triggerScenarios":"Polling a share consumer that fetches a batch whose magic is >= V2 while 'check.crcs' is true (default) and batch.ensureValid() throws CorruptRecordException. Reached on every iteration inside the fetch streaming loop of ShareCompletedFetch.","commonSituations":"Disk or page-cache bit rot on the broker, network/SSL corruption in transit, a transient OS-level I/O error on the broker log segment, or a client pointed at a stale/failing broker replica. Can also appear after an unclean leader election or partial segment truncation where headers and payload disagree.","solutions":["Retry the poll; transient corruption in transit often clears on the next fetch.","If persistent, set check.crcs=false only to bypass (not recommended long-term) and verify with a fresh fetch from a different broker/replica.","Inspect broker logs and disk health for the affected partition/offset range; restore the segment from a healthy replica or recreate the topic data.","Confirm broker and client versions match to rule out a record-format incompatibility."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// No pre-call validation possible: corruption is detected by CRC at read time.\n// The only knob is whether the client checks at all.\nprops.put(\"check.crcs\", \"false\"); // disables this check entirely (trade-off documented below)","typeGuard":null,"tryCatchPattern":"// CorruptRecordException is recoverable: skip and acknowledge, do not block the share group.\nimport org.apache.kafka.common.errors.CorruptRecordException;\n\ntry {\n    ConsumerRecords<K,V> records = shareConsumer.poll(Duration.ofSeconds(5));\n    // ... process ...\n} catch (CorruptRecordException e) {\n    // The fetch's partition/offset is in the message; acknowledge to advance.\n    log.error(\"Corrupt batch detected, skipping: {}\", e.getMessage(), e);\n    shareConsumer.acknowledge(e.offsetAndMetadata()); // or context-appropriate ack\n}","preventionTips":["Corruption usually means disk/network/broker faults — surface it to ops, do not swallow it silently.","Set check.crcs=true in production (the default); only disable in trusted LANs where CPU matters more than silent corruption.","Track corrupt-offset metrics so you can correlate with broker disk failures or producer bugs.","Wire CorruptRecordException to your dead-letter / skip pipeline so the share group keeps progressing."],"tags":["kafka","share-consumer","data-corruption","crc","fetch"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}