dotnet/reactive · error · ArgumentNullException

ArgumentNullException

Error message

ArgumentNullException

What it means

System.Reactive.Async's While operator requires a synchronous condition delegate and a source observable. The library throws ArgumentNullException immediately when either is null, failing fast at call time rather than producing an erroring observable. This protects the returned operator from undefined behavior during subscription.

Solutions

  1. Ensure the condition delegate is a non-null Func<bool> (e.g. () => i < 10).
  2. Ensure the source argument is a valid, non-null IAsyncObservable<TSource> before passing it.
  3. If the source may come from a nullable expression, coalesce it with an empty observable or throw a descriptive error at your own boundary.
  4. Wrap the call in ArgumentNullException handling only at top-level boundaries where inputs are external.

Example fix

// before
var res = AsyncObservable.While(cond, maybeSource);
// after
if (maybeSource == null) maybeSource = AsyncObservable.Empty<int>();
var res = AsyncObservable.While(() => cond(), maybeSource);
Defensive patterns

Strategy: validation

Validate before calling

if (condition == null) throw new ArgumentNullException(nameof(condition));
if (source == null) throw new ArgumentNullException(nameof(source));
var res = AsyncObservable.While(condition, source);

Type guard

static bool IsValidWhileArgs<TSource>(Func<bool> condition, IAsyncObservable<TSource> source)
    => condition is not null && source is not null;

Try / catch

try { var res = AsyncObservable.While(condition, source); }
catch (ArgumentNullException ex) when (ex.ParamName is "condition" or "source")
{
    // log and fall back to Empty or rethrow with context
}

Prevention

When it happens

Trigger: Calling AsyncObservable.While with a null condition Func<bool>, or with a null IAsyncObservable<TSource> source (e.g. a factory method returned null).

Common situations: Passing the result of a method that returned null instead of an observable; typos where a lambda variable is still null; refactoring away a default source without updating the call site.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/While.cs:17

// 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.Reactive.Disposables;
using System.Threading.Tasks;

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        // REVIEW: Use a tail-recursive sink.

        public static IAsyncObservable<TSource> While<TSource>(Func<bool> condition, IAsyncObservable<TSource> source)
        {
            if (condition == null)
                throw new ArgumentNullException(nameof(condition));
            if (source == null)
                throw new ArgumentNullException(nameof(source));

            return Create<TSource>(async observer =>
            {
                var subscription = new SerialAsyncDisposable();

                var o = default(IAsyncObserver<TSource>);

                o = AsyncObserver.CreateUnsafe<TSource>(
                        observer.OnNextAsync,
                        observer.OnErrorAsync,
                        MoveNext
                    );

                async ValueTask MoveNext()
                {
                    var b = default(bool);

View on GitHub (pinned to 94b5d5ab91)