App-vNext/Polly · error · ArgumentNullException

timeoutProvider

Error message

timeoutProvider

What it means

ArgumentNullException (ParamName "timeoutProvider") is thrown at configuration time by TimeoutAsync(Func<TimeSpan> timeoutProvider) when the provider is null (AsyncTimeoutSyntax.cs:200-203). The provider is mandatory because the timeout is resolved per-execution; without it the policy cannot compute a deadline. This overload wraps the provider in a Context-ignoring lambda and uses the optimistic strategy with an empty timeout handler.

Source

Thrown at src/Polly/Timeout/AsyncTimeoutSyntax.cs:202

    /// <exception cref="ArgumentNullException">Thrown when <paramref name="onTimeoutAsync"/> is <see langword="null"/>.</exception>
    public static AsyncTimeoutPolicy TimeoutAsync(TimeSpan timeout, TimeoutStrategy timeoutStrategy, Func<Context, TimeSpan, Task, Exception, Task> onTimeoutAsync)
    {
        TimeoutValidator.ValidateTimeSpanTimeout(timeout);

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

    /// <summary>
    /// Builds an <see cref="AsyncPolicy"/> 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>
    /// <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 TimeoutAsync(Func<TimeSpan> timeoutProvider)
    {
        if (timeoutProvider == null)
        {
            throw new ArgumentNullException(nameof(timeoutProvider));
        }

        return TimeoutAsync(_ => timeoutProvider(), TimeoutStrategy.Optimistic, EmptyHandlerAsync);
    }

    /// <summary>
    /// Builds an <see cref="AsyncPolicy" /> 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>
    /// <param name="timeoutProvider">A function to provide the timeout for this execution.</param>
    /// <param name="timeoutStrategy">The timeout strategy.</param>
    /// <returns>The policy instance.</returns>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="timeoutProvider"/> is <see langword="null"/>.</exception>
    public static AsyncTimeoutPolicy TimeoutAsync(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 a fixed timeout suffices, use the TimeoutAsync(TimeSpan) or TimeoutAsync(int seconds) overload instead.
  3. Guard the provider in the composition root before building the policy.

Example fix

// before
Func<TimeSpan> provider = config.GetTimeoutProvider(); // null when missing
var policy = Policy.TimeoutAsync(provider);

// after
Func<TimeSpan> provider = config.GetTimeoutProvider()
    ?? (() => TimeSpan.FromSeconds(30));
var policy = Policy.TimeoutAsync(provider);
Defensive patterns

Strategy: validation

Validate before calling

if (timeoutProvider is null) throw new InvalidOperationException("timeoutProvider is required.");
var policy = Policy.TimeoutAsync(timeoutProvider);

Type guard

static bool IsValidTimeoutProvider(Func<TimeSpan> provider) => provider is not null;

Prevention

When it happens

Trigger: Policy.TimeoutAsync((Func<TimeSpan>)null) — null passed as the timeout provider.

Common situations: Config-driven timeout function that resolved to null; refactor that extracted the provider into a field left null; tests stubbing only part of the call.

Understand the failure class

Related errors


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