apache/hadoop · error · HadoopIllegalArgumentException

Cannot truncate to a negative file size: {}.

Error message

Cannot truncate to a negative file size: {}.

What it means

DFSClient.truncate() validates the target size client-side before any RPC: newLength < 0 is rejected with HadoopIllegalArgumentException. Negative sizes can only come from caller arithmetic (the protocol has no negative lengths), so this is a guard against underflow or unvalidated user input rather than a server condition.

Source

Thrown at hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSClient.java:1681

          QuotaByStorageTypeExceededException.class,
          FileAlreadyExistsException.class,
          FileNotFoundException.class,
          ParentNotDirectoryException.class,
          SafeModeException.class,
          NSQuotaExceededException.class,
          UnresolvedPathException.class,
          SnapshotAccessControlException.class);
    }
  }

  /**
   * Truncate a file to an indicated size
   * See {@link ClientProtocol#truncate}.
   */
  public boolean truncate(String src, long newLength) throws IOException {
    checkOpen();
    if (newLength < 0) {
      throw new HadoopIllegalArgumentException(
          "Cannot truncate to a negative file size: " + newLength + ".");
    }
    try (TraceScope ignored = newPathTraceScope("truncate", src)) {
      return namenode.truncate(src, newLength, clientName);
    } catch (RemoteException re) {
      throw re.unwrapRemoteException(AccessControlException.class,
          UnresolvedPathException.class);
    }
  }

  /**
   * Delete file or directory.
   * See {@link ClientProtocol#delete(String, boolean)}.
   */
  @Deprecated
  public boolean delete(String src) throws IOException {
    checkOpen();
    return delete(src, true);

View on GitHub (pinned to 2add963021)

Solutions

  1. Clamp before calling: newLength = Math.max(0L, Math.min(newLength, fileLen)) with fileLen freshly read.
  2. Validate sizes at your API/CLI boundary and reject negatives there with a clear message.
  3. Fix the subtraction site: re-read the file length immediately before computing the delta.

Example fix

// before
long target = fileLen - bytesToDrop; // negative when bytesToDrop > fileLen
boolean finished = dfs.truncate(path, target);

// after
long target = Math.max(0L, fileLen - bytesToDrop);
boolean finished = dfs.truncate(path, target);
Defensive patterns

Strategy: validation

Validate before calling

long fileLen = fs.getFileStatus(path).getLen();
long target = Math.max(0L, Math.min(newLength, fileLen));
if (newLength < 0) { throw new IllegalArgumentException("negative truncate size: " + newLength); }

Try / catch

catch (HadoopIllegalArgumentException e) {
  // surface the invalid newLength to the caller/config owner with the computed value
}

Prevention

When it happens

Trigger: Computing newLength = currentLen - bytesToDrop where bytesToDrop > currentLen (reads a stale length, or a concurrent truncate shrank the file first); passing a user-supplied size string like '-1' straight through; compensating logic subtracting more than the file holds.

Common situations: Interactive tools accepting user sizes without validation; retry/rollback code truncating back to an old offset computed from a different file version; unit tests probing boundary values.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/f7ce4de011962176. Report an issue: GitHub.