{"record":{"id":"8a5c348cf09aa418","repo":"apache/hadoop","slug":"unexpected-block-size-numbytes","errorCode":null,"errorMessage":"Unexpected block size: ${numBytes}","messagePattern":"Unexpected block size: (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/protocol/Block.java","lineNumber":229,"sourceCode":"  }\n\n  @Override // Writable\n  public void readFields(DataInput in) throws IOException {\n    readHelper(in);\n  }\n\n  final void writeHelper(DataOutput out) throws IOException {\n    out.writeLong(blockId);\n    out.writeLong(numBytes);\n    out.writeLong(generationStamp);\n  }\n\n  final void readHelper(DataInput in) throws IOException {\n    this.blockId = in.readLong();\n    this.numBytes = in.readLong();\n    this.generationStamp = in.readLong();\n    if (numBytes < 0) {\n      throw new IOException(\"Unexpected block size: \" + numBytes);\n    }\n  }\n\n  // write only the identifier part of the block\n  public void writeId(DataOutput out) throws IOException {\n    out.writeLong(blockId);\n    out.writeLong(generationStamp);\n  }\n\n  // Read only the identifier part of the block\n  public void readId(DataInput in) throws IOException {\n    this.blockId = in.readLong();\n    this.generationStamp = in.readLong();\n  }\n\n  /**\n   * Compares this Block with the specified Block for order. Returns a negative\n   * integer, zero, or a positive integer as this Block is less than, equal to,","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/protocol/Block.java#L211-L247","documentation":"Thrown by Block.readHelper after deserializing a block (blockId, numBytes, generationStamp) from a DataInput stream when numBytes is negative. Block is the wire representation of an HDFS block, so a negative size means the bytes just read are not a valid Block encoding. This is a data-integrity guard against a corrupt or misaligned stream, not a user-input error.","triggerScenarios":"Deserializing a Block from an IPC reply or data-transfer stream that is corrupt, truncated, or out of frame: e.g. a client/server version mismatch where the two sides serialize Block differently, garbage read after a protocol desync, or a partially-flipped buffer from a failing disk or NIC on the DataNode.","commonSituations":"Mixed Hadoop versions (rolling upgrade with incompatible wire formats), network-level corruption (bad NIC/cable, TCP proxy mangling frames), reading a block report or edit log segment that is truncated. Rarely, faulty direct-byte-buffer or checksum bugs in a custom DataNode.","solutions":["Verify client and server run compatible Hadoop versions (same major line for hdfs-client wire protocol); align versions and retry.","Check DataNode logs for disk read errors or checksum failures on the blockId reported in the message; evict/reschedule the replica if the media is bad.","Inspect the network path (switch, NIC, offloaded checksums/TOE) between client and DataNode for corruption; disable suspect offloads and retry.","If using a proxy or custom IPC layer, verify it does not splice or reorder bytes in the data-transfer channel."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  block.readFields(in);\n} catch (IOException e) {\n  if (e.getMessage().contains(\"Unexpected block size\")) {\n    // stream corrupt / version mismatch — do not retry the same stream\n    throw new DataCorruptionException(\"bad Block encoding\", e);\n  }\n  throw e;\n}","preventionTips":["Keep client and server Hadoop versions within the documented wire-compatibility window during rolling upgrades.","Monitor DataNode disk and checksum metrics so corrupt replicas are evicted before clients read them.","Avoid custom splicing of the data-transfer socket; if unavoidable, add integrity checks (e.g. TCP checksums are weak — use MACs on sensitive links)."],"tags":["hdfs","serialization","data-corruption","wire-protocol"],"backgroundTag":"corrupt-data-deserialization","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}