dotnet/reactive · error · ArgumentNullException

scheduler (Value cannot be null)

Error message

scheduler (Value cannot be null)

What it means

System.Reactive's recursive Schedule extension (Scheduler.Recursive.cs) requires a non-null IScheduler receiver and throws ArgumentNullException (Parameter 'scheduler') when it is null. The guard runs before the recursive-scheduling wrapper is constructed so invalid calls fail immediately.

Solutions

  1. Invoke Schedule on a valid IScheduler instance (Scheduler.Default, TaskPoolScheduler.Default, TestScheduler).
  2. Add `if (scheduler == null) scheduler = Scheduler.Default;` or use the null-coalescing operator before the call.
  3. Repair whatever supplies the scheduler (DI registration, factory, property) so it is never null.

Example fix

// before
IScheduler scheduler = _options.Scheduler; // may be null
scheduler.Schedule(self => DoWork(self)); // throws
// after
var scheduler = _options.Scheduler ?? Scheduler.Default;
scheduler.Schedule(self => DoWork(self));
Defensive patterns

Strategy: validation

Validate before calling

if (scheduler == null) scheduler = Scheduler.Default;

Type guard

bool HasScheduler(IScheduler s) => s is not null;

Try / catch

try { disposable = scheduler.Schedule(self => next => { /* recursive work */ }); }
catch (ArgumentNullException ex) when (ex.ParamName == "scheduler") { disposable = Scheduler.Default.Schedule(self => next => { /* recursive work */ }); }

Prevention

When it happens

Trigger: Calling scheduler.Schedule(Action<Action> action) on a null IScheduler receiver — permitted by the compiler because it is an extension method, e.g. `IScheduler s = null; s.Schedule(self => { ... });`.

Common situations: A scheduler dependency from DI/config is null; a scheduler returned by a provider method is null; refactoring removed the scheduler assignment while keeping the extension call.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Concurrency/Scheduler.Recursive.cs:22

using System.Reactive.Disposables;

namespace System.Reactive.Concurrency
{
    public static partial class Scheduler
    {
        /// <summary>
        /// Schedules an action to be executed recursively.
        /// </summary>
        /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
        /// <param name="action">Action to execute recursively. The parameter passed to the action is used to trigger recursive scheduling of the action.</param>
        /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
        /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
        public static IDisposable Schedule(this IScheduler scheduler, Action<Action> action)
        {
            if (scheduler == null)
            {
                throw new ArgumentNullException(nameof(scheduler));
            }

            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            return scheduler.Schedule(action, static (a, self) => a(() => self(a)));
        }

        /// <summary>
        /// Schedules an action to be executed recursively.
        /// </summary>
        /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
        /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
        /// <param name="state">State passed to the action to be executed.</param>
        /// <param name="action">Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in recursive invocation state.</param>
        /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>

View on GitHub (pinned to 94b5d5ab91)