dotnet/reactive · error · ArgumentNullException

source

Error message

source

What it means

This is a standard ArgumentNullException raised synchronously by the DistinctUntilChanged extension method before any subscription happens. The library validates its arguments eagerly so that a null source is reported at the call site rather than surfacing later as a NullReferenceException inside the async subscription pipeline. Passing a null IAsyncObservable<TSource> is never valid.

Solutions

  1. Ensure the observable passed to DistinctUntilChanged is non-null before calling
  2. Check why the upstream producer returned null (failed factory, uninitialized field) and fix that
  3. Coalesce with AsyncObservable.Empty<TSource>() if a null source is legitimate in your domain

Example fix

// before
var result = source.DistinctUntilChanged(); // source may be null
// after
var result = (source ?? AsyncObservable.Empty<int>()).DistinctUntilChanged();
Defensive patterns

Strategy: validation

Validate before calling

if (source is null) throw new ArgumentNullException(nameof(source));

Type guard

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

Try / catch

try { var result = AsyncObservable.DistinctUntilChanged(src); }
catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* substitute empty or log */ }

Prevention

When it happens

Trigger: Calling AsyncObservable.DistinctUntilChanged(source) with source == null, e.g. a method returned null instead of an observable, or a nullable field was not initialized.

Common situations: Chaining operators where an earlier factory method returned null; conditional pipelines where the source variable is null on some code path; refactoring that removed a null check upstream.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/DistinctUntilChanged.cs:15

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information. 

using System.Collections.Generic;
using System.Threading.Tasks;

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

            return Create(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.DistinctUntilChanged(observer)));
        }

        public static IAsyncObservable<TSource> DistinctUntilChanged<TSource>(IAsyncObservable<TSource> source, IEqualityComparer<TSource> comparer)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (comparer == null)
                throw new ArgumentNullException(nameof(comparer));

            return Create(
                source,
                comparer,
                static (source, comparer, observer) => source.SubscribeSafeAsync(AsyncObserver.DistinctUntilChanged(observer, comparer)));
        }

        public static IAsyncObservable<TSource> DistinctUntilChanged<TSource, TKey>(IAsyncObservable<TSource> source, Func<TSource, TKey> keySelector)

View on GitHub (pinned to 94b5d5ab91)