vectordotdev/vector · error

cannot extend a file through the truncation API

Error message

cannot extend a file through the truncation API

What it means

The disk v2 buffer's file abstraction exposes truncate only to shrink a file. This error is raised by truncate when the requested size is larger than the file's current length, since growing through the truncation API is not supported. It is returned as an InvalidInput io error.

Solutions

  1. Only call truncate with a size <= the file's current length; check metadata().len() first.
  2. If growth is intended, use the file's write/extend path instead of truncate.
  3. Re-fetch current size immediately before truncating to avoid acting on stale metadata.

Example fix

// before
file.truncate(new_size).await?; // new_size may exceed len
// after
let len = file.metadata().await?.len();
if new_size <= len {
    file.truncate(new_size).await?;
}
Defensive patterns

Strategy: validation

Validate before calling

// guard truncation calls
async fn safe_truncate(file: &BufferFile, size: u64) -> io::Result<()> {
    let len = file.metadata().await?.len();
    if size > len {
        return Err(io::Error::new(io::ErrorKind::InvalidInput, "size exceeds file length"));
    }
    file.truncate(size).await
}

Try / catch

match file.truncate(size).await {
    Err(e) if e.kind() == io::ErrorKind::InvalidInput => {
        // tried to grow via truncate; use write path instead
    }
    other => other?,
}

Prevention

When it happens

Trigger: Calling File::truncate(size) with size > current file length; a caller computing a truncation target from stale metadata after the file has grown or been compacted.

Common situations: Buffer recovery/compaction logic racing with concurrent writes; a bug or off-by-one in code computing the retained byte offset.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of vectordotdev/vector@bdb87aeaa4 (2026-09-16). Data as JSON: /api/errors/321bec8f693fdf44. Report an issue: GitHub.

Appendix: source

Thrown at lib/vector-buffers/src/variants/disk_v2/io.rs:302

/// Builds a set of `OpenOptions` for opening a file as readable.
fn open_readable_file_options() -> OpenOptions {
    let mut open_options = OpenOptions::new();
    open_options.read(true);
    open_options
}

impl AsyncFile for tokio::fs::File {
    async fn metadata(&self) -> io::Result<Metadata> {
        let metadata = self.metadata().await?;
        Ok(Metadata {
            len: metadata.len(),
        })
    }

    async fn truncate(&self, size: u64) -> io::Result<()> {
        let current_size = self.metadata().await?.len();
        if size > current_size {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "cannot extend a file through the truncation API",
            ));
        }

        self.set_len(size).await
    }

    async fn sync_all(&self) -> io::Result<()> {
        self.sync_all().await
    }
}

impl ReadableMemoryMap for memmap2::Mmap {}

impl ReadableMemoryMap for memmap2::MmapMut {}

impl WritableMemoryMap for memmap2::MmapMut {

View on GitHub (pinned to bdb87aeaa4)