dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'second')

Error message

Value cannot be null. (Parameter 'second')

What it means

WithLatestFrom requires a non-null `second` observable whose latest emitted value is paired with each value of the source. The extension method throws ArgumentNullException with parameter name 'second' when that argument is null, before creating the combined observable.

Solutions

  1. Provide a valid IAsyncObservable<TSecond> for `second` before building the pipeline.
  2. If the secondary stream may be absent, use an empty or never-completing observable rather than null.
  3. Initialize the secondary source earlier in startup so it is available when the pipeline is built.
  4. Add a null check with a clear error message where the second source is obtained.

Example fix

// before
var result = first.WithLatestFrom(second); // second is null
// after
second ??= AsyncObservable.Never<TSecond>();
var result = first.WithLatestFrom(second);
Defensive patterns

Strategy: validation

Validate before calling

if (second is null) throw new ArgumentNullException(nameof(second));
var result = first.WithLatestFrom(second);

Type guard

bool IsSecondValid<T>(IAsyncObservable<T> o) => o is not null;

Try / catch

try
{
    var result = first.WithLatestFrom(second);
}
catch (ArgumentNullException ex) when (ex.ParamName == "second")
{
    // substitute a never-completing stream so nothing is emitted from `second`
    var result = first.WithLatestFrom(AsyncObservable.Never<TSecond>());
}

Prevention

When it happens

Trigger: Calling first.WithLatestFrom(second) (the two-parameter overload) with a null second argument, e.g. when the secondary stream is loaded lazily/conditionally and is null at pipeline build time.

Common situations: Telemetry/UI scenarios where the 'other' stream comes from an optional service that failed to initialize; pipelines configured from config files with a missing secondary source key; refactors where the second observable was renamed and an old null default remained.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/WithLatestFrom.cs:18

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

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<(TFirst first, TSecond second)> WithLatestFrom<TFirst, TSecond>(this IAsyncObservable<TFirst> first, IAsyncObservable<TSecond> second)
        {
            if (first == null)
                throw new ArgumentNullException(nameof(first));
            if (second == null)
                throw new ArgumentNullException(nameof(second));

            return CreateAsyncObservable<(TFirst first, TSecond second)>.From(
                first,
                second,
                static async (first, second, observer) =>
                {
                    var (firstObserver, secondObserver) = AsyncObserver.WithLatestFrom(observer);

                    // REVIEW: Consider concurrent subscriptions.

                    var firstSubscription = await first.SubscribeSafeAsync(firstObserver).ConfigureAwait(false);
                    var secondSubscription = await second.SubscribeSafeAsync(secondObserver).ConfigureAwait(false);

                    return StableCompositeAsyncDisposable.Create(firstSubscription, secondSubscription);
                });
        }

        public static IAsyncObservable<TResult> WithLatestFrom<TFirst, TSecond, TResult>(this IAsyncObservable<TFirst> first, IAsyncObservable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)

View on GitHub (pinned to 94b5d5ab91)