dotnet/reactive · error · ArgumentNullException

source

Error message

source

What it means

The TakeWhile(source, predicate) operator throws ArgumentNullException because the source observable argument was null. Like all operators in this library, it validates its arguments eagerly when the pipeline is constructed so that a null source surfaces at the TakeWhile call site rather than later during subscription. The message names 'source'.

Solutions

  1. Coalesce before filtering: (source ?? AsyncObservable.Empty<TSource>()).TakeWhile(predicate).
  2. Fix the producer of the source to return AsyncObservable.Empty<TSource>() instead of null.
  3. Null-check the source at the pipeline entry and fail with a domain-specific message.

Example fix

// before
var taken = source.TakeWhile(x => x > 0);
// after
var taken = (source ?? AsyncObservable.Empty<int>()).TakeWhile(x => x > 0);
Defensive patterns

Strategy: validation

Validate before calling

if (source == null) source = AsyncObservable.Empty<TSource>();

Type guard

bool HasSource<T>(IAsyncObservable<T>? s) => s is not null;

Try / catch

try { var taken = source.TakeWhile(predicate); } catch (ArgumentNullException ex) when (ex.ParamName == "source") { var taken = AsyncObservable.Empty<TSource>(); }

Prevention

When it happens

Trigger: Calling AsyncObservable.TakeWhile(null, predicate) with a sync Func<TSource, bool> predicate.

Common situations: Pipeline built from a repository/service method that returned null instead of an empty stream; nullable collection-to-observable conversions where the input collection was null; conditional query composition where a mandatory step was skipped.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeWhile.cs:14

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information. 

using System.Threading.Tasks;

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<TSource> TakeWhile<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (predicate == null)
                throw new ArgumentNullException(nameof(predicate));

            return CreateAsyncObservable<TSource>.From(
                source,
                predicate,
                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.TakeWhile(observer, predicate)));
        }

        public static IAsyncObservable<TSource> TakeWhile<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (predicate == null)
                throw new ArgumentNullException(nameof(predicate));

            return CreateAsyncObservable<TSource>.From(
                source,

View on GitHub (pinned to 94b5d5ab91)