dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'gate')

Error message

Value cannot be null. (Parameter 'gate')

What it means

Synchronize(source, gate) requires a non-null AsyncGate to serialize observer callbacks. The operator validates its arguments eagerly and throws ArgumentNullException before returning the observable. Passing a null gate means there is no lock object to coordinate concurrent OnNext/OnError/OnCompleted calls, so the library refuses to build the operator.

Solutions

  1. Pass a constructed gate: new AsyncGate()
  2. If you don't already have a gate, call Synchronize(source) without the gate parameter, which creates one internally
  3. Ensure the field/property holding the gate is initialized before the operator is built

Example fix

// before
var gate = GetGate(); // may return null
var synced = source.Synchronize(gate);
// after
var gate = GetGate() ?? new AsyncGate();
var synced = source.Synchronize(gate);
Defensive patterns

Strategy: validation

Validate before calling

if (gate is null) throw new InvalidOperationException("Gate must be initialized before Synchronize");
var synced = source.Synchronize(gate);

Type guard

bool HasGate(AsyncGate? gate) => gate is not null;

Try / catch

try { var synced = source.Synchronize(gate); }
catch (ArgumentNullException ex) when (ex.ParamName == "gate") { /* supply default gate */ }

Prevention

When it happens

Trigger: Calling AsyncObservable.Synchronize(source, null) — e.g. a gate variable that was never initialized or was set to null after conditional cleanup.

Common situations: Developers caching an AsyncGate in a field that is null before initialization, or refactoring from the gate-less Synchronize(source) overload and passing null by mistake.

Related errors


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

Appendix: source

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

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)));
        }
    }

    public partial class AsyncObserver
    {
        public static IAsyncObserver<TSource> Synchronize<TSource>(IAsyncObserver<TSource> observer)
        {
            if (observer == null)
                throw new ArgumentNullException(nameof(observer));

            return Synchronize(observer, new AsyncGate());
        }

View on GitHub (pinned to 94b5d5ab91)