{"record":{"id":"3886fcc8e7178323","repo":"dotnet/reactive","slug":"specified-argument-was-out-of-the-range-of-valid-values-3886fc","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'timeShift')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'timeShift'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Window.cs","lineNumber":381,"sourceCode":"                        refCount\n                    );\n            }\n\n            return CoreAsync();\n        }\n\n        public static ValueTask<(IAsyncObserver<TSource>, IAsyncDisposable)> Window<TSource>(IAsyncObserver<IAsyncObservable<TSource>> observer, IAsyncDisposable subscription, TimeSpan timeSpan, TimeSpan timeShift) => Window(observer, subscription, timeSpan, timeShift, TaskPoolAsyncScheduler.Default);\n\n        public static ValueTask<(IAsyncObserver<TSource>, IAsyncDisposable)> Window<TSource>(IAsyncObserver<IAsyncObservable<TSource>> observer, IAsyncDisposable subscription, TimeSpan timeSpan, TimeSpan timeShift, IAsyncScheduler scheduler)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (subscription == null)\n                throw new ArgumentNullException(nameof(subscription));\n            if (timeSpan < TimeSpan.Zero)\n                throw new ArgumentOutOfRangeException(nameof(timeSpan));\n            if (timeShift < TimeSpan.Zero)\n                throw new ArgumentOutOfRangeException(nameof(timeShift));\n            if (scheduler == null)\n                throw new ArgumentNullException(nameof(scheduler));\n\n            var gate = new AsyncGate();\n\n            var d = new CompositeAsyncDisposable();\n            var timer = new SerialAsyncDisposable();\n            var refCount = new RefCountAsyncDisposable(d);\n\n            var queue = new Queue<IAsyncSubject<TSource>>();\n\n            var nextOpen = timeShift;\n            var nextClose = timeSpan;\n            var totalTime = TimeSpan.Zero;\n\n            var isOpen = false;\n            var isClose = false;\n","sourceCodeStart":363,"sourceCodeEnd":399,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Window.cs#L363-L399","documentation":"In the sliding time Window, timeShift is the interval between window starts and must be non-negative. The operator throws ArgumentOutOfRangeException when it is negative because window start times would regress in time, an unschedulable configuration.","triggerScenarios":"Passing a negative TimeSpan as the timeShift argument (4th parameter) of Window(observer, subscription, timeSpan, timeShift[, scheduler]) — e.g. a computed shift that went negative or swapped the timeShift/timeSpan arguments.","commonSituations":"Swapping timeSpan and timeShift where one is negative; deriving the shift from a percentage or delta that can go below zero; parsing durations from settings where negatives are not screened.","solutions":["Clamp or recompute the shift to be >= TimeSpan.Zero before calling Window","Check argument order — ensure timeSpan (length) is passed before timeShift (period)","Fix the producer of the negative duration (config parsing, timestamp subtraction)"],"exampleFix":"// before\nawait AsyncObservable.Window(observer, subscription, shift, windowLen, scheduler); // args swapped\n// after\nawait AsyncObservable.Window(observer, subscription, windowLen, shift < TimeSpan.Zero ? TimeSpan.Zero : shift, scheduler);","handlingStrategy":"validation","validationCode":"if (timeShift < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(timeShift));\nvar safeShift = TimeSpan.Max(TimeSpan.Zero, timeShift);","typeGuard":"static bool IsValidShift(TimeSpan t) => t >= TimeSpan.Zero;","tryCatchPattern":"try\n{\n    await AsyncObservable.Window(observer, subscription, timeSpan, timeShift, scheduler);\n}\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"timeShift\")\n{\n    // fix argument order or clamp the shift\n}","preventionTips":["Double-check timeSpan vs timeShift argument order (length before period)","Clamp percentages/deltas used to derive shift values","Unit-test window composition with boundary durations (0, 1 tick)"],"tags":["csharp","async-rx","argument-validation","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"}