{"record":{"id":"4b56e25617039927","repo":"dotnet/reactive","slug":"specified-argument-was-out-of-the-range-of-valid-values-4b56e2","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'timeSpan')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'timeSpan'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Window.cs","lineNumber":295,"sourceCode":"                            }\n\n                            await observer.OnCompletedAsync().ConfigureAwait(false);\n                        }\n                    ),\n                    refCount\n                );\n        }\n\n        public static ValueTask<(IAsyncObserver<TSource>, IAsyncDisposable)> Window<TSource>(IAsyncObserver<IAsyncObservable<TSource>> observer, IAsyncDisposable subscription, TimeSpan timeSpan) => Window(observer, subscription, timeSpan, TaskPoolAsyncScheduler.Default);\n\n        public static ValueTask<(IAsyncObserver<TSource>, IAsyncDisposable)> Window<TSource>(IAsyncObserver<IAsyncObservable<TSource>> observer, IAsyncDisposable subscription, TimeSpan timeSpan, 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 (scheduler == null)\n                throw new ArgumentNullException(nameof(scheduler));\n\n            var gate = new AsyncGate();\n\n            var window = default(IAsyncSubject<TSource>);\n            var d = new CompositeAsyncDisposable();\n            var refCount = new RefCountAsyncDisposable(d);\n\n            async Task CreateWindowAsync()\n            {\n                window = new SequentialSimpleAsyncSubject<TSource>();\n\n                var wrapper = new WindowAsyncObservable<TSource>(window, refCount);\n\n                await observer.OnNextAsync(wrapper).ConfigureAwait(false);\n            }\n","sourceCodeStart":277,"sourceCodeEnd":313,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Window.cs#L277-L313","documentation":"This ArgumentOutOfRangeException is thrown by the time-based Window helper when timeSpan is negative (less than TimeSpan.Zero). Zero is allowed (windows close immediately per tick semantics), but a negative duration has no meaning for a timer-based window. The check runs synchronously before any scheduling occurs.","triggerScenarios":"Calling Window(source, timeSpan) or the internal helper with a negative TimeSpan, e.g. a value read from config in milliseconds that was negative, or subtracting TimeSpans in the wrong order.","commonSituations":"Duration configuration parsed as a signed number; clock arithmetic (end - start with end < start); serialization of durations that lost sign constraints.","solutions":["Pass a non-negative TimeSpan such as TimeSpan.FromSeconds(5).","Clamp: if (ts < TimeSpan.Zero) ts = TimeSpan.Zero; or throw your own validation error earlier.","Fix the config/parser so durations are validated as non-negative at load time.","Check any date/duration arithmetic producing the TimeSpan for reversed operands."],"exampleFix":"// before\nvar ts = end - start; // end earlier than start => negative\nvar windows = source.Window(ts);\n// after\nvar ts = end > start ? end - start : TimeSpan.Zero;\nvar windows = source.Window(ts);","handlingStrategy":"validation","validationCode":"if (timeSpan < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(timeSpan), timeSpan, \"Window duration must be non-negative.\");\n// or clamp: timeSpan = timeSpan < TimeSpan.Zero ? TimeSpan.Zero : timeSpan;","typeGuard":"bool IsValidDuration(TimeSpan ts) => ts >= TimeSpan.Zero;","tryCatchPattern":"try\n{\n    var windows = source.Window(timeSpan);\n}\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"timeSpan\")\n{\n    timeSpan = DefaultWindowDuration; // e.g. TimeSpan.FromSeconds(1)\n}","preventionTips":["Validate durations from configuration as non-negative at parse time.","Check for reversed operands in end-start TimeSpan arithmetic.","Represent durations with unsigned/validated types at your config boundary."],"tags":["argument-out-of-range","argument-validation","async-rx","window-operator","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"}