{"record":{"id":"436119c6304d8c80","repo":"dotnet/reactive","slug":"element-no-longer-available-in-the-buffer","errorCode":null,"errorMessage":"Element no longer available in the buffer.","messagePattern":"Element no longer available in the buffer\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/RefCountList.cs","lineNumber":27,"sourceCode":"{\n    internal sealed class RefCountList<T>(int readerCount) : IRefCountList<T>\n    {\n        private readonly IDictionary<int, RefCount> _list = new Dictionary<int, RefCount>();\n\n        public int ReaderCount { get; set; } = readerCount;\n\n        public void Clear() => _list.Clear();\n\n        public int Count { get; private set; }\n\n        public T this[int i]\n        {\n            get\n            {\n                Debug.Assert(i < Count);\n\n                if (!_list.TryGetValue(i, out var res))\n                    throw new InvalidOperationException(\"Element no longer available in the buffer.\");\n\n                var val = res.Value;\n\n                if (--res.Count == 0)\n                {\n                    _list.Remove(i);\n                }\n\n                return val;\n            }\n        }\n\n        public void Add(T item)\n        {\n            _list[Count] = new RefCount(item, ReaderCount);\n\n            Count++;\n        }","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/RefCountList.cs#L9-L45","documentation":"RefCountList<T> is an internal ref-counted circular buffer used by buffering operators such as Memoize/Buffer with reference semantics. Its indexer decrements the reference count for index i and removes entries whose count reaches zero. If the entry is already gone, the buffer's contract (Debug.Assert(i < Count)) was violated by the caller, and the list throws InvalidOperationException(\"Element no longer available in the buffer.\") instead of returning the value.","triggerScenarios":"Enumerating a buffered/shared sequence more times or in more overlapping passes than the buffer capacity/ref-counting allows; reading an index after its references were exhausted (item evicted from the circular buffer); concurrent enumeration of the shared buffer from multiple consumers.","commonSituations":"Multiple foreach loops over one Memoize'd/buffered sequence with too small a buffer, re-enumerating an enumerator past eviction, or consumers of different speeds over a shared buffer sequence.","solutions":["Increase the buffer capacity/size passed to the buffering operator so elements live long enough.","Ensure each consumer enumerates its own enumerator rather than sharing one enumerator across loops.","Avoid re-enumerating a shared buffered sequence more times than supported; memoize materialized results (ToList) when reuse is needed.","This is an internal invariant breach — if reproducible with normal usage, file a bug against System.Interactive with a minimal repro.","Serialize access if multiple threads enumerate the shared buffer concurrently."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { var item = buffer[i]; }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"no longer available\")) {\n    // re-enumerate from the original source or re-memoize\n}","preventionTips":["Size buffers to cover the number of overlapping consumers/re-enumerations.","Don't share a single enumerator across loops; call GetEnumerator per consumer.","Materialize (ToList) if you need unbounded replay of buffered data.","Keep consumer progress within the buffer's live window.","Report genuine invariant violations as library bugs with a repro."],"tags":["linq","invalid-operation","buffering","shared-sequence","ix-interactive"],"backgroundTag":"internal-invariant-violation","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"}