dotnet/reactive · error · ArgumentNullException

source

Error message

source

What it means

The scheduled overload TakeLast(source, count, scheduler) validates all three parameters and throws ArgumentNullException (with param name 'source') at TakeLast.cs:42 when the source observable is null.

Solutions

  1. Create or obtain a valid IAsyncObservable<TSource> before calling TakeLast.
  2. Substitute AsyncObservable.Empty<TSource>() when the source is legitimately absent.
  3. Fix the upstream null-producing code path.
  4. Assert non-null source at the pipeline boundary.

Example fix

// before: AsyncObservable.TakeLast(source, 5, TaskPoolAsyncScheduler.Default); // source null  // after: var last = (source ?? AsyncObservable.Empty<int>()).TakeLast(5, TaskPoolAsyncScheduler.Default);
Defensive patterns

Strategy: validation

Validate before calling

if (source == null) throw new ArgumentNullException(nameof(source)); if (scheduler == null) scheduler = TaskPoolAsyncScheduler.Default; var last = AsyncObservable.TakeLast(source, count, scheduler);

Type guard

static bool IsValidTakeLast<TSource>(IAsyncObservable<TSource> s, int c, IAsyncScheduler sch) => s != null && c >= 0 && sch != null;

Try / catch

try { var last = AsyncObservable.TakeLast(source, count, scheduler); } catch (ArgumentNullException ex) when (ex.ParamName == "source") { var last = AsyncObservable.Empty<TSource>(); }

Prevention

When it happens

Trigger: Calling TakeLast(null, count, scheduler) or source.TakeLast(count, scheduler) where source is null; passing a scheduler overload down a chain whose upstream source was never created.

Common situations: Custom operator plumbing where a null source flows through extension methods; DI/config supplying no observable; refactors that removed the source assignment.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeLast.cs:42

            }

            return CreateAsyncObservable<TSource>.From(
                source,
                count,
                static async (source, count, observer) =>
                {
                    var (sink, drain) = AsyncObserver.TakeLast(observer, count);

                    var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);

                    return StableCompositeAsyncDisposable.Create(subscription, drain);
                });
        }

        public static IAsyncObservable<TSource> TakeLast<TSource>(this IAsyncObservable<TSource> source, int count, IAsyncScheduler scheduler)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (count < 0)
                throw new ArgumentOutOfRangeException(nameof(count));
            if (scheduler == null)
                throw new ArgumentNullException(nameof(scheduler));

            if (count == 0)
            {
                return Empty<TSource>();
            }

            return CreateAsyncObservable<TSource>.From(
                source,
                (count, scheduler),
                static async (source, state, observer) =>
                {
                    var (sink, drain) = AsyncObserver.TakeLast(observer, state.count, state.scheduler);

                    var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);

View on GitHub (pinned to 94b5d5ab91)