{"record":{"id":"644e00a292df9b6f","repo":"dotnet/reactive","slug":"anonymousasynciterator-cannot-be-cloned-it-is-only-intended","errorCode":null,"errorMessage":"AnonymousAsyncIterator cannot be cloned. It is only intended for use as an iterator.","messagePattern":"AnonymousAsyncIterator cannot be cloned\\. It is only intended for use as an iterator\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Linq.Async/System/Linq/AsyncEnumerator.cs","lineNumber":79,"sourceCode":"        private sealed class AnonymousAsyncIterator<T> : AsyncIterator<T>\n        {\n            private readonly Func<T> _currentFunc;\n            private readonly Func<ValueTask<bool>> _moveNext;\n            private Func<ValueTask>? _dispose;\n\n            public AnonymousAsyncIterator(Func<ValueTask<bool>> moveNext, Func<T> currentFunc, Func<ValueTask> dispose)\n            {\n                _moveNext = moveNext;\n                _currentFunc = currentFunc;\n                _dispose = dispose;\n\n                // Explicit call to initialize enumerator mode\n                GetAsyncEnumerator(default);\n            }\n\n            public override AsyncIteratorBase<T> Clone()\n            {\n                throw new NotSupportedException(\"AnonymousAsyncIterator cannot be cloned. It is only intended for use as an iterator.\");\n            }\n\n            public override async ValueTask DisposeAsync()\n            {\n                var dispose = Interlocked.Exchange(ref _dispose, null);\n\n                if (dispose != null)\n                {\n                    await dispose().ConfigureAwait(false);\n                }\n\n                await base.DisposeAsync().ConfigureAwait(false);\n            }\n\n            protected override async ValueTask<bool> MoveNextCore()\n            {\n                switch (_state)\n                {","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Linq.Async/System/Linq/AsyncEnumerator.cs#L61-L97","documentation":"AnonymousAsyncIterator is the iterator created by async LINQ operators built from lambdas/delegates; it captures compiler-generated state that cannot be duplicated. Its Clone() override therefore throws NotSupportedException stating it can only be used as an iterator. Cloning is normally an internal detail of AsyncIteratorBase used for concurrent/multiple enumeration.","triggerScenarios":"Enumerating (or re-enumerating concurrently) an async operator result whose iterator is an AnonymousAsyncIterator in a path that calls Clone(), such as multiple simultaneous MoveNextAsync consumers or code that explicitly clones the iterator.","commonSituations":"Sharing one IAsyncEnumerable across parallel consumers expecting independent iterators, calling GetEnumerator/Clone directly, or custom operators deriving from AnonymousAsyncIterator and being enumerated more than once concurrently.","solutions":["Create a new enumeration per consumer: call the operator again (each call returns a fresh IAsyncEnumerable/iterator).","Serialize access: enumerate sequentially or buffer results before sharing.","If multiple independent streams are needed, materialize with ToListAsync and enumerate the list in parallel."],"exampleFix":"// before\nvar src = AsyncEnumerable.Range(0, 10).Select(async x => x * 2);\nawait Task.WhenAll(Consume(src.GetEnumerator()), Consume(src.GetEnumerator())); // clones iterator\n// after\nvar src = AsyncEnumerable.Range(0, 10).Select(x => x * 2);\nawait Task.WhenAll(Consume(src), Consume(src)); // fresh enumerator per call","handlingStrategy":"fallback","validationCode":"// request a fresh enumerable per consumer instead of cloning iterators: each call to the operator returns an independent IAsyncEnumerable.","typeGuard":null,"tryCatchPattern":"try { consume(it.Clone()); } catch (NotSupportedException ex) when (ex.Message.Contains(\"cannot be cloned\")) { consume(source.GetEnumerator()); }","preventionTips":["Never share a single iterator/enumerator across concurrent consumers.","Call the composing operator again for each independent stream.","Materialize with ToListAsync before parallel consumption.","Avoid deriving custom operators from AnonymousAsyncIterator if multi-enumeration is required."],"tags":["async","ix-net","not-supported"],"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"}