{"record":{"id":"666d35afce551b65","repo":"App-vNext/Polly","slug":"sleepdurationprovider","errorCode":null,"errorMessage":"sleepDurationProvider","messagePattern":"sleepDurationProvider","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Polly/Retry/AsyncRetrySyntax.cs","lineNumber":886,"sourceCode":"        => policyBuilder.WaitAndRetryForeverAsync(sleepDurationProvider, EmptyHandlerWithContext);\n\n    /// <summary>\n    /// Builds an <see cref=\"AsyncRetryPolicy\"/> that will wait and retry indefinitely\n    /// calling <paramref name=\"onRetry\"/> on each retry with the raised exception.\n    ///     On each retry, the duration to wait is calculated by calling <paramref name=\"sleepDurationProvider\" /> with\n    ///     the current retry number (1 for first retry, 2 for second etc).\n    /// </summary>\n    /// <param name=\"policyBuilder\">The policy builder.</param>\n    /// <param name=\"sleepDurationProvider\">A function providing the duration to wait before retrying.</param>\n    /// <param name=\"onRetry\">The action to call on each retry.</param>\n    /// <returns>The policy instance.</returns>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"sleepDurationProvider\"/> is <see langword=\"null\"/>.</exception>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"onRetry\"/> is <see langword=\"null\"/>.</exception>\n    public static AsyncRetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func<int, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan> onRetry)\n    {\n        if (sleepDurationProvider == null)\n        {\n            throw new ArgumentNullException(nameof(sleepDurationProvider));\n        }\n\n        if (onRetry == null)\n        {\n            throw new ArgumentNullException(nameof(onRetry));\n        }\n\n        return policyBuilder.WaitAndRetryForeverAsync(\n            (retryCount, _) => sleepDurationProvider(retryCount),\n            (exception, timespan, _) => onRetry(exception, timespan));\n    }\n\n    /// <summary>\n    /// Builds an <see cref=\"AsyncRetryPolicy\"/> that will wait and retry indefinitely\n    /// calling <paramref name=\"onRetry\"/> on each retry with the raised exception and retry count.\n    ///     On each retry, the duration to wait is calculated by calling <paramref name=\"sleepDurationProvider\" /> with\n    ///     the current retry number (1 for first retry, 2 for second etc).\n    /// </summary>","sourceCodeStart":868,"sourceCodeEnd":904,"githubUrl":"https://github.com/App-vNext/Polly/blob/d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac/src/Polly/Retry/AsyncRetrySyntax.cs#L868-L904","documentation":"ArgumentNullException is thrown when sleepDurationProvider is null in WaitAndRetryForeverAsync(Func<int,TimeSpan> sleepDurationProvider, Action<Exception,TimeSpan> onRetry). This function is the sole source of wait durations for the infinite retry policy and cannot be omitted.","triggerScenarios":"Calling WaitAndRetryForeverAsync with a null sleepDurationProvider — typically from a factory or DI container that did not resolve the delegate.","commonSituations":"DI registration missing for the provider delegate, or a conditional assignment that left the variable null on an unhandled branch.","solutions":["Supply a non-null lambda: retryAttempt => TimeSpan.FromSeconds(retryAttempt).","Verify DI registration of the provider before building the policy.","Use a named local function instead of a variable to guarantee non-null."],"exampleFix":"// before\n.WaitAndRetryForeverAsync(null, onRetry)\n\n// after\n.WaitAndRetryForeverAsync(retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), onRetry)","handlingStrategy":"validation","validationCode":"if (sleepDurationProvider is null)\n    throw new InvalidOperationException(\"sleepDurationProvider must be configured.\");","typeGuard":"static bool IsValidProvider(Func<int, TimeSpan> provider) => provider is not null;","tryCatchPattern":null,"preventionTips":["Inline the provider lambda at the call site.","Register provider delegates via DI with non-null factory methods."],"tags":["retry","argument-validation","null-reference","polly-v7"],"backgroundTag":null,"analyzedSha":"d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac","analyzedAt":"2026-08-13T16:36:01.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}