dotnet/reactive · error · ArgumentNullException

throw new ArgumentNullException(nameof(observer));

Error message

throw new ArgumentNullException(nameof(observer));

What it means

AsyncObserver.TakeWhile (the observer-factory overload taking Func<TSource, bool>) throws ArgumentNullException when the downstream IAsyncObserver<TSource> is null. Observer factories in System.Reactive.Async validate both observer and predicate because the returned observer wraps the downstream observer and forwards emissions to it.

Solutions

  1. Pass the actual downstream observer received from the subscription callback instead of null.
  2. Null-check the observer before composing the operator chain and fail early with a clear message.
  3. When piping, always thread the observer argument through the lambda: (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.TakeWhile(observer, predicate)).

Example fix

// before
source.SubscribeSafeAsync(AsyncObserver.TakeWhile(null, x => x < 10));
// after
await source.SubscribeSafeAsync(AsyncObserver.TakeWhile(observer, x => x < 10));
Defensive patterns

Strategy: validation

Validate before calling

if (observer is null) throw new ArgumentNullException(nameof(observer));
var obs = AsyncObserver.TakeWhile(observer, predicate);

Type guard

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

Try / catch

try { var obs = AsyncObserver.TakeWhile(observer, pred); }
catch (ArgumentNullException ex) when (ex.ParamName == "observer") { /* propagate the real observer */ }

Prevention

When it happens

Trigger: Calling AsyncObserver.TakeWhile(observer, predicate) with a null observer, typically when composing custom subscription pipelines where the downstream observer comes from a factory or field that is null.

Common situations: Custom operator authoring where the observer parameter of a composed SubscribeSafeAsync callback was not propagated; test harnesses constructing observers manually.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeWhile.cs:69

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

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

    public partial class AsyncObserver
    {
        public static IAsyncObserver<TSource> TakeWhile<TSource>(IAsyncObserver<TSource> 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 =>
                {
                    bool b;

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

View on GitHub (pinned to 94b5d5ab91)