dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'onCompletedAsync')

Error message

Value cannot be null. (Parameter 'onCompletedAsync')

What it means

ArgumentNullException from CreateUnsafe for onCompletedAsync being null; the message exposes 'Value cannot be null. (Parameter onCompletedAsync)'. The completion handler is mandatory in the Unsafe overload, which forwards delegates straight to UnsafeAsyncObserver<T>. The throw happens synchronously, before any observer callbacks can run.

Solutions

  1. Pass a non-null onCompletedAsync, e.g. () => default for no-op completion.
  2. Validate all three delegates before calling CreateUnsafe.
  3. Use the safe Create(onNextAsync) overload when custom completion/error handling is unnecessary.

Example fix

// before
AsyncObserver.CreateUnsafe<int>(onNext, onError, null);
// after
AsyncObserver.CreateUnsafe<int>(onNext, onError, () => default);
Defensive patterns

Strategy: validation

Validate before calling

if (onCompletedAsync is null) onCompletedAsync = () => default;
var observer = AsyncObserver.CreateUnsafe(onNextAsync, onErrorAsync, onCompletedAsync);

Try / catch

try
{
    var observer = AsyncObserver.CreateUnsafe(onNextAsync, onErrorAsync, onCompletedAsync);
}
catch (ArgumentNullException ex) when (ex.ParamName == "onCompletedAsync")
{
    var observer = AsyncObserver.CreateUnsafe(onNextAsync, onErrorAsync, () => default);
}

Prevention

When it happens

Trigger: Calling CreateUnsafe<T>(onNext, onError, null) with a null completion delegate, e.g. a () => default handler stored in a field that was never initialized.

Common situations: Internal operators building observers from nullable handler fields; hand-written operators or tests omitting the completion handler; refactoring that dropped the () => default argument.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/AsyncObserver.cs:42

        {
            if (onNextAsync == null)
                throw new ArgumentNullException(nameof(onNextAsync));
            if (onErrorAsync == null)
                throw new ArgumentNullException(nameof(onErrorAsync));
            if (onCompletedAsync == null)
                throw new ArgumentNullException(nameof(onCompletedAsync));

            return new AsyncObserver<T>(onNextAsync, onErrorAsync, onCompletedAsync);
        }

        internal static IAsyncObserver<T> CreateUnsafe<T>(Func<T, ValueTask> onNextAsync, Func<Exception, ValueTask> onErrorAsync, Func<ValueTask> onCompletedAsync)
        {
            if (onNextAsync == null)
                throw new ArgumentNullException(nameof(onNextAsync));
            if (onErrorAsync == null)
                throw new ArgumentNullException(nameof(onErrorAsync));
            if (onCompletedAsync == null)
                throw new ArgumentNullException(nameof(onCompletedAsync));

            return new UnsafeAsyncObserver<T>(onNextAsync, onErrorAsync, onCompletedAsync);
        }
    }
}

View on GitHub (pinned to 94b5d5ab91)