dotnet/reactive · error · ArgumentOutOfRangeException

Specified argument was out of the range of valid values…

Error message

Specified argument was out of the range of valid values. (Parameter 'period')

What it means

EventLoopScheduler.SchedulePeriodic<TState>(state, period, action) throws ArgumentOutOfRangeException(nameof(period)) when period is negative (less than TimeSpan.Zero). A negative periodic interval is meaningless, so the scheduler rejects it before creating the PeriodicallyScheduledWorkItem.

Solutions

  1. Clamp the period: if (period < TimeSpan.Zero) period = TimeSpan.Zero; before calling.
  2. Validate configuration values at startup and fail with a clear message.
  3. Use TimeSpan.Zero or a positive value; zero means run on every scheduler tick.

Example fix

// before
var period = TimeSpan.FromMilliseconds(config.PeriodMs); // may be negative
scheduler.SchedulePeriodic(state, period, Tick);

// after
var period = TimeSpan.FromMilliseconds(Math.Max(0, config.PeriodMs));
scheduler.SchedulePeriodic(state, period, Tick);
Defensive patterns

Strategy: validation

Validate before calling

if (period < TimeSpan.Zero)
    throw new ArgumentException("period must be >= TimeSpan.Zero", nameof(period));
scheduler.SchedulePeriodic(state, period, action);

Type guard

static bool IsValidPeriod(TimeSpan p) => p >= TimeSpan.Zero;

Try / catch

try { scheduler.SchedulePeriodic(state, period, action); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "period")
{
    // correct config value
}

Prevention

When it happens

Trigger: Calling SchedulePeriodic with TimeSpan.FromMilliseconds(-1) or a negative TimeSpan computed from configuration (e.g. subtracting durations, parsing '-5' from settings).

Common situations: Config files or app settings where a period value was entered/negated incorrectly; date math producing a negative interval; Timeout semantics (-1 = infinite) mistakenly reused as a period.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Concurrency/EventLoopScheduler.cs:186

            return si;
        }

        /// <summary>
        /// Schedules a periodic piece of work on the designated thread.
        /// </summary>
        /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
        /// <param name="state">Initial state passed to the action upon the first iteration.</param>
        /// <param name="period">Period for running the work periodically.</param>
        /// <param name="action">Action to be executed, potentially updating the state.</param>
        /// <returns>The disposable object used to cancel the scheduled recurring action (best effort).</returns>
        /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="period"/> is less than <see cref="TimeSpan.Zero"/>.</exception>
        /// <exception cref="ObjectDisposedException">The scheduler has been disposed and doesn't accept new work.</exception>
        public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<TState, TState> action)
        {
            if (period < TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(period));
            }

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

            return new PeriodicallyScheduledWorkItem<TState>(this, state, period, action);
        }

        private sealed class PeriodicallyScheduledWorkItem<TState> : IDisposable
        {
            private readonly TimeSpan _period;
            private readonly Func<TState, TState> _action;
            private readonly EventLoopScheduler _scheduler;
            private readonly AsyncLock _gate = new();

            private TState _state;

View on GitHub (pinned to 94b5d5ab91)