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

  1. Check stream.CanWrite before calling SetLength; skip for read-only streams.
  2. Use a MemoryStream or PipeWriter for write paths instead of ReadOnlySequenceStream.
  3. 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

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


AI-assisted analysis of dotnet/orleans@fca799fa70 (2026-08-13). Data as JSON: /api/errors/3ed6cc0809b9114c. Report an issue: GitHub.