{"record":{"id":"f8ef05d61d439872","repo":"dotnet/reactive","slug":"specified-argument-was-out-of-the-range-of-valid-values-f8ef05","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'capacity')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'capacity'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Concurrency/SchedulerQueue.cs","lineNumber":35,"sourceCode":"\n        /// <summary>\n        /// Creates a new scheduler queue with a default initial capacity.\n        /// </summary>\n        public SchedulerQueue()\n            : this(1024)\n        {\n        }\n\n        /// <summary>\n        /// Creates a new scheduler queue with the specified initial capacity.\n        /// </summary>\n        /// <param name=\"capacity\">Initial capacity of the scheduler queue.</param>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"capacity\"/> is less than zero.</exception>\n        public SchedulerQueue(int capacity)\n        {\n            if (capacity < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(capacity));\n            }\n\n            _queue = new PriorityQueue<ScheduledItem<TAbsolute>>(capacity);\n        }\n\n        /// <summary>\n        /// Gets the number of scheduled items in the scheduler queue.\n        /// </summary>\n        public int Count => _queue.Count;\n\n        /// <summary>\n        /// Enqueues the specified work item to be scheduled.\n        /// </summary>\n        /// <param name=\"scheduledItem\">Work item to be scheduled.</param>\n        public void Enqueue(ScheduledItem<TAbsolute> scheduledItem) => _queue.Enqueue(scheduledItem);\n\n        /// <summary>\n        /// Removes the specified work item from the scheduler queue.","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Concurrency/SchedulerQueue.cs#L17-L53","documentation":"SchedulerQueue<TAbsolute>'s constructor validates that the initial capacity is non-negative and throws ArgumentOutOfRangeException naming 'capacity'. The capacity is forwarded to the underlying PriorityQueue, which cannot allocate with a negative size.","triggerScenarios":"new SchedulerQueue<DateTimeOffset>(-1); a computed capacity that underflowed (e.g. size - reserved); copying a default(-1)-like value from config.","commonSituations":"Integer arithmetic producing negative capacity; uninitialized config values interpreted as -1; porting code from collections that allowed -1.","solutions":["Pass 0 or a positive capacity","Clamp computed capacity with Math.Max(0, computed)","Fix the source of the negative number (config default, subtraction underflow)"],"exampleFix":"// before\nint capacity = expectedCount - reservedCount; // can be negative\nvar queue = new SchedulerQueue<DateTimeOffset>(capacity);\n// after\nint capacity = Math.Max(0, expectedCount - reservedCount);\nvar queue = new SchedulerQueue<DateTimeOffset>(capacity);","handlingStrategy":"validation","validationCode":"if (capacity < 0) capacity = 0;","typeGuard":"static bool IsValidCapacity(int c) => c >= 0;","tryCatchPattern":"try { var q = new SchedulerQueue<DateTimeOffset>(capacity); }\ncatch (ArgumentOutOfRangeException) { var q = new SchedulerQueue<DateTimeOffset>(0); }","preventionTips":["Clamp computed sizes with Math.Max(0, x)","Avoid -1 sentinels flowing into capacity parameters","Assert capacity bounds in configuration parsing"],"tags":["argument-out-of-range","scheduler","queue"],"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"}