{"record":{"id":"2b3a8e7df1aaf5c1","repo":"App-vNext/Polly","slug":"sleepdurationprovider-2b3a8e","errorCode":null,"errorMessage":"sleepDurationProvider","messagePattern":"sleepDurationProvider","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Polly/Retry/RetrySyntax.cs","lineNumber":270,"sourceCode":"    /// 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=\"retryCount\">The retry count.</param>\n    /// <param name=\"sleepDurationProvider\">The function that provides the duration to wait for a particular retry attempt.</param>\n    /// <param name=\"onRetry\">The action to call on each retry.</param>\n    /// <returns>The policy instance.</returns>\n    /// <exception cref=\"ArgumentOutOfRangeException\">retryCount;Value must be greater than or equal to zero.</exception>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"sleepDurationProvider\"/> or <paramref name=\"onRetry\"/> is <see langword=\"null\"/>.</exception>\n    public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan, int, Context> onRetry)\n    {\n        if (retryCount < 0)\n        {\n            throw new ArgumentOutOfRangeException(nameof(retryCount), \"Value must be greater than or equal to zero.\");\n        }\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        var sleepDurations = Enumerable.Range(1, retryCount)\n                                       .Select(sleepDurationProvider);\n\n        return new RetryPolicy(\n            policyBuilder,\n            onRetry,\n            retryCount,\n            sleepDurationsEnumerable: sleepDurations);\n    }\n\n    /// <summary>","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/App-vNext/Polly/blob/d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac/src/Polly/Retry/RetrySyntax.cs#L252-L288","documentation":"Thrown by the legacy Polly v7 synchronous WaitAndRetry API when the sleepDurationProvider (Func<int, TimeSpan>) is null for the canonical overload. The provider is invoked once per retry to build the sleep-duration sequence, so a null provider would NRE during Enumerable.Select; Polly validates it at construction.","triggerScenarios":"Calling Policy.Handle<...>().WaitAndRetry(retryCount, null, onRetry) with null for the Func<int, TimeSpan> sleepDurationProvider at RetrySyntax.cs:270.","commonSituations":"Passing a backoff factory variable that is null; conditional wiring where the provider is only set in one branch; refactor that replaced the lambda with a null placeholder.","solutions":["Supply a non-null Func<int, TimeSpan>, e.g. attempt => TimeSpan.FromSeconds(Math.Pow(2, attempt)).","Use a named helper (Polly Backoff) or a constant provider instead of null.","Validate the provider is non-null before building the policy."],"exampleFix":"// before\nvar policy = Policy.Handle<HttpRequestException>().WaitAndRetry(3, null, (ex, d, i, ctx) => Log(ex, d, i, ctx));\n// after\nvar policy = Policy.Handle<HttpRequestException>().WaitAndRetry(3,\n    attempt => TimeSpan.FromSeconds(Math.Pow(2, attempt)),\n    (ex, d, i, ctx) => Log(ex, d, i, ctx));","handlingStrategy":"validation","validationCode":"if (sleepDurationProvider is null)\n    throw new InvalidOperationException(\"sleepDurationProvider is required.\");\nvar policy = Policy.Handle<TException>(pred).WaitAndRetry(retryCount, sleepDurationProvider, onRetry);","typeGuard":"static bool IsValid(Func<int, TimeSpan>? provider) => provider is not null;","tryCatchPattern":null,"preventionTips":["Always pass a non-null backoff provider; use a named helper for common strategies.","Enable nullable reference types so null providers are flagged at compile time.","Default provider fields to a sensible exponential backoff."],"tags":["argumentnullexception","polly","retry","sync","validation","csharp","legacy-api"],"backgroundTag":null,"analyzedSha":"d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac","analyzedAt":"2026-08-13T16:36:01.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}