dotnet/orleans · warning · NotSupportedException
This stream is read-only.
Error message
This stream is read-only.
What it means
Thrown by ReadOnlySequenceStream.SetLength because the stream wraps a fixed ReadOnlySequence<byte> and is read-only; changing the length is meaningless and unsupported. The stream exposes CanWrite = false, so callers should respect that capability flag.
Source
Thrown at src/Azure/Orleans.Journaling.AzureStorage/ReadOnlySequenceStream.cs:85
return new ValueTask<int>(Read(buffer.Span));
}
public override long Seek(long offset, SeekOrigin origin)
{
ObjectDisposedException.ThrowIf(_disposed, this);
var newPosition = origin switch
{
SeekOrigin.Begin => offset,
SeekOrigin.Current => _position + offset,
SeekOrigin.End => _sequence.Length + offset,
_ => throw new ArgumentOutOfRangeException(nameof(origin))
};
Position = newPosition;
return _position;
}
public override void SetLength(long value) => throw new NotSupportedException("This stream is read-only.");
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException("This stream is read-only.");
public override void Write(ReadOnlySpan<byte> buffer) => throw new NotSupportedException("This stream is read-only.");
protected override void Dispose(bool disposing)
{
_disposed = true;
base.Dispose(disposing);
}
}
View on GitHub (pinned to fca799fa70)
Solutions
- Check stream.CanWrite before calling SetLength; skip for read-only streams.
- Use a MemoryStream or PipeWriter for write paths instead of ReadOnlySequenceStream.
- Refactor the consumer to branch on CanWrite/CanSeek capabilities.
Example fix
// before stream.SetLength(needed); // after if (stream.CanWrite) stream.SetLength(needed);
Defensive patterns
Strategy: type-guard
Validate before calling
if (stream.CanWrite) stream.SetLength(value);
Type guard
static bool IsWritableStream(Stream s) => s.CanWrite;
Prevention
- Check CanWrite before SetLength/Write.
- Use MemoryStream or PipeWriter for write paths.
- Keep read-only and writable stream types distinct.
When it happens
Trigger: Calling stream.SetLength(...) on a ReadOnlySequenceStream, typically via a generic serializer or stream utility that unconditionally calls SetLength when preparing a buffer.
Common situations: A third-party serializer/deserializer that calls SetLength to pre-size; code written for MemoryStream reused against this read-only stream; copy helpers that truncate before writing.
Related errors
- Specified argument was out of the range of valid values.
- Specified method is not supported.
- The journal id must not be the default value.
- The grain did not reactivate after DeactivateOnIdle.
- Recovered {name} count does not match. Written: {written.Cou
AI-assisted analysis of dotnet/orleans@fca799fa70 (2026-08-13).
Data as JSON: /api/errors/3ed6cc0809b9114c.
Report an issue: GitHub.