tursodatabase/turso · error · IOException
An attempt was made to seek before the beginning of the…
Error message
An attempt was made to seek before the beginning of the stream.
What it means
After resolving the target position (offset from Begin/Current/End), SqliteBlob.Seek requires the result to be non-negative; a negative position is impossible for the underlying MemoryStream, so it throws IOException (Properties.Resources.SeekBeforeBegin), matching System.IO stream semantics: seeking before the start is an I/O-level error, not an argument error.
Solutions
- Compute the target position yourself first and handle the negative case before calling Seek.
- For rewinds, prefer an absolute Seek(x, SeekOrigin.Begin) over large negative relative offsets.
- Base rewind amounts on bytes actually read (stream.Position), not on assumed layout sizes.
Example fix
// before blob.Seek(-16, SeekOrigin.Current); // fewer than 16 bytes were read -> negative position // after long rewind = Math.Min(16, blob.Position); blob.Seek(-rewind, SeekOrigin.Current);
Defensive patterns
Strategy: validation
Validate before calling
long target = origin switch {
SeekOrigin.Begin => offset,
SeekOrigin.Current => blob.Position + offset,
SeekOrigin.End => blob.Length + offset,
_ => throw new ArgumentOutOfRangeException(nameof(origin))
};
if (target < 0) throw new IOException("seek before begin");
blob.Seek(offset, origin); Try / catch
try {
blob.Seek(offset, origin);
} catch (IOException) when (origin != SeekOrigin.Begin && offset < 0) {
// record shorter than the fixed header expects: treat as malformed
} Prevention
- Derive rewind amounts from bytes actually read, never from struct sizes.
- Use absolute Begin-based seeks for fixed-layout records.
- Validate relative-seek math against Position/Length before calling Seek.
When it happens
Trigger: Seek(-n, SeekOrigin.Current) with n greater than the current Position; Seek(-n, SeekOrigin.End) with n greater than the blob length; passing a negative offset with SeekOrigin.Begin.
Common situations: Generic stream helpers that rewind by struct sizes (e.g., Seek(-sizeof(Header), Current)) after fewer bytes were actually read; parsers whose offset math was tuned to a different file layout; reuse of relative-seek code on short blobs.
Related errors
- Invalid value for enum type .
- Error reading ASCII stream
- Error reading binary stream
- Error reading Unicode stream
- Offset and count were out of bounds for the array.
AI-assisted analysis of tursodatabase/turso@244cde92a7 (2026-08-20).
Data as JSON: /api/errors/777e2cf69cddfeef.
Report an issue: GitHub.
Appendix: source
Thrown at bindings/dotnet/src/Turso.Data.Sqlite/SqliteBlob.cs:80
public override int Read(byte[] buffer, int offset, int count)
{
ValidateBuffer(buffer, offset, count);
return GetStream().Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
var stream = GetStream();
var position = origin switch
{
SeekOrigin.Begin => offset,
SeekOrigin.Current => stream.Position + offset,
SeekOrigin.End => stream.Length + offset,
_ => throw new ArgumentException(Properties.Resources.InvalidEnumValue(typeof(SeekOrigin), origin), nameof(origin))
};
if (position < 0)
throw new IOException(Properties.Resources.SeekBeforeBegin);
stream.Position = position;
return position;
}
public override void SetLength(long value)
{
throw new NotSupportedException(Properties.Resources.ResizeNotSupported);
}
public override void Write(byte[] buffer, int offset, int count)
{
if (_readOnly)
throw new NotSupportedException(Properties.Resources.WriteNotSupported);
ValidateBuffer(buffer, offset, count);
if (count == 0)
return;View on GitHub (pinned to 244cde92a7)