dotnet/reactive · error · ArgumentNullException

action (Value cannot be null)

Error message

action (Value cannot be null)

What it means

System.Reactive's ScheduleAsync validates the action delegate before scheduling. Supplying a null Func<IScheduler, TState, CancellationToken, Task<IDisposable>> causes an ArgumentNullException with message "Value cannot be null (Parameter 'action')". The guard guarantees the scheduled invocation always has a callable delegate.

Solutions

  1. Pass a real delegate returning Task<IDisposable>, e.g. async (s, st, ct) => { ...; return Disposable.Empty; }.
  2. Check action for null before calling ScheduleAsync.
  3. Fix whatever produces the delegate so it cannot return null.

Example fix

// before
scheduler.ScheduleAsync(state, dueTime, null); // throws
// after
scheduler.ScheduleAsync(state, dueTime, async (s, st, ct) =>
{
    await DoWorkAsync(st, ct);
    return Disposable.Empty;
});
Defensive patterns

Strategy: validation

Validate before calling

if (action == null) throw new InvalidOperationException("ScheduleAsync requires a non-null cancelable action");

Type guard

bool IsValidAction<TState>(Func<IScheduler, TState, CancellationToken, Task<IDisposable>> a) => a is not null;

Try / catch

try { disposable = scheduler.ScheduleAsync(state, dueTime, action); }
catch (ArgumentNullException ex) when (ex.ParamName == "action") { disposable = Disposable.Empty; /* or reschedule with a valid delegate */ }

Prevention

When it happens

Trigger: Calling scheduler.ScheduleAsync<TState>(state, DateTimeOffset dueTime, Func<IScheduler, TState, CancellationToken, Task<IDisposable>> action) with action == null, e.g. scheduler.ScheduleAsync(state, DateTimeOffset.Now, (Func<IScheduler, int, CancellationToken, Task<IDisposable>>)null).

Common situations: The cancelable-work delegate (returning Task<IDisposable>) is built dynamically or from configuration and comes out null; overload-resolution confusion leads to passing null; refactoring removed the callback.

Related errors


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

Appendix: source

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

        /// Schedules work using an asynchronous method, allowing for cooperative scheduling in an imperative coding style.
        /// </summary>
        /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
        /// <param name="scheduler">Scheduler to schedule work on.</param>
        /// <param name="state">State to pass to the asynchronous method.</param>
        /// <param name="dueTime">Absolute time at which to execute the action.</param>
        /// <param name="action">Asynchronous method to run the work, using Yield and Sleep operations for cooperative scheduling and injection of cancellation points.</param>
        /// <returns>Disposable object that allows to cancel outstanding work on cooperative cancellation points or through the cancellation token passed to the asynchronous method.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
        public static IDisposable ScheduleAsync<TState>(this IScheduler scheduler, TState state, DateTimeOffset dueTime, Func<IScheduler, TState, CancellationToken, Task<IDisposable>> action)
        {
            if (scheduler == null)
            {
                throw new ArgumentNullException(nameof(scheduler));
            }

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

            return ScheduleAsync_(scheduler, state, dueTime, action);
        }

        /// <summary>
        /// Schedules work using an asynchronous method, allowing for cooperative scheduling in an imperative coding style.
        /// </summary>
        /// <param name="scheduler">Scheduler to schedule work on.</param>
        /// <param name="dueTime">Absolute time at which to execute the action.</param>
        /// <param name="action">Asynchronous method to run the work, using Yield and Sleep operations for cooperative scheduling and injection of cancellation points.</param>
        /// <returns>Disposable object that allows to cancel outstanding work on cooperative cancellation points or through the cancellation token passed to the asynchronous method.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
        public static IDisposable ScheduleAsync(this IScheduler scheduler, DateTimeOffset dueTime, Func<IScheduler, CancellationToken, Task> action)
        {
            if (scheduler == null)
            {
                throw new ArgumentNullException(nameof(scheduler));

View on GitHub (pinned to 94b5d5ab91)