{"record":{"id":"7bf2ac9e189ce49c","repo":"dotnet/reactive","slug":"duration-takelastbuffer","errorCode":null,"errorMessage":"duration","messagePattern":"duration","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeLastBuffer.cs","lineNumber":36,"sourceCode":"                throw new ArgumentOutOfRangeException(nameof(count));\n\n            if (count == 0)\n            {\n                return Empty<IList<TSource>>();\n            }\n\n            return CreateAsyncObservable<IList<TSource>>.From(\n                source,\n                count,\n                static (source, count, observer) => source.SubscribeSafeAsync(AsyncObserver.TakeLastBuffer(observer, count)));\n        }\n\n        public static IAsyncObservable<IList<TSource>> TakeLastBuffer<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<IList<TSource>>();\n            }\n\n            return CreateAsyncObservable<IList<TSource>>.From(\n                source,\n                duration,\n                static (source, duration, observer) => source.SubscribeSafeAsync(AsyncObserver.TakeLastBuffer(observer, duration)));\n        }\n\n        public static IAsyncObservable<IList<TSource>> TakeLastBuffer<TSource>(this IAsyncObservable<TSource> source, TimeSpan duration, IClock clock)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (duration < TimeSpan.Zero)\n                throw new ArgumentOutOfRangeException(nameof(duration));","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeLastBuffer.cs#L18-L54","documentation":"TakeLastBuffer(source, duration) rejects negative TimeSpans with ArgumentOutOfRangeException. A negative window is meaningless for 'buffer the last duration worth of elements'; TimeSpan.Zero is allowed and returns an empty buffer observable.","triggerScenarios":"source.TakeLastBuffer(TimeSpan.FromSeconds(-5)) — from timestamp subtraction in the wrong order or negative config values.","commonSituations":"Computing windows from DateTime differences incorrectly; user-supplied retention windows parsed without validation; timezone/DST arithmetic producing negative deltas.","solutions":["Pass a non-negative TimeSpan; clamp negatives to TimeSpan.Zero.","Fix the window computation so end >= start.","Validate configured durations at application startup."],"exampleFix":"// before\nvar buf = source.TakeLastBuffer(window); // window may be negative\n// after\nvar safeWindow = window < TimeSpan.Zero ? TimeSpan.Zero : window;\nvar buf = source.TakeLastBuffer(safeWindow);","handlingStrategy":"validation","validationCode":"if (duration < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(duration));\nsource.TakeLastBuffer(duration);","typeGuard":null,"tryCatchPattern":"try { var buf = source.TakeLastBuffer(duration); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"duration\") { /* clamp to TimeSpan.Zero */ }","preventionTips":["Clamp windows: Math.Max(TimeSpan.Zero, d).","Validate parsed/configured durations before use.","Watch for negative deltas from timestamp subtraction and DST transitions."],"tags":["argument-out-of-range","asyncrx","takelastbuffer","duration","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"}