{"record":{"id":"025d7f2339bbbc19","repo":"dotnet/reactive","slug":"specified-argument-was-out-of-range-of-valid-values-take","errorCode":null,"errorMessage":"Specified argument was out of range of valid values. (Parameter 'duration')","messagePattern":"Specified argument was out of range of valid values\\. \\(Parameter 'duration'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Take.cs","lineNumber":37,"sourceCode":"                throw new ArgumentOutOfRangeException(nameof(count));\n\n            if (count == 0)\n            {\n                return Empty<TSource>();\n            }\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                count,\n                static (source, count, observer) => source.SubscribeSafeAsync(AsyncObserver.Take(observer, count)));\n        }\n\n        public static IAsyncObservable<TSource> Take<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (duration < TimeSpan.Zero)\n                throw new ArgumentOutOfRangeException(nameof(duration));\n\n            if (duration == TimeSpan.Zero)\n            {\n                return Empty<TSource>();\n            }\n\n            // REVIEW: May be easier to just use TakeUntil with a Timer parameter. Do we want Take on the observer?\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                duration,\n                static async (source, duration, observer) =>\n                {\n                    var (sourceObserver, timer) = await AsyncObserver.Take(observer, duration).ConfigureAwait(false);\n\n                    var subscription = await source.SubscribeSafeAsync(sourceObserver).ConfigureAwait(false);\n\n                    return StableCompositeAsyncDisposable.Create(subscription, timer);","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Take.cs#L19-L55","documentation":"Take(source, duration) requires a non-negative TimeSpan; a negative duration is invalid (it would mean taking elements from before the start) and throws ArgumentOutOfRangeException. TimeSpan.Zero is valid and yields an empty observable.","triggerScenarios":"Calling source.Take(negativeTimeSpan), typically from a computed window such as endTime - now when now is past endTime.","commonSituations":"Deadline-based windows where the deadline already elapsed, or a configuration value parsed as a negative duration.","solutions":["Clamp the duration: if (d < TimeSpan.Zero) d = TimeSpan.Zero;","Return Empty<T>() early when the computed window is non-positive","Validate configured duration values at startup"],"exampleFix":"// before\nvar window = source.Take(deadline - DateTime.Now); // may be negative\n// after\nvar d = deadline - DateTime.Now;\nvar window = d > TimeSpan.Zero ? source.Take(d) : AsyncObservable.Empty<T>();","handlingStrategy":"validation","validationCode":"if (duration < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(duration));\nvar window = source.Take(duration);","typeGuard":"bool IsValidDuration(TimeSpan d) => d >= TimeSpan.Zero;","tryCatchPattern":"try { var window = source.Take(duration); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"duration\") { /* treat as elapsed: use Empty */ }","preventionTips":["Clamp deadline-derived durations to TimeSpan.Zero minimum","Validate parsed duration configuration values","Handle already-elapsed deadlines before building the window"],"tags":["argument-out-of-range","duration","async-rx"],"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"}