App-vNext/Polly · error · ArgumentNullException

timeoutProvider

Error message

timeoutProvider

What it means

Thrown by the parameterless-callback overload TimeoutAsync<TResult>(Func<TimeSpan> timeoutProvider) when the provider is null. This overload wires a default empty handler and forces Optimistic strategy, so the only thing it validates is the provider itself. Construction-time guard.

Source

Thrown at src/Polly/Timeout/AsyncTimeoutTResultSyntax.cs:220

        {
            throw new ArgumentOutOfRangeException(nameof(timeout));
        }

        return TimeoutAsync<TResult>(_ => timeout, timeoutStrategy, onTimeoutAsync);
    }

    /// <summary>
    /// Builds an <see cref="AsyncPolicy{TResult}"/> that will wait asynchronously for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException"/> will be thrown if the delegate does not complete within the configured timeout.
    /// </summary>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="timeoutProvider">A function to provide the timeout for this execution.</param>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="timeoutProvider"/> is <see langword="null"/>.</exception>
    /// <returns>The policy instance.</returns>
    public static AsyncTimeoutPolicy<TResult> TimeoutAsync<TResult>(Func<TimeSpan> timeoutProvider)
    {
        if (timeoutProvider == null)
        {
            throw new ArgumentNullException(nameof(timeoutProvider));
        }

        return TimeoutAsync<TResult>(_ => timeoutProvider(), TimeoutStrategy.Optimistic, EmptyHandlerOfT<TResult>);
    }

    /// <summary>
    /// Builds an <see cref="AsyncPolicy{TResult}"/> that will wait asynchronously for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException"/> will be thrown if the delegate does not complete within the configured timeout.
    /// </summary>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="timeoutProvider">A function to provide the timeout for this execution.</param>
    /// <param name="timeoutStrategy">The timeout strategy.</param>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="timeoutProvider"/> is <see langword="null"/>.</exception>
    /// <returns>The policy instance.</returns>
    public static AsyncTimeoutPolicy<TResult> TimeoutAsync<TResult>(Func<TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy)
    {
        if (timeoutProvider == null)
        {
            throw new ArgumentNullException(nameof(timeoutProvider));

View on GitHub (pinned to d0e46bdb1e)

Solutions

  1. Pass a non-null Func<TimeSpan>, e.g. () => TimeSpan.FromSeconds(30).
  2. If the timeout is static, prefer TimeoutAsync<TResult>(TimeSpan) which takes the value directly.
  3. Default the provider before the call: provider ?? (() => TimeSpan.FromSeconds(30)).

Example fix

// before
Func<TimeSpan> provider = null;
Policy.TimeoutAsync<MyResult>(provider);

// after
Func<TimeSpan> provider = () => TimeSpan.FromSeconds(30);
Policy.TimeoutAsync<MyResult>(provider);
Defensive patterns

Strategy: validation

Validate before calling

timeoutProvider ??= () => TimeSpan.FromSeconds(30);
var policy = Policy.TimeoutAsync<TResult>(timeoutProvider);

Type guard

static bool HasProvider(Func<TimeSpan> p) => p is not null;

Prevention

When it happens

Trigger: Calling Policy.TimeoutAsync<TResult>(null) or with a provider variable that is null. Reached when a simpler 'no callback' usage is wanted but the provider was never assigned.

Common situations: DI returning null for a configured provider; refactoring a fixed timeout into a provider and forgetting the assignment; optional parameter defaulting to null.

Understand the failure class

Related errors


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