{"record":{"id":"5a5a1b34392fddee","repo":"dotnet/reactive","slug":"exception-of-type-system-invalidoperationexception-was","errorCode":null,"errorMessage":"Exception of type 'System.InvalidOperationException' was thrown.","messagePattern":"Exception of type 'System\\.InvalidOperationException' was thrown\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Never.cs","lineNumber":38,"sourceCode":"        private sealed class NeverAsyncEnumerable<TValue> : IAsyncEnumerable<TValue>\n        {\n            internal static readonly NeverAsyncEnumerable<TValue> Instance = new();\n\n            public IAsyncEnumerator<TValue> GetAsyncEnumerator(CancellationToken cancellationToken)\n            {\n                cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior.\n\n                return new NeverAsyncEnumerator(cancellationToken);\n            }\n\n            private sealed class NeverAsyncEnumerator(CancellationToken token) : IAsyncEnumerator<TValue>\n            {\n                private readonly CancellationToken _token = token;\n\n                private CancellationTokenRegistration _registration;\n                private bool _once;\n\n                public TValue Current => throw new InvalidOperationException();\n\n                public ValueTask DisposeAsync()\n                {\n                    _registration.Dispose();\n                    return default;\n                }\n\n                public ValueTask<bool> MoveNextAsync()\n                {\n                    if (_once)\n                    {\n                        return new ValueTask<bool>(false);\n                    }\n\n                    _once = true;\n                    var task = new TaskCompletionSource<bool>();\n                    _registration = _token.Register(state => ((TaskCompletionSource<bool>)state!).TrySetCanceled(_token), task);\n                    return new ValueTask<bool>(task.Task);","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Never.cs#L20-L56","documentation":"AsyncEnumerable.Never never produces values or completes; its enumerator's Current getter is defined to throw InvalidOperationException because accessing Current without a produced element is meaningless for this operator. This is an internal-invariant guard: there is no current element by design.","triggerScenarios":"Accessing enumerator.Current (directly or via foreach/state machine misuse) on an AsyncEnumerable.Never<TValue> enumerator — e.g. manually calling MoveNextAsync logic bypassed, or a consumer framework reading Current without a successful MoveNextAsync.","commonSituations":"Using Never in tests or placeholder pipelines and accidentally reading Current; combining Never with operators that assume at least one element; incorrect manual enumerator loops that don't check MoveNextAsync's result.","solutions":["Never read Current unless MoveNextAsync returned true; for Never it never will, so treat the stream as permanently silent.","Replace Never with a source that actually emits (e.g. AsyncEnumerable.Return) if you expected values.","If you need an abortable silent stream, await a Task that never completes honoring the token, or use Timer-based sources instead."],"exampleFix":"// before\nvar e = AsyncEnumerable.Never<int>().GetAsyncEnumerator();\nvar x = e.Current; // throws\n// after\nif (await e.MoveNextAsync()) { var x = e.Current; } // never true for Never\n","handlingStrategy":"validation","validationCode":"if (await enumerator.MoveNextAsync()) { use(enumerator.Current); } // never true for Never","typeGuard":"bool HasCurrent(IAsyncEnumerator<T> e) => e.MoveNextAsync().AsTask().GetAwaiter().GetResult();","tryCatchPattern":"try { var v = e.Current; }\ncatch (InvalidOperationException) { /* Never yields: treat as no-value */ }","preventionTips":["Only access Current after a successful MoveNextAsync.","Never expect values from AsyncEnumerable.Never — use it purely as a placeholder/park.","In tests, assert the sequence never emits instead of reading Current."],"tags":["invalid-operation","async-streams","never","enumerator"],"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"}