App-vNext/Polly · error · ArgumentNullException

sleepDurations

Error message

sleepDurations

What it means

This WaitAndRetry overload (RetrySyntax.cs:453) takes an IEnumerable<TimeSpan> sleepDurations and throws ArgumentNullException when that collection is null. The collection defines both the per-attempt wait and the retry count (one attempt per element), so a null collection gives Polly no retry plan.

Source

Thrown at src/Polly/Retry/RetrySyntax.cs:453

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

    /// <summary>
    /// Builds a <see cref="Policy"/> that will wait and retry as many times as there are provided <paramref name="sleepDurations"/>
    /// calling <paramref name="onRetry"/> on each retry with the raised exception, current sleep duration, retry count and context data.
    /// On each retry, the duration to wait is the current <paramref name="sleepDurations"/> item.
    /// </summary>
    /// <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 WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable<TimeSpan> sleepDurations, Action<Exception, TimeSpan, int, Context> onRetry)
    {
        if (sleepDurations == null)
        {
            throw new ArgumentNullException(nameof(sleepDurations));
        }

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

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

    /// <summary>
    /// Builds a <see cref="Policy"/> 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 IEnumerable<TimeSpan>, e.g. an array of wait durations.
  2. If the list is built dynamically, default to Enumerable.Empty<TimeSpan>() or a sensible fallback array rather than null.
  3. Verify config binding produced a populated collection.

Example fix

// before
TimeSpan[] durations = config.GetSection("Durations").Get<TimeSpan[]>();
var policy = Policy.Handle<X>().WaitAndRetry(durations, onRetry); // durations may be null
// after
var durations = config.GetSection("Durations").Get<TimeSpan[]>() ?? Array.Empty<TimeSpan>();
var policy = Policy.Handle<X>().WaitAndRetry(durations, onRetry);
Defensive patterns

Strategy: validation

Validate before calling

durations ??= Array.Empty<TimeSpan>();
if (durations is null) throw new ArgumentNullException(nameof(durations));
var policy = Policy.Handle<X>().WaitAndRetry(durations, onRetry);

Prevention

When it happens

Trigger: Calling Policy.Handle<...>().WaitAndRetry(null, onRetry) — passing null for the IEnumerable<TimeSpan> sleepDurations.

Common situations: A collection builder returned null; a config section that maps to a TimeSpan array was empty/unmapped and deserialized to null; conditional initialization that never assigned the list.

Related errors


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