dotnet/reactive · error · InvalidOperationException
Sequence contains no elements
Error message
Sequence contains no elements
What it means
AsyncSubject.GetResult (the task-like GetResult used by await and by blocking waiters) throws InvalidOperationException("Sequence contains no elements") when the subject completed without ever producing a value and without an error. An AsyncSubject only yields a value if OnNext was called at least once before OnCompleted.
Solutions
- Guard with await subject.FirstOrDefaultAsync() or use .DefaultIfEmpty() before awaiting.
- Check subject.Has.Value pattern or subscribe with a default in your pipeline.
- If empty results are valid, catch InvalidOperationException around GetResult/await.
- Fix the upstream source so it emits at least one value before completion.
Example fix
// before var value = subject.GetResult(); // after var value = await subject.DefaultIfEmpty(defaultValue).FirstAsync();
Defensive patterns
Strategy: try-catch
Validate before calling
var hasValue = await subject.AnyAsync(); if (!hasValue) value = defaultValue; else value = await subject.FirstAsync();
Try / catch
try { var value = subject.GetResult(); }
catch (InvalidOperationException) { var value = defaultValue; } Prevention
- Apply DefaultIfEmpty before awaiting an AsyncSubject.
- Only await subjects guaranteed to receive at least one OnNext before completion.
When it happens
Trigger: Awaiting or calling GetResult on an AsyncSubject<T> that received OnCompleted with zero OnNext calls (or with only OnError, that path throws the stored exception instead).
Common situations: A source sequence that legitimately emitted nothing; a filtered/empty source; a race where GetResult runs before any value arrived but after completion flag read.
Understand the failure class
Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.
Related errors
- Strings_Linq.NO_ELEMENTS
- Strings_Linq.NO_ELEMENTS
- NO_MATCHING_ELEMENTS
- Strings_Linq.NO_ELEMENTS
- Strings_Linq.NO_ELEMENTS
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/38bcf3ca0a0b5075.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive/Subjects/AsyncSubject.cs:434
public T GetResult()
{
if (Volatile.Read(ref _observers) != Terminated)
{
using var e = new ManualResetEventSlim(initialState: false);
//
// [OK] Use of unsafe Subscribe: this type's Subscribe implementation is safe.
//
Subscribe/*Unsafe*/(new BlockingObserver(e));
e.Wait();
}
_exception?.Throw();
if (!_hasValue)
{
throw new InvalidOperationException(Strings_Linq.NO_ELEMENTS);
}
return _value!;
}
private sealed class BlockingObserver : IObserver<T>
{
private readonly ManualResetEventSlim _e;
public BlockingObserver(ManualResetEventSlim e) => _e = e;
public void OnCompleted() => Done();
public void OnError(Exception error) => Done();
public void OnNext(T value) { }
private void Done() => _e.Set();View on GitHub (pinned to 94b5d5ab91)