{"record":{"id":"325f76eb375345fb","repo":"App-vNext/Polly","slug":"timeoutprovider","errorCode":null,"errorMessage":"timeoutProvider","messagePattern":"timeoutProvider","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Polly/Timeout/AsyncTimeoutSyntax.cs","lineNumber":202,"sourceCode":"    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"onTimeoutAsync\"/> is <see langword=\"null\"/>.</exception>\n    public static AsyncTimeoutPolicy TimeoutAsync(TimeSpan timeout, TimeoutStrategy timeoutStrategy, Func<Context, TimeSpan, Task, Exception, Task> onTimeoutAsync)\n    {\n        TimeoutValidator.ValidateTimeSpanTimeout(timeout);\n\n        return TimeoutAsync(_ => timeout, timeoutStrategy, onTimeoutAsync);\n    }\n\n    /// <summary>\n    /// 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.\n    /// </summary>\n    /// <param name=\"timeoutProvider\">A function to provide the timeout for this execution.</param>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"timeoutProvider\"/> is <see langword=\"null\"/>.</exception>\n    /// <returns>The policy instance.</returns>\n    public static AsyncTimeoutPolicy TimeoutAsync(Func<TimeSpan> timeoutProvider)\n    {\n        if (timeoutProvider == null)\n        {\n            throw new ArgumentNullException(nameof(timeoutProvider));\n        }\n\n        return TimeoutAsync(_ => timeoutProvider(), TimeoutStrategy.Optimistic, EmptyHandlerAsync);\n    }\n\n    /// <summary>\n    /// 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.\n    /// </summary>\n    /// <param name=\"timeoutProvider\">A function to provide the timeout for this execution.</param>\n    /// <param name=\"timeoutStrategy\">The timeout strategy.</param>\n    /// <returns>The policy instance.</returns>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"timeoutProvider\"/> is <see langword=\"null\"/>.</exception>\n    public static AsyncTimeoutPolicy TimeoutAsync(Func<TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy)\n    {\n        if (timeoutProvider == null)\n        {\n            throw new ArgumentNullException(nameof(timeoutProvider));\n        }","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/App-vNext/Polly/blob/d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac/src/Polly/Timeout/AsyncTimeoutSyntax.cs#L184-L220","documentation":"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.","triggerScenarios":"Policy.TimeoutAsync((Func<TimeSpan>)null) — null passed as the timeout provider.","commonSituations":"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.","solutions":["Pass a non-null Func<TimeSpan>, e.g. () => TimeSpan.FromSeconds(30).","If a fixed timeout suffices, use the TimeoutAsync(TimeSpan) or TimeoutAsync(int seconds) overload instead.","Guard the provider in the composition root before building the policy."],"exampleFix":"// before\nFunc<TimeSpan> provider = config.GetTimeoutProvider(); // null when missing\nvar policy = Policy.TimeoutAsync(provider);\n\n// after\nFunc<TimeSpan> provider = config.GetTimeoutProvider()\n    ?? (() => TimeSpan.FromSeconds(30));\nvar policy = Policy.TimeoutAsync(provider);","handlingStrategy":"validation","validationCode":"if (timeoutProvider is null) throw new InvalidOperationException(\"timeoutProvider is required.\");\nvar policy = Policy.TimeoutAsync(timeoutProvider);","typeGuard":"static bool IsValidTimeoutProvider(Func<TimeSpan> provider) => provider is not null;","tryCatchPattern":null,"preventionTips":["Prefer the TimeoutAsync(TimeSpan) overload when the timeout is fixed; it removes the nullable provider.","Coalesce null providers with a default function in the composition root.","Source timeout providers from a single factory so they are never null."],"tags":["argumentnullexception","timeout","configuration","validation","polly"],"backgroundTag":null,"analyzedSha":"d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac","analyzedAt":"2026-08-13T16:36:01.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}