{"record":{"id":"51c857bba516c510","repo":"dotnet/reactive","slug":"scheduler-parameter-scheduler-async","errorCode":null,"errorMessage":"scheduler (Parameter 'scheduler')","messagePattern":"scheduler \\(Parameter 'scheduler'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Concurrency/Scheduler.Async.cs","lineNumber":56,"sourceCode":"            public void Dispose()\n            {\n                _cts.Cancel();\n                _run.Dispose();\n            }\n        }\n\n        /// <summary>\n        /// Yields execution of the current work item on the scheduler to another work item on the scheduler.\n        /// The caller should await the result of calling Yield to schedule the remainder of the current work item (known as the continuation).\n        /// </summary>\n        /// <param name=\"scheduler\">Scheduler to yield work on.</param>\n        /// <returns>Scheduler operation object to await in order to schedule the continuation.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"scheduler\"/> is <c>null</c>.</exception>\n        public static SchedulerOperation Yield(this IScheduler scheduler)\n        {\n            if (scheduler == null)\n            {\n                throw new ArgumentNullException(nameof(scheduler));\n            }\n\n            return new SchedulerOperation(a => scheduler.Schedule(a), scheduler.GetCancellationToken());\n        }\n\n        /// <summary>\n        /// Yields execution of the current work item on the scheduler to another work item on the scheduler.\n        /// The caller should await the result of calling Yield to schedule the remainder of the current work item (known as the continuation).\n        /// </summary>\n        /// <param name=\"scheduler\">Scheduler to yield work on.</param>\n        /// <param name=\"cancellationToken\">Cancellation token to cancel the continuation to run.</param>\n        /// <returns>Scheduler operation object to await in order to schedule the continuation.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"scheduler\"/> is <c>null</c>.</exception>\n        public static SchedulerOperation Yield(this IScheduler scheduler, CancellationToken cancellationToken)\n        {\n            if (scheduler == null)\n            {\n                throw new ArgumentNullException(nameof(scheduler));","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Concurrency/Scheduler.Async.cs#L38-L74","documentation":"The async/await integration extension Scheduler.Async.Yield(this IScheduler scheduler) throws ArgumentNullException when the scheduler extension receiver is null. Yield creates a SchedulerOperation that posts the continuation to the given scheduler, so the receiver must be a real IScheduler instance.","triggerScenarios":"Calling 'scheduler.Yield()' where the scheduler expression evaluates to null (e.g. a null property, an extension invocation with a null IScheduler variable, or Scheduler.CurrentThread-like lookups that return null).","commonSituations":"Async Rx workflows where the ambient scheduler comes from configuration or AsyncLocal storage and is unset; refactoring 'await Scheduler.X.Yield()' into a variable that is conditionally assigned; mocking frameworks returning null schedulers.","solutions":["Ensure a non-null IScheduler instance before awaiting Yield, e.g. use Scheduler.Default as fallback.","Replace 'scheduler.Yield()' with 'var s = scheduler ?? Scheduler.Default; await s.Yield();'.","Fix ambient scheduler resolution so it never yields null at await points.","Check mocks/stubs in tests to return a real scheduler instance."],"exampleFix":"// before\nawait scheduler.Yield(); // scheduler is null\n// after\nawait (scheduler ?? Scheduler.Default).Yield();","handlingStrategy":"validation","validationCode":"if (scheduler == null)\n    throw new ArgumentNullException(nameof(scheduler));","typeGuard":"bool CanYield(IScheduler scheduler) => scheduler is not null;","tryCatchPattern":"try\n{\n    await scheduler.Yield();\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"scheduler\")\n{\n    await Scheduler.Default.Yield();\n}","preventionTips":["Coalesce ambient schedulers at the call site: '(scheduler ?? Scheduler.Default).Yield()'.","Initialize ambient scheduler state (AsyncLocal) before the first await.","In tests, make mocks return a concrete scheduler instead of null."],"tags":["rx","scheduler","async-await","argument-null","csharp"],"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"}