{"record":{"id":"0ce7064480063eb9","repo":"dotnet/reactive","slug":"source-takelast","errorCode":null,"errorMessage":"source","messagePattern":"source","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeLast.cs","lineNumber":42,"sourceCode":"            }\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                count,\n                static async (source, count, observer) =>\n                {\n                    var (sink, drain) = AsyncObserver.TakeLast(observer, count);\n\n                    var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);\n\n                    return StableCompositeAsyncDisposable.Create(subscription, drain);\n                });\n        }\n\n        public static IAsyncObservable<TSource> TakeLast<TSource>(this IAsyncObservable<TSource> source, int count, IAsyncScheduler scheduler)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (count < 0)\n                throw new ArgumentOutOfRangeException(nameof(count));\n            if (scheduler == null)\n                throw new ArgumentNullException(nameof(scheduler));\n\n            if (count == 0)\n            {\n                return Empty<TSource>();\n            }\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                (count, scheduler),\n                static async (source, state, observer) =>\n                {\n                    var (sink, drain) = AsyncObserver.TakeLast(observer, state.count, state.scheduler);\n\n                    var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeLast.cs#L24-L60","documentation":"The scheduled overload TakeLast(source, count, scheduler) validates all three parameters and throws ArgumentNullException (with param name 'source') at TakeLast.cs:42 when the source observable is null.","triggerScenarios":"Calling TakeLast(null, count, scheduler) or source.TakeLast(count, scheduler) where source is null; passing a scheduler overload down a chain whose upstream source was never created.","commonSituations":"Custom operator plumbing where a null source flows through extension methods; DI/config supplying no observable; refactors that removed the source assignment.","solutions":["Create or obtain a valid IAsyncObservable<TSource> before calling TakeLast.","Substitute AsyncObservable.Empty<TSource>() when the source is legitimately absent.","Fix the upstream null-producing code path.","Assert non-null source at the pipeline boundary."],"exampleFix":"// before: AsyncObservable.TakeLast(source, 5, TaskPoolAsyncScheduler.Default); // source null  // after: var last = (source ?? AsyncObservable.Empty<int>()).TakeLast(5, TaskPoolAsyncScheduler.Default);","handlingStrategy":"validation","validationCode":"if (source == null) throw new ArgumentNullException(nameof(source)); if (scheduler == null) scheduler = TaskPoolAsyncScheduler.Default; var last = AsyncObservable.TakeLast(source, count, scheduler);","typeGuard":"static bool IsValidTakeLast<TSource>(IAsyncObservable<TSource> s, int c, IAsyncScheduler sch) => s != null && c >= 0 && sch != null;","tryCatchPattern":"try { var last = AsyncObservable.TakeLast(source, count, scheduler); } catch (ArgumentNullException ex) when (ex.ParamName == \"source\") { var last = AsyncObservable.Empty<TSource>(); }","preventionTips":["Validate the full parameter triple (source, count, scheduler) before calling.","Default source fields to Empty<T>() instead of null.","Fix upstream factories that return null observables.","Log the null-source condition rather than silently swallowing it."],"tags":["csharp","null-argument","observable","scheduler"],"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"}