{"record":{"id":"d5d4f85ec499778a","repo":"dotnet/reactive","slug":"specified-argument-was-out-of-the-range-of-valid-values-d5d4f8","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'period')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'period'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Concurrency/TaskPoolScheduler.cs","lineNumber":288,"sourceCode":"            //\n            return new StopwatchImpl();\n        }\n\n        /// <summary>\n        /// Schedules a periodic piece of work by running a platform-specific timer to create tasks periodically.\n        /// </summary>\n        /// <typeparam name=\"TState\">The type of the state passed to the scheduled action.</typeparam>\n        /// <param name=\"state\">Initial state passed to the action upon the first iteration.</param>\n        /// <param name=\"period\">Period for running the work periodically.</param>\n        /// <param name=\"action\">Action to be executed, potentially updating the state.</param>\n        /// <returns>The disposable object used to cancel the scheduled recurring action (best effort).</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"action\"/> is <c>null</c>.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"period\"/> is less than <see cref=\"TimeSpan.Zero\"/>.</exception>\n        public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<TState, TState> action)\n        {\n            if (period < TimeSpan.Zero)\n            {\n                throw new ArgumentOutOfRangeException(nameof(period));\n            }\n\n            if (action == null)\n            {\n                throw new ArgumentNullException(nameof(action));\n            }\n\n            return new PeriodicallyScheduledWorkItem<TState>(state, period, action, _taskFactory);\n        }\n\n        private sealed class PeriodicallyScheduledWorkItem<TState> : IDisposable\n        {\n            private TState _state;\n\n            private readonly TimeSpan _period;\n            private readonly TaskFactory _taskFactory;\n            private readonly Func<TState, TState> _action;\n            private readonly AsyncLock _gate = new();","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Concurrency/TaskPoolScheduler.cs#L270-L306","documentation":"TaskPoolScheduler.SchedulePeriodic throws ArgumentOutOfRangeException when the period is less than TimeSpan.Zero (negative). Periodic scheduling requires a non-negative TimeSpan; the library normalizes zero/negative semantics but treats a strictly negative period as an invalid value. The check runs before the action null-check.","triggerScenarios":"Calling scheduler.SchedulePeriodic(state, TimeSpan.FromXxx(-n), action) with any negative duration, e.g. a computed interval that turned negative.","commonSituations":"Computing the period by subtraction (end - start where start > end), parsing durations from config with a sign error, or converting from another unit with a wrong scale.","solutions":["Pass a non-negative TimeSpan as the period.","Clamp the computed period before the call, e.g. period < TimeSpan.Zero ? TimeSpan.Zero : period.","Fix the source of the negative duration (reversed operands, bad parsing)."],"exampleFix":"// before\nvar period = end - start; // start > end => negative\nscheduler.SchedulePeriodic(state, period, next => next);\n// after\nvar period = end > start ? end - start : TimeSpan.FromSeconds(1);\nscheduler.SchedulePeriodic(state, period, next => next);","handlingStrategy":"validation","validationCode":"if (period < TimeSpan.Zero) throw new InvalidOperationException($\"period must be >= 0, was {period}\");\nscheduler.SchedulePeriodic(state, period, action);","typeGuard":null,"tryCatchPattern":"try { scheduler.SchedulePeriodic(state, period, action); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"period\") { log.LogError(\"Negative period {Period}\", period); }","preventionTips":["Use TimeSpan.FromXxx constructors rather than manual arithmetic for periods.","Clamp computed intervals with Math.Max(TimeSpan.Zero, period)."],"tags":["rx","scheduler","argument-out-of-range","timespan"],"backgroundTag":"argument-out-of-range","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}