dotnet/reactive · error · ArgumentNullException

ArgumentNullException(nameof(observer))

Error message

ArgumentNullException(nameof(observer))

What it means

AsyncObserver.ToList(observer) converts a downstream observer into one that buffers elements into a List<TSource> and emits it on completion. The observer argument must be non-null; otherwise ArgumentNullException(nameof(observer)) is thrown synchronously. This is part of the internal observer-composition API.

Solutions

  1. Forward the actual observer from your Create<TSource,TResult> callback into AsyncObserver.ToList.
  2. Add an argument guard in your own code before composing observer chains.
  3. Rename shadowed variables so the lambda's observer parameter is the one passed on.

Example fix

// before
Create<int, IList<int>>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.ToList(obs))) // wrong var
// after
Create<int, IList<int>>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.ToList(observer)))
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

bool IsValidObserver<T>(IAsyncObserver<T> o) => o is not null;

Try / catch

try { var obs = AsyncObserver.ToList(observer); } catch (ArgumentNullException ex) when (ex.ParamName == "observer") { /* fix observer wiring */ }

Prevention

When it happens

Trigger: Calling AsyncObserver.ToList(null) — typically inside a custom Create-based operator where the observer was not forwarded from the subscribe callback.

Common situations: Hand-written operator plumbing where the observer parameter of the Create lambda was shadowed or dropped; test harnesses that forgot to instantiate a stub observer.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToList.cs:25

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

            return Create<TSource, IList<TSource>>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.ToList(observer)));
        }
    }

    public partial class AsyncObserver
    {
        public static IAsyncObserver<TSource> ToList<TSource>(IAsyncObserver<IList<TSource>> observer)
        {
            if (observer == null)
                throw new ArgumentNullException(nameof(observer));

            return Aggregate<TSource, List<TSource>, IList<TSource>>(observer, new List<TSource>(), (xs, x) => { xs.Add(x); return xs; }, xs => xs);
        }
    }
}

View on GitHub (pinned to 94b5d5ab91)