rtk-ai/rtk · error · anyhow::Error

unexpected end of stream

Error message

unexpected end of stream

What it means

This error is raised by the binlog reader's `read_exact` helper when a requested read of `len` bytes would extend past the end of the in-memory buffer (`end > cursor.get_ref().len()`). It means the .NET binary log (binlog) file is truncated or malformed: a record header promised more bytes than the file actually contains. The library throws instead of panicking or returning garbage bytes.

Solutions

  1. Verify the .binlog file is complete and uncorrupted (re-run `dotnet build -bl` to regenerate it); do not read a log while the build is still running.
  2. Confirm the input actually is a Binary Log (magic bytes) before parsing; passing an arbitrary file here produces bogus lengths and immediate truncation errors.
  3. Check reader/binlog format-version handling: if the file came from a newer .NET SDK, upgrade the tool so record headers are parsed with the correct offsets.
  4. Wrap parsing in error handling that treats 'unexpected end of stream' as an input-validity problem and falls back to raw `dotnet` output rather than failing the whole command.

Example fix

// before
let bytes = reader.read_exact(len as usize)?; // bails on truncated input

// after
let bytes = match reader.read_exact(len as usize) {
    Ok(b) => b,
    Err(_) => return fallback_to_raw_dotnet(args), // rtk fallback pattern
};
Defensive patterns

Strategy: fallback

Validate before calling

let meta = std::fs::metadata(path)?;
if meta.len() < 64 { bail!("file too small to be a binlog"); }
let mut f = std::fs::File::open(path)?;
let mut magic = [0u8; 7];
use std::io::Read;
f.read_exact(&mut magic)?; // fails fast if truncated
if &magic != b"!BinLog" { bail!("not a binlog file"); }

Try / catch

match read_binlog_summary(path) {
    Ok(summary) => summary,
    Err(e) if e.to_string().contains("unexpected end of stream") => {
        eprintln!("binlog truncated/corrupt, falling back to raw output");
        passthrough_raw()
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Any of the reader helpers (`skip`, `read_u8`, `read_i32_le`, `read_i64_le`, `read_dotnet_string`) request bytes whose end offset exceeds the buffer length — e.g. a length-prefixed string whose declared length runs past EOF, a fixed-size integer near EOF, or a `skip` past the last byte.

Common situations: Corrupted or partially downloaded/truncated .binlog file; a file that is not actually a binlog being fed to the parser; a binlog produced by a newer .NET SDK with record formats the reader misinterprets, causing bogus length fields; reading a file that is still being written by a concurrent `dotnet build`.

Understand the failure class

Background: "cannot parse invalid wire-format data", "cannot unmarshal", "failed unmarshalling": protobuf unmarshal errors explained — this error's family across 10 libraries.

Related errors


AI-assisted analysis of rtk-ai/rtk@0924356b4c (2026-09-19). Data as JSON: /api/errors/bcdac1fa21cdb4b2. Report an issue: GitHub.

Appendix: source

Thrown at src/cmds/dotnet/binlog.rs:580

    cursor: Cursor<&'a [u8]>,
}

impl<'a> BinReader<'a> {
    fn new(bytes: &'a [u8]) -> Self {
        Self {
            cursor: Cursor::new(bytes),
        }
    }

    fn is_eof(&self) -> bool {
        (self.cursor.position() as usize) >= self.cursor.get_ref().len()
    }

    fn read_exact(&mut self, len: usize) -> Result<&'a [u8]> {
        let start = self.cursor.position() as usize;
        let end = start.saturating_add(len);
        if end > self.cursor.get_ref().len() {
            anyhow::bail!("unexpected end of stream");
        }
        self.cursor.set_position(end as u64);
        Ok(&self.cursor.get_ref()[start..end])
    }

    fn skip(&mut self, len: usize) -> Result<()> {
        let _ = self.read_exact(len)?;
        Ok(())
    }

    fn read_u8(&mut self) -> Result<u8> {
        Ok(self.read_exact(1)?[0])
    }

    fn read_bool(&mut self) -> Result<bool> {
        Ok(self.read_u8()? != 0)
    }

View on GitHub (pinned to 0924356b4c)