App-vNext/Polly · error · ArgumentNullException

sleepDurations

Error message

sleepDurations

What it means

ArgumentNullException thrown at construction by WaitAndRetry<TResult>(IEnumerable<TimeSpan> sleepDurations, Action<DelegateResult<TResult>, TimeSpan, int, Context> onRetry) when sleepDurations is null. This is the canonical full-detail overload; it validates sleepDurations first and rejects null with parameter name 'sleepDurations' because the policy iterates the collection to drive each retry.

Source

Thrown at src/Polly/Retry/RetryTResultSyntax.cs:509

        return policyBuilder.WaitAndRetry(sleepDurations, (outcome, span, _, ctx) => onRetry(outcome, span, ctx));
    }

    /// <summary>
    /// Builds a <see cref="Policy{TResult}"/> that will wait and retry as many times as there are provided <paramref name="sleepDurations"/>
    /// calling <paramref name="onRetry"/> on each retry with the handled exception or result, current sleep duration, retry count and context data.
    /// On each retry, the duration to wait is the current <paramref name="sleepDurations"/> item.
    /// </summary>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="policyBuilder">The policy builder.</param>
    /// <param name="sleepDurations">The sleep durations to wait for on each retry.</param>
    /// <param name="onRetry">The action to call on each retry.</param>
    /// <returns>The policy instance.</returns>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="sleepDurations"/> or <paramref name="onRetry"/> is <see langword="null"/>.</exception>
    public static RetryPolicy<TResult> WaitAndRetry<TResult>(this PolicyBuilder<TResult> policyBuilder, IEnumerable<TimeSpan> sleepDurations, Action<DelegateResult<TResult>, TimeSpan, int, Context> onRetry)
    {
        if (sleepDurations == null)
        {
            throw new ArgumentNullException(nameof(sleepDurations));
        }

        if (onRetry == null)
        {
            throw new ArgumentNullException(nameof(onRetry));
        }

        return new RetryPolicy<TResult>(
            policyBuilder,
            onRetry,
            sleepDurationsEnumerable: sleepDurations);
    }

    /// <summary>
    /// Builds a <see cref="Policy{TResult}"/> that will wait and retry indefinitely until the action succeeds.
    ///     On each retry, the duration to wait is calculated by calling <paramref name="sleepDurationProvider" /> with
    ///     the current retry number (1 for first retry, 2 for second etc).
    /// </summary>

View on GitHub (pinned to d0e46bdb1e)

Solutions

  1. Pass a non-null collection, even empty: Array.Empty<TimeSpan>() or new[] { TimeSpan.FromSeconds(1) }.
  2. Initialize the duration list from config with a non-null default (e.g. ?? Array.Empty<TimeSpan>()).
  3. Validate the config binding before building the policy.

Example fix

// before
var p = Policy.HandleResult<int>(r => r == 0).WaitAndRetry(_config.Backoffs, onRetry); // _config.Backoffs is null

// after
var backoffs = _config.Backoffs ?? new[] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(5) };
var p = Policy.HandleResult<int>(r => r == 0).WaitAndRetry(backoffs, onRetry);
Defensive patterns

Strategy: validation

Validate before calling

var backoffs = sleepDurations ?? throw new InvalidOperationException("sleepDurations required.");
var policy = Policy.HandleResult<T>(pred).WaitAndRetry(backoffs, onRetry);

Type guard

static bool IsDurations(IEnumerable<TimeSpan> d) => d is not null;

Prevention

When it happens

Trigger: Calling policyBuilder.WaitAndRetry<TResult>(null, onRetry), or passing a null IEnumerable<TimeSpan> field (e.g. a config list that failed to bind) as the sleepDurations argument.

Common situations: Configuration binding for the duration list returned null; developer passed a list variable that was never initialized; empty vs null confusion (empty list is valid, null is not).

Related errors


AI-assisted analysis of App-vNext/Polly@d0e46bdb1e (2026-08-13). Data as JSON: /api/errors/acc6b0ecb1cb5bd7. Report an issue: GitHub.