{"record":{"id":"d26548ed11040e50","repo":"dotnet/reactive","slug":"delay-cannot-be-less-than-zero","errorCode":null,"errorMessage":"Delay cannot be less than zero","messagePattern":"Delay cannot be less than zero","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Binding.cs","lineNumber":259,"sourceCode":"\n        /// <summary>\n        /// Returns an observable sequence that stays connected to the source as long as there is at least one subscription to the observable sequence.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Connectable observable sequence.</param>\n        /// <param name=\"disconnectDelay\">The time span that should be waited before possibly unsubscribing from the connectable observable.</param>\n        /// <returns>An observable sequence that stays connected to the source as long as there is at least one subscription to the observable sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> is null.</exception>\n        public static IObservable<TSource> RefCount<TSource>(this IConnectableObservable<TSource> source, TimeSpan disconnectDelay)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (disconnectDelay < TimeSpan.Zero)\n            {\n                throw new ArgumentException(\"Delay cannot be less than zero\", nameof(disconnectDelay));\n            }\n\n            return s_impl.RefCount(source, disconnectDelay);\n        }\n\n        /// <summary>\n        /// Returns an observable sequence that stays connected to the source as long as there is at least one subscription to the observable sequence.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Connectable observable sequence.</param>\n        /// <param name=\"disconnectDelay\">The time span that should be waited before possibly unsubscribing from the connectable observable.</param>\n        /// <param name=\"scheduler\">The scheduler to use for delayed unsubscription.</param>\n        /// <returns>An observable sequence that stays connected to the source as long as there is at least one subscription to the observable sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> is null.</exception>\n        public static IObservable<TSource> RefCount<TSource>(this IConnectableObservable<TSource> source, TimeSpan disconnectDelay, IScheduler scheduler)\n        {\n            if (source == null)\n            {","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Binding.cs#L241-L277","documentation":"RefCount(source, disconnectDelay) requires a non-negative TimeSpan; a negative delay is meaningless for scheduling disconnection and throws ArgumentException with the message 'Delay cannot be less than zero'.","triggerScenarios":"Passing TimeSpan values like TimeSpan.FromSeconds(-1), a computed delay that went negative (e.g. deadline already passed), or a config value parsed to a negative duration.","commonSituations":"Configuration files with negative timeout values, subtraction of timestamps yielding negative delays, or unit tests probing boundary values.","solutions":["Clamp the delay to TimeSpan.Zero before calling: TimeSpan.FromTicks(Math.Max(0, delay.Ticks))","Validate the configured delay when loading configuration","Use TimeSpan.Zero if immediate disconnect is intended"],"exampleFix":"// before\nvar obs = conn.RefCount(TimeSpan.FromSeconds(-1));\n// after\nvar delay = TimeSpan.FromSeconds(-1);\nvar obs = conn.RefCount(delay < TimeSpan.Zero ? TimeSpan.Zero : delay);","handlingStrategy":"validation","validationCode":"if (disconnectDelay < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(disconnectDelay), disconnectDelay, \"Delay cannot be less than zero\");","typeGuard":null,"tryCatchPattern":"try { var obs = conn.RefCount(delay); } catch (ArgumentException ex) when (ex.ParamName == \"disconnectDelay\") { delay = TimeSpan.Zero; }","preventionTips":["Clamp computed delays with Math.Max on ticks","Validate TimeSpan values from configuration on load","Use TimeSpan.Zero for immediate disconnect semantics"],"tags":["argument-range","reactive-extensions","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-23T08:17:48.524Z"}