dotnet/reactive · error · NotSupportedException

Consecutive appends or prepends with a count of more than…

Error message

Consecutive appends or prepends with a count of more than int.MaxValue ({int.MaxValue}) are not supported.

What it means

When Append/Prepend operators are chained, each level tracks a cumulative count. Once the chain reaches int.MaxValue consecutive appends/prepends, constructing another level throws NotSupportedException because the count can no longer be incremented within an int.

Solutions

  1. Refactor to build the items into a collection first and use a single source.ToObservable() plus one Append
  2. Use Observable.Concat over a sequence of observables or a Subject/IObservable generator instead of chained Append/Prepend
  3. Cap or batch the number of consecutive appends

Example fix

// before
IObservable<int> src = Observable.Empty<int>();
for (int i = 0; i < n; i++) src = src.Append(i); // throws for huge n
// after
IObservable<int> src = Enumerable.Range(0, n).ToObservable();
Defensive patterns

Strategy: try-catch

Validate before calling

if (appendsPlanned >= int.MaxValue) throw new InvalidOperationException("Too many consecutive appends; restructure the stream");

Try / catch

try
{
    src = src.Append(item);
}
catch (NotSupportedException)
{
    // fall back to Observable.Concat over accumulated items
    src = accumulated.ToObservable();
}

Prevention

When it happens

Trigger: Chaining roughly 2,147,483,647 Append or Prepend calls on the same observable sequence; in practice, unbounded loops that keep calling source = source.Append(item) or source.Prepend(item) per iteration.

Common situations: Building a stream incrementally inside a long-running loop instead of using Create/Observable.Create or a Subject; recursive stream-construction code without a base case.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/d122422d8addce24. Report an issue: GitHub.

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable/AppendPrepend.cs:482

            public Node(T value)
                : this(null, value)
            {
            }

            public Node(Node<T>? parent, T value)
            {
                Parent = parent;
                Value = value;

                if (parent == null)
                {
                    Count = 1;
                }
                else
                {
                    if (parent.Count == int.MaxValue)
                    {
                        throw new NotSupportedException($"Consecutive appends or prepends with a count of more than int.MaxValue ({int.MaxValue}) are not supported.");
                    }

                    Count = parent.Count + 1;
                }
            }

            public T[] ToReverseArray()
            {
                var array = new T[Count];
                var current = this;
                for (var i = Count - 1; i >= 0; i--)
                {
                    array[i] = current!.Value; // NB: Count property ensures non-nullability.
                    current = current.Parent;
                }
                return array;
            }
        }

View on GitHub (pinned to 94b5d5ab91)