dotnet/reactive · error · ArgumentNullException

action (Value cannot be null)

Error message

action (Value cannot be null)

What it means

This ArgumentNullException is thrown by the Schedule(this IScheduler, Action<Action>) extension in Scheduler.Recursive.cs when the recursive action delegate is null. Rx's scheduler extensions validate arguments eagerly at the call site so a null delegate fails immediately rather than surfacing later inside the scheduler's queue. Only the action parameter is null here; the scheduler parameter was already checked.

Solutions

  1. Ensure the Action<Action> delegate passed to Schedule is non-null before calling.
  2. If the delegate comes from a variable or factory, initialize or provide a default implementation.
  3. Guard the call site: only invoke Schedule when the action was actually constructed.

Example fix

// before
Action<Action> act = GetAction(); // may return null
Scheduler.Schedule(scheduler, act);
// after
Action<Action> act = GetAction() ?? (_ => { });
Scheduler.Schedule(scheduler, act);
Defensive patterns

Strategy: validation

Validate before calling

if (action == null) throw new InvalidOperationException("Recursive action must be provided before scheduling");

Type guard

bool IsValidAction(Action<Action> a) => a is not null;

Try / catch

try { Scheduler.Schedule(scheduler, action); } catch (ArgumentNullException ex) when (ex.ParamName == "action") { /* log and supply default action */ }

Prevention

When it happens

Trigger: Calling Scheduler.Schedule(scheduler, (Action<Action>)null) or Schedule<TState> overloads with a null recursive action lambda, typically because a variable holding the delegate was null or a factory returned null.

Common situations: Storing the recursive action in a nullable field that was not yet initialized; conditional logic that assigns the delegate only on some paths; passing the result of a function that returns null as the action.

Related errors


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

Appendix: source

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

    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>
        /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
        public static IDisposable Schedule<TState>(this IScheduler scheduler, TState state, Action<TState, Action<TState>> action)
        {
            if (scheduler == null)
            {

View on GitHub (pinned to 94b5d5ab91)