{"record":{"id":"3ac6d80bb0958e03","repo":"dotnet/reactive","slug":"capacity","errorCode":null,"errorMessage":"capacity","messagePattern":"capacity","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/GroupBy.cs","lineNumber":398,"sourceCode":"                throw new ArgumentNullException(nameof(elementSelector));\n            if (capacity < 0)\n                throw new ArgumentOutOfRangeException(nameof(capacity));\n\n            return GroupBy(observer, subscription, keySelector, elementSelector, capacity, EqualityComparer<TKey>.Default);\n        }\n\n        public static (IAsyncObserver<TSource>, IAsyncDisposable) GroupBy<TSource, TKey, TElement>(IAsyncObserver<IGroupedAsyncObservable<TKey, TElement>> observer, IAsyncDisposable subscription, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, int capacity, IEqualityComparer<TKey> comparer)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (subscription == null)\n                throw new ArgumentNullException(nameof(subscription));\n            if (keySelector == null)\n                throw new ArgumentNullException(nameof(keySelector));\n            if (elementSelector == null)\n                throw new ArgumentNullException(nameof(elementSelector));\n            if (capacity < 0)\n                throw new ArgumentOutOfRangeException(nameof(capacity));\n            if (comparer == null)\n                throw new ArgumentNullException(nameof(comparer));\n\n            return GroupBy<TSource, TKey, TElement>(observer, subscription, x => new ValueTask<TKey>(keySelector(x)), x => new ValueTask<TElement>(elementSelector(x)), capacity, comparer);\n        }\n\n        public static (IAsyncObserver<TSource>, IAsyncDisposable) GroupBy<TSource, TKey>(IAsyncObserver<IGroupedAsyncObservable<TKey, TSource>> observer, IAsyncDisposable subscription, Func<TSource, ValueTask<TKey>> keySelector)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (subscription == null)\n                throw new ArgumentNullException(nameof(subscription));\n            if (keySelector == null)\n                throw new ArgumentNullException(nameof(keySelector));\n\n            return GroupBy(observer, subscription, keySelector, int.MaxValue, EqualityComparer<TKey>.Default);\n        }\n","sourceCodeStart":380,"sourceCodeEnd":416,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/GroupBy.cs#L380-L416","documentation":"In the 6-argument GroupBy overload, a negative capacity throws ArgumentOutOfRangeException named 'capacity' (line 398). The message surface here shows only the parameter name, which is the framework's abbreviated rendering of the range violation. Capacity sizes the internal group map and must be non-negative.","triggerScenarios":"Calling GroupBy<TSource,TKey,TElement>(observer, subscription, keySelector, elementSelector, capacity < 0, comparer) — usually with capacity derived from config, user input, or arithmetic that can go negative.","commonSituations":"'Unset' config sentinel values like -1 passed through; computed sizes such as maxItems - extra; deserialized settings where capacity was omitted and defaulted to a negative number.","solutions":["Clamp before calling: Math.Max(0, capacity).","Substitute a default positive capacity (e.g. 16) when the value is unknown or negative.","Validate the config/input source and reject or correct negative values at load time.","Log the raw source value to find where the negative number originates."],"exampleFix":"// before\nGroupBy(observer, sub, k, e, settings.InitialGroupCapacity, cmp); // can be -1\n// after\nvar cap = Math.Max(16, settings.InitialGroupCapacity);\nGroupBy(observer, sub, k, e, cap, cmp);","handlingStrategy":"validation","validationCode":"if (capacity < 0) throw new ArgumentOutOfRangeException(nameof(capacity));\nGroupBy(observer, subscription, keySelector, elementSelector, capacity, comparer);","typeGuard":"bool IsValidCapacity(int c) => c >= 0;","tryCatchPattern":"try { GroupBy(obs, sub, k, e, cap, cmp); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"capacity\") { /* clamp value and retry */ }","preventionTips":["Sanitize capacity-bearing config values at load.","Use Math.Max(0, value) on any computed capacity.","Replace -1 'unset' sentinels with nullable ints handled explicitly.","Log the raw value when validation fails to find the source."],"tags":["argument-out-of-range","capacity","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"}