dotnet/reactive · error · ArgumentOutOfRangeException

capacity

Error message

capacity

What it means

This ArgumentOutOfRangeException is thrown synchronously when the capacity argument is negative. capacity sizes the internal dictionary of concurrently open groups, so a negative value is meaningless and rejected before any subscription occurs.

Solutions

  1. Pass a non-negative capacity (0 or greater).
  2. Clamp computed values before the call: capacity = Math.Max(0, computedCapacity).
  3. Validate configuration-sourced capacity at startup and fail fast with a clear message.

Example fix

// before
var grouped = source.GroupByUntil(ks, ds, config.MaxGroups - config.UsedGroups);
// after
var capacity = Math.Max(0, config.MaxGroups - config.UsedGroups);
var grouped = source.GroupByUntil(ks, ds, capacity);
Defensive patterns

Strategy: validation

Validate before calling

if (capacity < 0)
    throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be non-negative.");
var grouped = source.GroupByUntil(ks, ds, capacity);

Type guard

static bool IsValidCapacity(int capacity) => capacity >= 0;

Try / catch

try
{
    var grouped = source.GroupByUntil(ks, ds, capacity);
}
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "capacity")
{
    capacity = 0; // or log and retry with a valid capacity
    var grouped = source.GroupByUntil(ks, ds, capacity);
}

Prevention

When it happens

Trigger: Calling a capacity-taking GroupByUntil overload with capacity < 0, e.g. source.GroupByUntil(keySelector, durationSelector, -1) or a capacity value computed from user input or configuration that resolved to a negative number.

Common situations: Reading a capacity from config with a sign/datatype mistake; arithmetic like maxCount - expected producing -1 when the collection is empty; misunderstanding that 0 is allowed but negative is not.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/GroupByUntil.cs:201

            if (comparer == null)
                throw new ArgumentNullException(nameof(comparer));

            return CreateAsyncObservable<IGroupedAsyncObservable<TKey, TSource>>.From(
                source,
                (keySelector, durationSelector, comparer),
                static (source, state, observer) => GroupByUntilCore<TSource, TKey, TSource, TDuration>(source, observer, (o, d) => AsyncObserver.GroupByUntil(o, d, state.keySelector, state.durationSelector, state.comparer)));
        }

        public static IAsyncObservable<IGroupedAsyncObservable<TKey, TSource>> GroupByUntil<TSource, TKey, TDuration>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<IGroupedAsyncObservable<TKey, TSource>, IAsyncObservable<TDuration>> durationSelector, int capacity)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (keySelector == null)
                throw new ArgumentNullException(nameof(keySelector));
            if (durationSelector == null)
                throw new ArgumentNullException(nameof(durationSelector));
            if (capacity < 0)
                throw new ArgumentOutOfRangeException(nameof(capacity));

            return CreateAsyncObservable<IGroupedAsyncObservable<TKey, TSource>>.From(
                source,
                (keySelector, durationSelector, capacity),
                static (source, state, observer) => GroupByUntilCore<TSource, TKey, TSource, TDuration>(source, observer, (o, d) => AsyncObserver.GroupByUntil(o, d, state.keySelector, state.durationSelector, state.capacity)));
        }

        public static IAsyncObservable<IGroupedAsyncObservable<TKey, TSource>> GroupByUntil<TSource, TKey, TDuration>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, Func<IGroupedAsyncObservable<TKey, TSource>, IAsyncObservable<TDuration>> durationSelector, int capacity, IEqualityComparer<TKey> comparer)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (keySelector == null)
                throw new ArgumentNullException(nameof(keySelector));
            if (durationSelector == null)
                throw new ArgumentNullException(nameof(durationSelector));
            if (capacity < 0)
                throw new ArgumentOutOfRangeException(nameof(capacity));
            if (comparer == null)

View on GitHub (pinned to 94b5d5ab91)