dotnet/reactive · error · ArgumentOutOfRangeException

Specified argument was out of the range of valid values…

Error message

Specified argument was out of the range of valid values. (Parameter 'retryCount')

What it means

The Retry(retryCount) overload validates that retryCount is non-negative; the generic ArgumentOutOfRangeException fires for parameter 'retryCount' when it is negative. Fix: pass 0 or a positive retry limit.

Solutions

  1. Validate retryCount >= 0 before calling
  2. Clamp with Math.Max(0, retryCount)
  3. Sanitize config/user input that supplies the retry limit

Example fix

// before
var result = source.Retry(retries);
// after
var result = source.Retry(Math.Max(0, retries));
Defensive patterns

Strategy: validation

Validate before calling

if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount));
var result = source.Retry(Math.Max(0, retryCount));

Try / catch

try { var result = source.Retry(retryCount); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "retryCount") { /* clamp or report invalid setting */ }

Prevention

When it happens

Trigger: Calling Retry(source, -1) or a negative count computed from user input, config, or arithmetic (e.g. remaining = budget - spent going negative).

Common situations: Retry limits read from app settings without validation; off-by-one arithmetic producing -1; defaults not initialized.

Related errors


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

Appendix: source

Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/Retry.cs:38

                throw new ArgumentNullException(nameof(source));

            return new[] { source }.Repeat().Catch();
        }

        /// <summary>
        /// Creates a sequence that retries enumerating the source sequence as long as an error occurs, with the specified
        /// maximum number of retries.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="retryCount">Maximum number of retries.</param>
        /// <returns>Sequence concatenating the results of the source sequence as long as an error occurs.</returns>
        public static IEnumerable<TSource> Retry<TSource>(this IEnumerable<TSource> source, int retryCount)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (retryCount < 0)
                throw new ArgumentOutOfRangeException(nameof(retryCount));

            return new[] { source }.Repeat(retryCount).Catch();
        }
    }
}

View on GitHub (pinned to 94b5d5ab91)