dotnet/reactive · error · ArgumentNullException
new ArgumentNullException(nameof(source))
Error message
new ArgumentNullException(nameof(source))
What it means
The GetAwaiter extension on IAsyncObservable<TSource> lets you await an async observable as a single-value subject. It validates the source and throws ArgumentNullException(nameof(source)) when the observable is null, before creating the underlying SequentialAsyncAsyncSubject.
Solutions
- Ensure the source is produced by AsyncObservable operators, which never return null — trace upstream to find where null was introduced.
- Add an explicit null check or `?? throw new InvalidOperationException(...)` before awaiting.
- Use nullable-reference-type annotations so the compiler flags the possibly-null source.
Example fix
// before
var result = await maybeSource; // maybeSource may be null
// after
if (maybeSource == null) throw new InvalidOperationException("source was not initialized");
var result = await maybeSource; Defensive patterns
Strategy: validation
Validate before calling
if (source is null) throw new InvalidOperationException("observable source was not initialized"); Type guard
static bool IsAwaitableSource<TSource>(IAsyncObservable<TSource>? s) => s is not null;
Try / catch
try { var value = await source; } catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* handle missing source */ } Prevention
- Only assign sources from operator/factory results, which are never null; investigate any null return upstream
- Initialize observable fields eagerly or use lazy initialization
- Enable nullable reference types to catch possibly-null sources at compile time
When it happens
Trigger: Writing `await someSource` (or calling source.GetAwaiter()) where someSource is null — e.g. a lookup/cache miss returned null, a factory method returned null, or an optional dependency was never assigned.
Common situations: Chaining operator results where an earlier call returned null unexpectedly; awaiting a field initialized lazily but accessed before initialization; dictionary lookups with TryGetValue ignored.
Related errors
- new ArgumentNullException(nameof(source))
- new ArgumentNullException(nameof(keySelector))
- observer
- Value cannot be null. (Parameter 'subscribeAsync')
- Value cannot be null. (Parameter 'observer')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/0bf6281745502e61.
Report an issue: GitHub.
Appendix: source
Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/GetAwaiter.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.Reactive.Subjects;
namespace System.Reactive.Linq
{
public partial class AsyncObservable
{
public static AsyncAsyncSubject<TSource> GetAwaiter<TSource>(this IAsyncObservable<TSource> source)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
var subject = new SequentialAsyncAsyncSubject<TSource>();
var subscribeTask = source.SubscribeSafeAsync(subject);
subscribeTask.AsTask().ContinueWith(t =>
{
if (t.Exception != null)
{
subject.OnErrorAsync(t.Exception); // NB: Should not occur due to use of SubscribeSafeAsync.
}
});
return subject;
}
public static AsyncAsyncSubject<TSource> GetAwaiter<TSource>(this IConnectableAsyncObservable<TSource> source)
{View on GitHub (pinned to 94b5d5ab91)