dotnet/reactive · error · ArgumentOutOfRangeException

count

Error message

count

What it means

ArgumentOutOfRangeException whose parameter name is 'count', thrown by TakeLastBuffer's range guard: the number of trailing elements to buffer must be non-negative. A negative count makes the end-of-sequence window undefined, so the operator rejects it up front.

Solutions

  1. Pass a non-negative count (0 is allowed and yields an empty buffer).
  2. Clamp: Math.Max(0, n) before the call.
  3. Validate the configured buffer size at startup.

Example fix

// before
var buf = source.TakeLastBuffer(size); // can be -1
// after
var buf = source.TakeLastBuffer(Math.Max(0, size));
Defensive patterns

Strategy: validation

Validate before calling

if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));
source.TakeLastBuffer(count);

Try / catch

try { var buf = source.TakeLastBuffer(count); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "count") { /* clamp to 0 */ }

Prevention

When it happens

Trigger: source.TakeLastBuffer(-1) — negative values from subtraction, unsigned conversion, or unvalidated configuration.

Common situations: Buffer size computed as newer - older with wrong order; config-driven batch size that went negative; sentinel value -1 for 'unset'.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeLastBuffer.cs:18

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information. 

using System.Collections.Generic;
using System.Reactive.Concurrency;
using System.Threading.Tasks;

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<IList<TSource>> TakeLastBuffer<TSource>(this IAsyncObservable<TSource> source, int count)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (count < 0)
                throw new ArgumentOutOfRangeException(nameof(count));

            if (count == 0)
            {
                return Empty<IList<TSource>>();
            }

            return CreateAsyncObservable<IList<TSource>>.From(
                source,
                count,
                static (source, count, observer) => source.SubscribeSafeAsync(AsyncObserver.TakeLastBuffer(observer, count)));
        }

        public static IAsyncObservable<IList<TSource>> TakeLastBuffer<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (duration < TimeSpan.Zero)
                throw new ArgumentOutOfRangeException(nameof(duration));

View on GitHub (pinned to 94b5d5ab91)