dotnet/reactive · error · ArgumentNullException

ArgumentNullException

Error message

ArgumentNullException

What it means

The async-predicate Where overload (Func<TSource, ValueTask<bool>>) validates inputs eagerly and throws ArgumentNullException with param name 'source' when the source IAsyncObservable<TSource> is null. Note: with the parameterless ArgumentNullException constructor, the exception message does not name the parameter — the param name comes from the throw site.

Solutions

  1. Ensure the async source-producing method returns a non-null observable, not null.
  2. Null-check the awaited source before composing the query.
  3. Replace null returns in factories with AsyncObservable.Empty<TSource>().

Example fix

// before
var source = await GetSourceAsync(); // returns null
var filtered = source.Where(async x => await CheckAsync(x));
// after
var source = await GetSourceAsync() ?? AsyncObservable.Empty<int>();
var filtered = source.Where(async x => await CheckAsync(x));
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

try
{
    var filtered = source.Where(async x => await CheckAsync(x));
}
catch (ArgumentNullException ex) when (ex.ParamName == "source")
{
    // log the null source and use an empty observable
}

Prevention

When it happens

Trigger: Chaining .Where(asyncPredicate) on a null IAsyncObservable<TSource>; e.g. a null returned by a preceding custom async operator or an uninitialized variable.

Common situations: Async factory methods (Task<IAsyncObservable<T>>) whose await result was null; cached lookups missing; code paths composing queries before the source assignment completes.

Related errors


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

Appendix: source

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

    public partial class AsyncObservable
    {
        public static IAsyncObservable<TSource> Where<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.Where(observer, predicate)));
        }

        public static IAsyncObservable<TSource> Where<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.Where(observer, predicate)));
        }

        public static IAsyncObservable<TSource> Where<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)