{"record":{"id":"9e25fdd356279396","repo":"App-vNext/Polly","slug":"value-must-be-greater-than-or-equal-to-zero-9e25fd","errorCode":null,"errorMessage":"Value must be greater than or equal to zero.","messagePattern":"Value must be greater than or equal to zero\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Polly/Retry/AsyncRetrySyntax.cs","lineNumber":151,"sourceCode":"            onRetryAsync: async (outcome, i, ctx) => onRetry(outcome, i, ctx));\n#pragma warning restore 1998\n    }\n\n    /// <summary>\n    /// Builds an <see cref=\"AsyncRetryPolicy\"/> that will retry <paramref name=\"retryCount\"/> times\n    /// calling <paramref name=\"onRetryAsync\"/> on each retry with the raised exception, retry count and context data.\n    /// </summary>\n    /// <param name=\"policyBuilder\">The policy builder.</param>\n    /// <param name=\"retryCount\">The retry count.</param>\n    /// <param name=\"onRetryAsync\">The action to call asynchronously on each retry.</param>\n    /// <returns>The policy instance.</returns>\n    /// <exception cref=\"ArgumentOutOfRangeException\">retryCount;Value must be greater than zero.</exception>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"onRetryAsync\"/> is <see langword=\"null\"/>.</exception>\n    public static AsyncRetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Func<Exception, int, Context, Task> onRetryAsync)\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 (onRetryAsync == null)\n        {\n            throw new ArgumentNullException(nameof(onRetryAsync));\n        }\n\n        return new AsyncRetryPolicy(\n            policyBuilder,\n            (outcome, _, i, ctx) => onRetryAsync(outcome, i, ctx),\n            retryCount);\n    }\n\n    /// <summary>\n    ///     Builds an <see cref=\"AsyncRetryPolicy\" /> that will retry indefinitely until the action succeeds.\n    /// </summary>\n    /// <param name=\"policyBuilder\">The policy builder.</param>\n    /// <returns>The policy instance.</returns>","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/App-vNext/Polly/blob/d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac/src/Polly/Retry/AsyncRetrySyntax.cs#L133-L169","documentation":"Thrown by AsyncRetrySyntax.RetryAsync(this PolicyBuilder, retryCount, Func<Exception,int,Context,Task> onRetryAsync) when retryCount is less than zero. A negative retry count is nonsensical; zero means no retries (fail fast) and is the valid floor.","triggerScenarios":"Calling .RetryAsync(retryCount: -1, ...) on the context-aware async-callback overload; any negative integer for retryCount.","commonSituations":"Config-driven retry count defaulting to -1 (sentinel for \"unset\"); arithmetic that subtracts from retry count; env var parsed as a negative number.","solutions":["Set retryCount to 0 or a positive integer (0 = no retries).","Clamp the config value with Math.Max(0, configuredValue) at load time.","Use a distinct sentinel and map it to a default retry count rather than passing it through."],"exampleFix":"// before\nvar policy = Policy.Handle<HttpRequestException>().RetryAsync(-1, onRetryAsync);\n// after\nvar policy = Policy.Handle<HttpRequestException>().RetryAsync(3, onRetryAsync);","handlingStrategy":"validation","validationCode":"var retryCount = Math.Max(0, config.GetValue(\"Retry:Count\", 3));\nvar policy = builder.RetryAsync(retryCount, onRetryAsync);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp config-driven retry counts with Math.Max(0, value) at load.","Use 0 to mean fail-fast (no retries) rather than a negative sentinel.","Validate retry config via options-pattern Validate() to fail at startup."],"tags":["retry","argumentoutofrange","configuration","validation","legacy"],"backgroundTag":null,"analyzedSha":"d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac","analyzedAt":"2026-08-13T16:36:01.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}