dotnet/reactive · error · ArgumentOutOfRangeException

ArgumentOutOfRangeException: bufferSize

Error message

ArgumentOutOfRangeException: bufferSize

What it means

Replay(source, bufferSize, scheduler) requires bufferSize >= 0 because it sizes the replay buffer of a ReplaySubject; a negative count is meaningless. A negative value raises ArgumentOutOfRangeException named 'bufferSize' before any subscription happens.

Solutions

  1. Clamp the buffer size: Math.Max(0, requestedSize).
  2. Validate settings at startup and fail fast with a clear message if negative.
  3. If unlimited replay is desired, use the parameterless Replay(source) overload instead of a huge/negative size.

Example fix

// before
int size = configuredMax - consumed; // can go negative
var replay = source.Replay(size, Scheduler.Default);
// after
int size = Math.Max(0, configuredMax - consumed);
var replay = source.Replay(size, Scheduler.Default);
Defensive patterns

Strategy: validation

Validate before calling

if (bufferSize < 0)
    throw new ArgumentOutOfRangeException(nameof(bufferSize), bufferSize, "Replay buffer size must be >= 0.");
var replay = source.Replay(bufferSize, scheduler);

Type guard

static int PositiveOrZero(int n) => Math.Max(0, n);

Try / catch

try { var replay = source.Replay(size, Scheduler.Default); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "bufferSize")
{
    var replay = source.Replay(0, Scheduler.Default); // or log and abort
}

Prevention

When it happens

Trigger: Calling source.Replay(bufferSize, scheduler) with a negative bufferSize, e.g. computed from a subtraction (count - dropped) or an unvalidated user/setting value.

Common situations: Config files or app settings where the buffer size was edited to a negative number; arithmetic like take(5) minus received(7); parsing user input without range validation.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Binding.cs:668

        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source sequence whose elements will be multicasted through a single shared subscription.</param>
        /// <param name="bufferSize">Maximum element count of the replay buffer.</param>
        /// <param name="scheduler">Scheduler where connected observers will be invoked on.</param>
        /// <returns>A connectable observable sequence that shares a single subscription to the underlying sequence.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="bufferSize"/> is less than zero.</exception>
        /// <remarks>Subscribers will receive all the notifications of the source subject to the specified replay buffer trimming policy.</remarks>
        /// <seealso cref="ReplaySubject{T}"/>
        public static IConnectableObservable<TSource> Replay<TSource>(this IObservable<TSource> source, int bufferSize, IScheduler scheduler)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (bufferSize < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(bufferSize));
            }

            if (scheduler == null)
            {
                throw new ArgumentNullException(nameof(scheduler));
            }

            return s_impl.Replay(source, bufferSize, scheduler);
        }

        /// <summary>
        /// Returns an observable sequence that is the result of invoking the selector on a connectable observable sequence that shares a single subscription to the underlying sequence replaying notifications subject to a maximum element count for the replay buffer.
        /// This operator is a specialization of Multicast using a <see cref="ReplaySubject{T}"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <typeparam name="TResult">The type of the elements in the result sequence.</typeparam>
        /// <param name="source">Source sequence whose elements will be multicasted through a single shared subscription.</param>
        /// <param name="selector">Selector function which can use the multicasted source sequence as many times as needed, without causing multiple subscriptions to the source sequence. Subscribers to the given source will receive all the notifications of the source subject to the specified replay buffer trimming policy.</param>

View on GitHub (pinned to 94b5d5ab91)