{"record":{"id":"46bc91f8b006abb6","repo":"dotnet/reactive","slug":"scheduler-takelast","errorCode":null,"errorMessage":"scheduler","messagePattern":"scheduler","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeLast.cs","lineNumber":46,"sourceCode":"                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);\n\n                    return StableCompositeAsyncDisposable.Create(subscription, drain);\n                });\n        }","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeLast.cs#L28-L64","documentation":"TakeLast(source, count, scheduler) throws ArgumentNullException(\"scheduler\") because the overload requires an explicit IAsyncScheduler to schedule the timing window. The Rx.NET Async operators validate all reference arguments eagerly at call time rather than surfacing nulls later inside the subscription pipeline. Passing a null scheduler is a programming error, so it fails fast with the parameter name.","triggerScenarios":"Calling TakeLast with a non-null source and non-negative count but scheduler == null, e.g. TakeLast(source, 5, (IAsyncScheduler)null).","commonSituations":"Refactoring from the TakeLast(source, count) overload and adding a scheduler argument that is still null; a scheduler variable resolved from DI/config that returned null; conditional scheduler selection leaving the variable unset.","solutions":["Pass a concrete IAsyncScheduler instance, e.g. the library's default/scheduler provider for your concurrency model","Use the TakeLast(source, count) overload instead if no scheduler customization is needed","Guard or assert the scheduler variable before calling, ensuring DI resolution produced a non-null instance"],"exampleFix":"// before\nvar result = source.TakeLast(5, (IAsyncScheduler)null);\n// after\nvar result = source.TakeLast(5, scheduler); // scheduler resolved from provider/DI\n// or: var result = source.TakeLast(5);","handlingStrategy":"validation","validationCode":"if (scheduler is null) throw new InvalidOperationException(\"scheduler must be provided before calling TakeLast\");\nvar result = source.TakeLast(5, scheduler);","typeGuard":"bool HasScheduler(IAsyncScheduler? s) => s is not null;","tryCatchPattern":"try { var r = source.TakeLast(5, scheduler); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"scheduler\") { /* fall back to parameterless overload */ var r = source.TakeLast(5); }","preventionTips":["Prefer the shorter TakeLast overloads unless you truly need scheduler control","Resolve schedulers from a single non-null provider","Add unit tests that construct operator pipelines through DI to catch null services"],"tags":["argument-null","rx","takelast","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"}