dotnet/reactive · error · InvalidOperationException

Strings_Linq.NO_ELEMENTS

Error message

Strings_Linq.NO_ELEMENTS

What it means

FirstAsync without a predicate throws InvalidOperationException with Strings_Linq.NO_ELEMENTS when the source completes without emitting any value, because there is no first element. Thrown in OnCompleted when _found is still false and forwarded via OnError.

Solutions

  1. Use FirstAsync().ToList then FirstOrDefault semantics, or FirstOrDefault via .FirstOrDefault() on the observable for a default value instead of an error
  2. Catch InvalidOperationException in OnError and supply a default
  3. Ensure the source actually emits before completion

Example fix

// before
source.FirstAsync().Subscribe(x => ..., ex => throw ex);
// after
source.FirstOrDefaultAsync().Subscribe(x => Console.WriteLine(x));
Defensive patterns

Strategy: try-catch

Try / catch

source.FirstAsync().Subscribe(
    x => Console.WriteLine(x),
    ex => { if (ex is InvalidOperationException) Console.WriteLine(default); else throw ex; });

Prevention

When it happens

Trigger: Subscribing to source.FirstAsync() where the source completes with zero OnNext calls.

Common situations: Taking the first message from an empty queue stream; awaiting the first event of a source that has already completed; filtering upstream left nothing to observe.

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


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/c6ca508edff43813. Report an issue: GitHub.

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable/FirstAsync.cs:44

                public _(IObserver<TSource> observer)
                    : base(observer)
                {
                }

                public override void OnNext(TSource value)
                {
                    _found = true;
                    ForwardOnNext(value);
                    ForwardOnCompleted();
                }

                public override void OnCompleted()
                {
                    if (!_found)
                    {
                        try
                        {
                            throw new InvalidOperationException(Strings_Linq.NO_ELEMENTS);
                        }
                        catch (Exception e)
                        {
                            ForwardOnError(e);
                        }
                    }
                }
            }
        }

        internal sealed class Predicate : Producer<TSource, Predicate._>
        {
            private readonly IObservable<TSource> _source;
            private readonly Func<TSource, bool> _predicate;

            public Predicate(IObservable<TSource> source, Func<TSource, bool> predicate)
            {
                _source = source;

View on GitHub (pinned to 94b5d5ab91)