dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'source')

Error message

Value cannot be null. (Parameter 'source')

What it means

System.Reactive.Async's Repeat operator validates its arguments eagerly before building the async observable. When the source IAsyncObservable<TSource> is null, the guard in the source-less overload's sibling (Repeat(source)) throws ArgumentNullException naming the 'source' parameter. This fail-fast behavior surfaces the bug at the call site rather than deep inside the reactive pipeline.

Solutions

  1. Ensure the source observable is initialized before calling Repeat
  2. Check the method or factory that produced the source for a code path returning null
  3. Wrap construction in a null check or throw a descriptive exception at your own boundary
  4. If null is legitimate, use AsyncObservable.Empty<TSource>() instead

Example fix

// before
IAsyncObservable<int> src = maybeNull;
var repeated = src.Repeat();
// after
IAsyncObservable<int> src = maybeNull ?? AsyncObservable.Empty<int>();
var repeated = src.Repeat();
Defensive patterns

Strategy: validation

Validate before calling

if (source is null) throw new ArgumentNullException(nameof(source));
var repeated = source.Repeat();

Type guard

bool IsValidSource<TSource>(IAsyncObservable<TSource> s) => s is not null;

Try / catch

try { var repeated = source.Repeat(); }
catch (ArgumentNullException ex) when (ex.ParamName == "source")
{
    // source was null; supply AsyncObservable.Empty<TSource>() or fix producer
}

Prevention

When it happens

Trigger: Calling Repeat(source) (or any Repeat overload taking a source) with a null IAsyncObservable<TSource>, e.g. a variable that was never assigned, a factory method that returned null, or a null result of a previous operator chain.

Common situations: Passing the result of an optional lookup into a query pipeline; deserializing an observable configuration whose source property is missing; refactoring code so an observable field is initialized later than the query construction.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Repeat.cs:49

                throw new ArgumentNullException(nameof(repeatCount));

            return Create<TSource>(observer => AsyncObserver.Repeat(observer, value, repeatCount));
        }

        public static IAsyncObservable<TSource> Repeat<TSource>(TSource value, int repeatCount, IAsyncScheduler scheduler)
        {
            if (repeatCount < 0)
                throw new ArgumentNullException(nameof(repeatCount));
            if (scheduler == null)
                throw new ArgumentNullException(nameof(scheduler));

            return Create<TSource>(observer => AsyncObserver.Repeat(observer, value, repeatCount, scheduler));
        }

        public static IAsyncObservable<TSource> Repeat<TSource>(this IAsyncObservable<TSource> source)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));

            return Create(source, static (source, observer) => AsyncObserver.Repeat(observer, source));
        }

        public static IAsyncObservable<TSource> Repeat<TSource>(this IAsyncObservable<TSource> source, int repeatCount)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (repeatCount < 0)
                throw new ArgumentNullException(nameof(repeatCount));

            return CreateAsyncObservable<TSource>.From(
                source,
                repeatCount,
                static (source, repeatCount, observer) => AsyncObserver.Repeat(observer, source, repeatCount));
        }
    }

View on GitHub (pinned to 94b5d5ab91)