dotnet/reactive · error · ArgumentNullException

scheduler (Parameter 'scheduler')

Error message

scheduler (Parameter 'scheduler')

What it means

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.

Solutions

  1. Ensure a non-null IScheduler instance before awaiting Yield, e.g. use Scheduler.Default as fallback.
  2. Replace 'scheduler.Yield()' with 'var s = scheduler ?? Scheduler.Default; await s.Yield();'.
  3. Fix ambient scheduler resolution so it never yields null at await points.
  4. Check mocks/stubs in tests to return a real scheduler instance.

Example fix

// before
await scheduler.Yield(); // scheduler is null
// after
await (scheduler ?? Scheduler.Default).Yield();
Defensive patterns

Strategy: validation

Validate before calling

if (scheduler == null)
    throw new ArgumentNullException(nameof(scheduler));

Type guard

bool CanYield(IScheduler scheduler) => scheduler is not null;

Try / catch

try
{
    await scheduler.Yield();
}
catch (ArgumentNullException ex) when (ex.ParamName == "scheduler")
{
    await Scheduler.Default.Yield();
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/51c857bba516c510. Report an issue: GitHub.

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Concurrency/Scheduler.Async.cs:56

            public void Dispose()
            {
                _cts.Cancel();
                _run.Dispose();
            }
        }

        /// <summary>
        /// Yields execution of the current work item on the scheduler to another work item on the scheduler.
        /// The caller should await the result of calling Yield to schedule the remainder of the current work item (known as the continuation).
        /// </summary>
        /// <param name="scheduler">Scheduler to yield work on.</param>
        /// <returns>Scheduler operation object to await in order to schedule the continuation.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is <c>null</c>.</exception>
        public static SchedulerOperation Yield(this IScheduler scheduler)
        {
            if (scheduler == null)
            {
                throw new ArgumentNullException(nameof(scheduler));
            }

            return new SchedulerOperation(a => scheduler.Schedule(a), scheduler.GetCancellationToken());
        }

        /// <summary>
        /// Yields execution of the current work item on the scheduler to another work item on the scheduler.
        /// The caller should await the result of calling Yield to schedule the remainder of the current work item (known as the continuation).
        /// </summary>
        /// <param name="scheduler">Scheduler to yield work on.</param>
        /// <param name="cancellationToken">Cancellation token to cancel the continuation to run.</param>
        /// <returns>Scheduler operation object to await in order to schedule the continuation.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is <c>null</c>.</exception>
        public static SchedulerOperation Yield(this IScheduler scheduler, CancellationToken cancellationToken)
        {
            if (scheduler == null)
            {
                throw new ArgumentNullException(nameof(scheduler));

View on GitHub (pinned to 94b5d5ab91)