dotnet/reactive · error · ArgumentNullException

throw new ArgumentNullException(nameof(source));

Error message

throw new ArgumentNullException(nameof(source));

What it means

The async TakeWhile(source, predicate) overload throws ArgumentNullException because the source observable argument was null. This overload accepts a Func<TSource, ValueTask<bool>> predicate for asynchronous filtering, but the source is still validated first at operator-construction time. The exception message names 'source'.

Solutions

  1. Coalesce the source: (source ?? AsyncObservable.Empty<TSource>()).TakeWhile(asyncPredicate).
  2. Fix the upstream factory to return a non-null (possibly empty) observable.
  3. Guard the composition point with a null check and a meaningful application-level error.

Example fix

// before
var taken = source.TakeWhile(async x => await CheckAsync(x));
// after
var taken = (source ?? AsyncObservable.Empty<int>()).TakeWhile(async x => await CheckAsync(x));
Defensive patterns

Strategy: validation

Validate before calling

if (source == null) source = AsyncObservable.Empty<TSource>();

Type guard

bool HasSource<T>(IAsyncObservable<T>? s) => s is not null;

Try / catch

try { var taken = source.TakeWhile(asyncPredicate); } catch (ArgumentNullException ex) when (ex.ParamName == "source") { var taken = AsyncObservable.Empty<TSource>(); }

Prevention

When it happens

Trigger: Calling AsyncObservable.TakeWhile(null, asyncPredicate) where asyncPredicate is a Func<TSource, ValueTask<bool>>.

Common situations: Async data pipelines where an upstream await returned null instead of a stream; factory methods that return null on empty results; DI-resolved observables that were not registered.

Related errors


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

Appendix: source

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

    public partial class AsyncObservable
    {
        public static IAsyncObservable<TSource> TakeWhile<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate)
        {
            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 static IAsyncObservable<TSource> TakeWhile<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate)
        {
            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 static IAsyncObservable<TSource> TakeWhile<TSource>(this IAsyncObservable<TSource> source, Func<TSource, int, bool> predicate)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (predicate == null)
                throw new ArgumentNullException(nameof(predicate));

            return CreateAsyncObservable<TSource>.From(
                source,

View on GitHub (pinned to 94b5d5ab91)