A phone is ringing, and your service has a couple of seconds to look the caller up in the CRM so the agent sees a name instead of a bare number. The CRM's first request hangs. You have a retry policy with three retries and exponential backoff, so you expect the second attempt to save you. It never runs. The call is answered with no context, and the logs show one TaskCanceledException.
I found this pattern in my own public template, dotnet-integration-api-template. It sets http.Timeout = 10s on a typed HttpClient and adds a Polly retry with AddPolicyHandler. The retries work fine for fast failures (a 503, a refused connection). For a request that hangs, the whole 10 seconds goes to attempt one, and the retry loop is cancelled with it.
So I built a small lab to measure exactly what each common wiring does.
The setup
Every scenario uses the same fake upstream: attempt 1 hangs (30 s), and attempt 2 onwards answers 200 immediately. Every scenario has the same budget: 2 s for the whole call, 500 ms per attempt where a per-attempt timeout exists, 3 retries, 200 ms base backoff.
The fake upstream is a primary HttpMessageHandler that logs every attempt, so the tests can count what really went over the "wire":
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken ct) { var n = Interlocked.Increment(ref _attempts); if (n <= HangingAttempts) await Task.Delay(HangFor, ct); // hang until cancelled return new HttpResponseMessage(HttpStatusCode.OK); } These are the four wirings:
// A: the common one. HttpClient.Timeout + retry handler b.ConfigureHttpClient(c => c.Timeout = TimeSpan.FromSeconds(2)); b.AddPolicyHandler(retry); // B: a Polly timeout, but registered BEFORE the retry b.AddPolicyHandler(Policy.TimeoutAsync<HttpResponseMessage>(TimeSpan.FromSeconds(2))); b.AddPolicyHandler(retry); // C: retry first, per-attempt timeout second, HttpClient.Timeout as the total budget b.ConfigureHttpClient(c => c.Timeout = TimeSpan.FromSeconds(2)); b.AddPolicyHandler(retry); // retry also handles TimeoutRejectedException b.AddPolicyHandler(Policy.TimeoutAsync<HttpResponseMessage>(TimeSpan.FromMilliseconds(500))); // D: Microsoft.Extensions.Http.Resilience b.AddStandardResilienceHandler(o => { o.TotalRequestTimeout.Timeout = TimeSpan.FromSeconds(2); o.AttemptTimeout.Timeout = TimeSpan.FromMilliseconds(500); o.Retry.Delay = TimeSpan.FromMilliseconds(200); }); The results
This is one real run on my Linux x64 dev box with .NET 8.0.34. The exact milliseconds will vary on your machine. The attempt counts won't.
| Scenario | Outcome | Attempts | Elapsed |
|---|---|---|---|
A: HttpClient.Timeout + retry | TaskCanceledException | 1 | 2006 ms |
| B: timeout policy outside retry | TimeoutRejectedException | 1 | 2000 ms |
| C: retry outside per-attempt timeout | 200 OK | 2 | 705 ms |
D: AddStandardResilienceHandler | 200 OK | 2 | 742 ms |
All four have the same retry policy, and only two of them ever retry. Here is the per-attempt timeline from the same run:
Why A fails: HttpClient.Timeout wraps the whole pipeline
HttpClient.Timeout is not a socket timeout. HttpClient creates a cancellation token linked to that timeout and passes it into the entire DelegatingHandler chain that IHttpClientFactory built. That chain includes your Polly handler. Retries happen inside that token's lifetime.
When attempt one hangs, nothing fails fast. The token fires at 2 s, the in-flight attempt is cancelled, and the retry handler sees a cancellation rather than a transient failure. The template's retry uses HandleTransientHttpError(), which handles HttpRequestException, 5xx and 408, but not cancellation.
The obvious "fix" is to also handle TaskCanceledException. I added that as a separate test (A2), and it still makes exactly one attempt. The token that HttpClient passed down is already cancelled, so there is nothing left to retry with. The default HttpClient.Timeout is 100 seconds, which is why many apps never notice: they just wait a very long time.
Why B fails: handler order is outermost-first
With IHttpClientFactory, handlers run in the order you register them, and the first one registered is the outermost. Registering the timeout before the retry gives one timeout around all attempts. That makes it a total-budget timeout, not a per-attempt one. Polly's own guidance for HttpClientFactory says the same thing: put the per-try timeout inside the retry (Polly wiki: Polly and HttpClientFactory).
Why C and D work
In C, the retry is the outer handler, and each attempt gets its own 500 ms timeout. Attempt one is cut at ~500 ms with a TimeoutRejectedException, which the retry handles. After a 200 ms backoff, attempt two returns 200 at ~705 ms. HttpClient.Timeout is still there, but now it means what it should: the total budget for the call.
D is the same shape, packaged. According to the Microsoft Learn docs, the standard handler chains rate limiter → total timeout → retry → circuit breaker → attempt timeout, outermost to innermost. The defaults are a 30 s total, a 10 s per attempt, and 3 retries with a 2 s exponential base delay and jitter. The retry handles TimeoutRejectedException by default.
I also tested the worst case, where the upstream hangs on every attempt. Both C and D stop at the 2 s total budget after 2–4 attempts instead of blowing past it.
Gotchas I hit along the way
- Leave
HttpClient.Timeoutabove the pipeline's total timeout. With D, I set it to the total plus one second. If it is lower, it fires first, and you are back in scenario A with a cancellation instead of the pipeline's ownTimeoutRejectedException. - Polly v7 and Polly v8 collide.
Microsoft.Extensions.Http.Polly(v7) andMicrosoft.Extensions.Http.Resilience(Polly.Core v8) both definePolly.Timeout.TimeoutRejectedException. Referencing both in one project givesCS0433. The lab uses anextern aliasso it can compare them side by side. In a real app, pick one stack. - Retries and POST don't mix by default. The standard handler retries all HTTP methods. Retrying a POST that creates a record can create it twice unless the endpoint is idempotent. The docs show
options.Retry.DisableFor(HttpMethod.Post, ...). - Test attempt counts, not just outcomes. A test that only asserts "eventually got an exception" passes for A and B. The useful assertion is
Assert.Equal(2, upstream.Attempts).
The takeaway
A retry policy only retries what it is allowed to see. If your only timeout is HttpClient.Timeout, or a Polly timeout registered before the retry, a hung upstream uses up the whole budget on attempt one, and your retries are decoration.
Give every call two numbers: a per-attempt timeout inside the retry and a total budget outside it. For anything with a human waiting on the other end, such as a ringing phone, a checkout or a chat reply, choose those numbers from how long that human will wait, not from the 100-second default.
My template has the same issue, so updating its wiring is my next change.
Code: github.com/AhmedElbashier/httpclient-timeout-retry-lab has the four scenarios, 7 xUnit tests, the raw run output and the chart script. Run it with dotnet test -c Release.
I'm Ahmed, a full-stack and integrations engineer (.NET, Next.js, VoIP/WebRTC). More of my work is on GitHub and my portfolio.


