dotnet/reactive · error · ArgumentNullException

action (Parameter 'action')

Error message

action (Parameter 'action')

What it means

HistoricalScheduler.ScheduleAbsolute<TState>(state, dueTime, action) throws ArgumentNullException when action is null. The historical (virtual-time) scheduler wraps the action in a rescheduling closure before enqueuing it at the absolute due time, so it validates the delegate up front.

Solutions

  1. Ensure the action delegate is non-null before calling ScheduleAbsolute.
  2. In test harnesses, assert/mock setup produces a real delegate for every event kind.
  3. Skip the schedule call when there is no work to run.

Example fix

// before
scheduler.ScheduleAbsolute(0, dueTime, _action); // _action may be null

// after
if (_action != null)
    scheduler.ScheduleAbsolute(0, dueTime, _action);
Defensive patterns

Strategy: validation

Validate before calling

if (action is null) throw new ArgumentNullException(nameof(action));
historicalScheduler.ScheduleAbsolute(state, dueTime, action);

Type guard

static bool NonNull<TState>(Func<IScheduler, TState, IDisposable>? a) => a is not null;

Try / catch

try { historicalScheduler.ScheduleAbsolute(state, dueTime, action); }
catch (ArgumentNullException ex) when (ex.ParamName == "action")
{
    // fix test harness delegate mapping
}

Prevention

When it happens

Trigger: Calling ScheduleAbsolute with a null Func<IScheduler, TState, IDisposable> — commonly in virtual-time test harnesses (TestScheduler-style code) where the action variable was conditionally assigned or a mock returned null.

Common situations: Rx test suites advancing virtual time with dynamically built actions; simulators that map events to scheduled work and produce a null callback for unmapped event types.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Concurrency/HistoricalScheduler.cs:142

            }

            return null;
        }

        /// <summary>
        /// Schedules an action to be executed at <paramref name="dueTime"/>.
        /// </summary>
        /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
        /// <param name="state">State passed to the action to be executed.</param>
        /// <param name="action">Action to be executed.</param>
        /// <param name="dueTime">Absolute time at which to execute the action.</param>
        /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
        /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
        public override IDisposable ScheduleAbsolute<TState>(TState state, DateTimeOffset dueTime, Func<IScheduler, TState, IDisposable> action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            ScheduledItem<DateTimeOffset, TState>? si = null;

            var run = new Func<IScheduler, TState, IDisposable>((scheduler, state1) =>
            {
                _queue.Remove(si!); // NB: Assigned before function is invoked.
                return action(scheduler, state1);
            });

            si = new ScheduledItem<DateTimeOffset, TState>(this, state, run, dueTime, Comparer);
            _queue.Enqueue(si);

            return si;
        }
    }
}

View on GitHub (pinned to 94b5d5ab91)