dotnet/reactive · error · ArgumentOutOfRangeException

ArgumentOutOfRangeException: window

Error message

ArgumentOutOfRangeException: window

What it means

The time-based Replay overload requires the replay window to be a non-negative TimeSpan. System.Reactive validates arguments eagerly at call time, and throwing ArgumentOutOfRangeException for 'window' means a negative duration was supplied. Rx deliberately fails fast before creating any subscription infrastructure.

Solutions

  1. Check the window argument at the call site and ensure it is >= TimeSpan.Zero
  2. If the window is computed, clamp or validate it before calling Replay (e.g. if (ts < TimeSpan.Zero) ts = TimeSpan.Zero)
  3. Log or assert the computed TimeSpan to find where the negative value originates

Example fix

// before
var window = endTime - startTime;
var replayed = source.Replay(window);
// after
var window = endTime - startTime;
if (window < TimeSpan.Zero) window = TimeSpan.Zero;
var replayed = source.Replay(window);
Defensive patterns

Strategy: validation

Validate before calling

if (window < TimeSpan.Zero)
    throw new ArgumentOutOfRangeException(nameof(window), "Replay window must be non-negative.");

Type guard

static bool IsValidReplayWindow(TimeSpan window) => window >= TimeSpan.Zero;

Try / catch

try
{
    replayed = source.Replay(window);
}
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "window")
{
    logger.LogWarning(ex, "Negative replay window {Window}; using default.", window);
    replayed = source.Replay(TimeSpan.Zero);
}

Prevention

When it happens

Trigger: Calling Observable.Replay(source, window) where window is a TimeSpan whose value is negative (e.g. TimeSpan.FromSeconds(-1), or a value computed from clock skew / subtraction where end < start).

Common situations: Computing a window by subtracting timestamps that came out inverted; misconfigured config values parsed into negative TimeSpans; unit tests passing TimeSpan.Zero minus something; sign errors when converting milliseconds.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Binding.cs:535

        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source sequence whose elements will be multicasted through a single shared subscription.</param>
        /// <param name="window">Maximum time length of the replay buffer.</param>
        /// <returns>A connectable observable sequence that shares a single subscription to the underlying sequence.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="window"/> is less than TimeSpan.Zero.</exception>
        /// <remarks>Subscribers will receive all the notifications of the source subject to the specified replay buffer trimming policy.</remarks>
        /// <seealso cref="ReplaySubject{T}"/>
        public static IConnectableObservable<TSource> Replay<TSource>(this IObservable<TSource> source, TimeSpan window)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (window < TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(window));
            }

            return s_impl.Replay(source, window);
        }

        /// <summary>
        /// Returns an observable sequence that is the result of invoking the selector on a connectable observable sequence that shares a single subscription to the underlying sequence replaying notifications subject to a maximum time length for the replay buffer.
        /// This operator is a specialization of Multicast using a <see cref="ReplaySubject{T}"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <typeparam name="TResult">The type of the elements in the result sequence.</typeparam>
        /// <param name="source">Source sequence whose elements will be multicasted through a single shared subscription.</param>
        /// <param name="selector">Selector function which can use the multicasted source sequence as many times as needed, without causing multiple subscriptions to the source sequence. Subscribers to the given source will receive all the notifications of the source subject to the specified replay buffer trimming policy.</param>
        /// <param name="window">Maximum time length of the replay buffer.</param>
        /// <returns>An observable sequence that contains the elements of a sequence produced by multicasting the source sequence within a selector function.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="window"/> is less than TimeSpan.Zero.</exception>
        /// <seealso cref="ReplaySubject{T}"/>

View on GitHub (pinned to 94b5d5ab91)