dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'observer')

Error message

Value cannot be null. (Parameter 'observer')

What it means

The AsyncObserver.All factory (sync-predicate overload) creates the observer that implements the All operator and first validates its arguments. A null downstream observer means there is nowhere to deliver the resulting bool or error, so ArgumentNullException('observer') is thrown. This is an internal pipeline API; it is normally only hit when composing observers manually.

Solutions

  1. Pass a non-null IAsyncObserver<bool>, typically obtained from CreateAsyncObserver or your sink implementation.
  2. Construct the observer before building the All observer; never pass the result of an uninitialized factory call.
  3. If you own wrapper code, assert the observer is non-null before forwarding to AsyncObserver.All.

Example fix

// before
IAsyncObserver<bool> sink = GetObserver(); // may return null
var obs = AsyncObserver.All(source, sink, pred);
// after
IAsyncObserver<bool> sink = GetObserver() ?? throw new InvalidOperationException("no sink");
var obs = AsyncObserver.All(source, sink, pred);
Defensive patterns

Strategy: validation

Validate before calling

if (observer == null) throw new InvalidOperationException("downstream observer must be constructed before AsyncObserver.All");
var obs = AsyncObserver.All(source, observer, predicate);

Type guard

bool HasObserver<TSource>(IAsyncObserver<TSource>? o) => o is not null;

Try / catch

try { var obs = AsyncObserver.All(source, observer, predicate); }
catch (ArgumentNullException ex) when (ex.ParamName == "observer") { /* build the observer first */ }

Prevention

When it happens

Trigger: Calling AsyncObserver.All<TSource>(null, predicate) directly, or wiring a custom subscription pipeline where the downstream IAsyncObserver<bool> was not yet constructed.

Common situations: Hand-rolling operator composition or tests that build AsyncObserver chains, where the observer argument is produced by another factory that returned null.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/All.cs:43

        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (predicate == null)
                throw new ArgumentNullException(nameof(predicate));

            return CreateAsyncObservable<bool>.From(
                source,
                predicate,
                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.All(observer, predicate)));
        }
    }

    public partial class AsyncObserver
    {
        public static IAsyncObserver<TSource> All<TSource>(IAsyncObserver<bool> observer, Func<TSource, bool> predicate)
        {
            if (observer == null)
                throw new ArgumentNullException(nameof(observer));
            if (predicate == null)
                throw new ArgumentNullException(nameof(predicate));

            return Create<TSource>(
                async x =>
                {
                    var b = default(bool);

                    try
                    {
                        b = predicate(x);
                    }
                    catch (Exception ex)
                    {
                        await observer.OnErrorAsync(ex).ConfigureAwait(false);
                        return;
                    }

View on GitHub (pinned to 94b5d5ab91)