dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'source')

Error message

Value cannot be null. (Parameter 'source')

What it means

The Synchronize extension serializes notifications from the source through an AsyncGate and first validates that the source observable is non-null, throwing ArgumentNullException at Synchronize.cs:14. Eager validation keeps the failure at the composition call site instead of during subscription.

Solutions

  1. Ensure the source observable is initialized before calling .Synchronize()
  2. If the source comes from DI or a registry, validate the resolution result and fail fast with a clear message
  3. Check for race conditions where the pipeline is built before the source is assigned
  4. Add a null guard with a descriptive exception at the point the source is obtained

Example fix

// before
var sync = registry.GetObservable(key).Synchronize(); // GetObservable may return null
// after
var src = registry.GetObservable(key) ?? throw new InvalidOperationException($"no observable registered for {key}");
var sync = src.Synchronize();
Defensive patterns

Strategy: validation

Validate before calling

if (source is null)
    throw new InvalidOperationException("Synchronize requires a non-null source observable.");
var sync = source.Synchronize();

Type guard

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

Try / catch

try
{
    var sync = source.Synchronize();
}
catch (ArgumentNullException ex) when (ex.ParamName == "source")
{
    logger.LogError(ex, "Synchronize called with null source; verify initialization order/DI binding");
}

Prevention

When it happens

Trigger: Calling source.Synchronize() where source is a null IAsyncObservable<T>, typically when the source came from a lookup, event stream registry, or conditional initialization that returned null.

Common situations: Multithreaded apps wiring up synchronized pipelines where the source is assigned on another thread and not yet initialized; misconfigured dependency injection returning a null observable binding.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Synchronize.cs:14

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

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

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

        public static IAsyncObservable<TSource> Synchronize<TSource>(this IAsyncObservable<TSource> source, AsyncGate gate)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (gate == null)
                throw new ArgumentNullException(nameof(gate));

            return CreateAsyncObservable<TSource>.From(
                source,
                gate,
                static (source, gate, observer) => source.SubscribeSafeAsync(AsyncObserver.Synchronize(observer, gate)));
        }
    }

View on GitHub (pinned to 94b5d5ab91)