dotnet/reactive · error · ArgumentNullException

ArgumentNullException(nameof(plans))

Error message

ArgumentNullException(nameof(plans))

What it means

When composes concurrent AsyncPlan<TResult> streams using join semantics. It validates the plans sequence eagerly and throws ArgumentNullException with param name 'plans' when the IEnumerable<AsyncPlan<TResult>> is null. The throw occurs synchronously when the query is built, not when it is subscribed.

Solutions

  1. Pass a non-null IEnumerable<AsyncPlan<TResult>>, even an empty one.
  2. Initialize the plan collection before calling When (e.g. new List<AsyncPlan<int>>()).
  3. Coalesce at the call site: plans ?? Enumerable.Empty<AsyncPlan<int>>() if empty is acceptable.

Example fix

// before
List<AsyncPlan<int>> plans = null;
var result = AsyncObservable.When(plans);
// after
var plans = new List<AsyncPlan<int>> { plan1, plan2 };
var result = AsyncObservable.When(plans);
Defensive patterns

Strategy: validation

Validate before calling

if (plans == null)
    plans = Enumerable.Empty<AsyncPlan<TResult>>(); // or throw a descriptive error

Type guard

bool HasPlans<TResult>(IEnumerable<AsyncPlan<TResult>> plans) => plans is not null;

Try / catch

try
{
    var obs = AsyncObservable.When(plans);
}
catch (ArgumentNullException ex) when (ex.ParamName == "plans")
{
    // fall back to AsyncObservable.Empty<TResult>() or log a plan-building bug
}

Prevention

When it happens

Trigger: Calling AsyncObservable.When<TResult>(null) — a null plans collection, e.g. from a variable that was never assigned or a method returning null.

Common situations: Building plan lists dynamically (loops adding plans) where the list was never initialized; a factory method returning null instead of an empty collection; LINQ chains that can yield null.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/When.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.Collections.Generic;
using System.Reactive.Disposables;
using System.Reactive.Joins;
using System.Threading;

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<TResult> When<TResult>(IEnumerable<AsyncPlan<TResult>> plans)
        {
            if (plans == null)
                throw new ArgumentNullException(nameof(plans));

            return Create<TResult>(async observer =>
            {
                var externalSubscriptions = new Dictionary<object, IAsyncJoinObserver>();
                var gate = new AsyncGate();
                var activePlans = new List<ActiveAsyncPlan>();

                var outputObserver = AsyncObserver.Create<TResult>(
                    observer.OnNextAsync,
                    async ex =>
                    {
                        foreach (var subscription in externalSubscriptions.Values)
                        {
                            await subscription.DisposeAsync().ConfigureAwait(false);
                        }

                        await observer.OnErrorAsync(ex).ConfigureAwait(false);
                    },

View on GitHub (pinned to 94b5d5ab91)