{"record":{"id":"d122422d8addce24","repo":"dotnet/reactive","slug":"consecutive-appends-or-prepends-with-a-count-of-more-than","errorCode":null,"errorMessage":"Consecutive appends or prepends with a count of more than int.MaxValue ({int.MaxValue}) are not supported.","messagePattern":"Consecutive appends or prepends with a count of more than int\\.MaxValue \\((.+?)\\) are not supported\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable/AppendPrepend.cs","lineNumber":482,"sourceCode":"            public Node(T value)\n                : this(null, value)\n            {\n            }\n\n            public Node(Node<T>? parent, T value)\n            {\n                Parent = parent;\n                Value = value;\n\n                if (parent == null)\n                {\n                    Count = 1;\n                }\n                else\n                {\n                    if (parent.Count == int.MaxValue)\n                    {\n                        throw new NotSupportedException($\"Consecutive appends or prepends with a count of more than int.MaxValue ({int.MaxValue}) are not supported.\");\n                    }\n\n                    Count = parent.Count + 1;\n                }\n            }\n\n            public T[] ToReverseArray()\n            {\n                var array = new T[Count];\n                var current = this;\n                for (var i = Count - 1; i >= 0; i--)\n                {\n                    array[i] = current!.Value; // NB: Count property ensures non-nullability.\n                    current = current.Parent;\n                }\n                return array;\n            }\n        }","sourceCodeStart":464,"sourceCodeEnd":500,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable/AppendPrepend.cs#L464-L500","documentation":"When Append/Prepend operators are chained, each level tracks a cumulative count. Once the chain reaches int.MaxValue consecutive appends/prepends, constructing another level throws NotSupportedException because the count can no longer be incremented within an int.","triggerScenarios":"Chaining roughly 2,147,483,647 Append or Prepend calls on the same observable sequence; in practice, unbounded loops that keep calling source = source.Append(item) or source.Prepend(item) per iteration.","commonSituations":"Building a stream incrementally inside a long-running loop instead of using Create/Observable.Create or a Subject; recursive stream-construction code without a base case.","solutions":["Refactor to build the items into a collection first and use a single source.ToObservable() plus one Append","Use Observable.Concat over a sequence of observables or a Subject/IObservable generator instead of chained Append/Prepend","Cap or batch the number of consecutive appends"],"exampleFix":"// before\nIObservable<int> src = Observable.Empty<int>();\nfor (int i = 0; i < n; i++) src = src.Append(i); // throws for huge n\n// after\nIObservable<int> src = Enumerable.Range(0, n).ToObservable();","handlingStrategy":"try-catch","validationCode":"if (appendsPlanned >= int.MaxValue) throw new InvalidOperationException(\"Too many consecutive appends; restructure the stream\");","typeGuard":null,"tryCatchPattern":"try\n{\n    src = src.Append(item);\n}\ncatch (NotSupportedException)\n{\n    // fall back to Observable.Concat over accumulated items\n    src = accumulated.ToObservable();\n}","preventionTips":["Never accumulate streams by chaining Append/Prepend in a loop","Use Enumerable/Buffer + ToObservable() or Observable.Concat for many items","Track chain depth and restructure periodically"],"tags":["notsupported","append","prepend","limit-exceeded","rx"],"backgroundTag":"unsupported-operation","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"}