{"record":{"id":"0bf6281745502e61","repo":"dotnet/reactive","slug":"new-argumentnullexception-nameof-source","errorCode":null,"errorMessage":"new ArgumentNullException(nameof(source))","messagePattern":"new ArgumentNullException\\(nameof\\(source\\)\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/GetAwaiter.cs","lineNumber":14,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information. \n\nusing System.Reactive.Subjects;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static AsyncAsyncSubject<TSource> GetAwaiter<TSource>(this IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            var subject = new SequentialAsyncAsyncSubject<TSource>();\n\n            var subscribeTask = source.SubscribeSafeAsync(subject);\n\n            subscribeTask.AsTask().ContinueWith(t =>\n            {\n                if (t.Exception != null)\n                {\n                    subject.OnErrorAsync(t.Exception); // NB: Should not occur due to use of SubscribeSafeAsync.\n                }\n            });\n\n            return subject;\n        }\n\n        public static AsyncAsyncSubject<TSource> GetAwaiter<TSource>(this IConnectableAsyncObservable<TSource> source)\n        {","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/GetAwaiter.cs#L1-L32","documentation":"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.","triggerScenarios":"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.","commonSituations":"Chaining operator results where an earlier call returned null unexpectedly; awaiting a field initialized lazily but accessed before initialization; dictionary lookups with TryGetValue ignored.","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."],"exampleFix":"// before\nvar result = await maybeSource; // maybeSource may be null\n// after\nif (maybeSource == null) throw new InvalidOperationException(\"source was not initialized\");\nvar result = await maybeSource;","handlingStrategy":"validation","validationCode":"if (source is null) throw new InvalidOperationException(\"observable source was not initialized\");","typeGuard":"static bool IsAwaitableSource<TSource>(IAsyncObservable<TSource>? s) => s is not null;","tryCatchPattern":"try { var value = await source; } catch (ArgumentNullException ex) when (ex.ParamName == \"source\") { /* handle missing source */ }","preventionTips":["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"],"tags":["argument-null","null-check","getawaiter","await-observable"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}