dotnet/reactive · error · ArgumentOutOfRangeException

ArgumentOutOfRangeException

Error message

ArgumentOutOfRangeException

What it means

Buffer(source, count) also validates that `count` is strictly positive; count <= 0 throws ArgumentOutOfRangeException synchronously. A zero or negative batch size is meaningless because the operator would never (or instantly) complete a buffer.

Solutions

  1. Validate count > 0 before calling Buffer and surface a friendly message.
  2. Clamp or default: Math.Max(1, configuredSize).
  3. Fix the configuration/parsing path producing the non-positive value.

Example fix

// before
var batches = source.Buffer(batchSize); // batchSize from config, may be 0
// after
if (batchSize <= 0) throw new ArgumentException("Batch size must be positive", nameof(batchSize));
var batches = source.Buffer(batchSize);
Defensive patterns

Strategy: validation

Validate before calling

if (count <= 0) throw new ArgumentOutOfRangeException(nameof(count), "Count must be positive");

Type guard

static bool IsValidBufferSize(int count) => count > 0;

Try / catch

try { var batches = source.Buffer(count); }
catch (ArgumentOutOfRangeException ex) { logger.LogError(ex, "Buffer count must be positive"); }

Prevention

When it happens

Trigger: Calling source.Buffer(count) with count == 0 or count < 0 — typically a variable read from configuration, parsed input, or computed value that was not validated.

Common situations: Config files with batchSize: 0 or missing values parsed to 0; user-supplied batch sizes; off-by-one or division-derived sizes that can evaluate to zero.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Single.cs:99

        /// <summary>
        /// Projects each element of an observable sequence into consecutive non-overlapping buffers which are produced based on element count information.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence, and in the lists in the result sequence.</typeparam>
        /// <param name="source">Source sequence to produce buffers over.</param>
        /// <param name="count">Length of each buffer.</param>
        /// <returns>An observable sequence of buffers.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is less than or equal to zero.</exception>
        public static IObservable<IList<TSource>> Buffer<TSource>(this IObservable<TSource> source, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

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

            return s_impl.Buffer(source, count);
        }

        /// <summary>
        /// Projects each element of an observable sequence into zero or more buffers which are produced based on element count information.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence, and in the lists in the result sequence.</typeparam>
        /// <param name="source">Source sequence to produce buffers over.</param>
        /// <param name="count">Length of each buffer.</param>
        /// <param name="skip">Number of elements to skip between creation of consecutive buffers.</param>
        /// <returns>An observable sequence of buffers.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> or <paramref name="skip"/> is less than or equal to zero.</exception>
        public static IObservable<IList<TSource>> Buffer<TSource>(this IObservable<TSource> source, int count, int skip)
        {
            if (source == null)

View on GitHub (pinned to 94b5d5ab91)