Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Asp.Net Core

Asp.Net Core: Fix gRPC Deadlines Ignored by a Retry Policy

- 28.11.26 - ErcanOPAK

⚙️ Fix gRPC Deadlines Ignored by a Retry Policy

A gRPC deadline, once set on a call, is an absolute point in time, not a per-attempt budget – and a Polly-based retry policy wrapped around a gRPC client call doesn’t automatically know that. If the same CallOptions.Deadline value is reused across every retry attempt, each retry still carries the original absolute deadline, so by the second or third attempt the deadline may already be almost spent, and the “retry” fails nearly instantly with DeadlineExceeded regardless of whether the server could have answered a fresh call just fine – the policy ends up spending the single remaining deadline budget faster instead of giving the call more real chances.

🔎 The Problem

var channel = GrpcChannel.ForAddress("https://backend:5001");
var client = new Greeter.GreeterClient(channel);

var policy = Policy
    .Handle<RpcException>(e => e.StatusCode == StatusCode.Unavailable)
    .WaitAndRetryAsync(3, attempt => TimeSpan.FromSeconds(attempt));

var deadline = DateTime.UtcNow.AddSeconds(5); // fixed, absolute deadline

await policy.ExecuteAsync(() =>
    client.SayHelloAsync(request, deadline: deadline).ResponseAsync);

// Attempt 1 fails fast at t=0.2s (Unavailable)
// Policy waits 1s, retries at t=1.2s - deadline (t=5s) still fine
// Policy waits 2s, retries at t=4.3s after a slow attempt 2 -
//   attempt 3 is called with the SAME deadline (t=5s), leaving well
//   under a second of actual budget, so it fails as DeadlineExceeded
//   almost immediately even though the server might have answered a
//   fresh 5-second call just fine

✅ Fix: Give Each Attempt Its Own Deadline

  • Compute a fresh per-attempt deadline inside the retry delegate, evaluated at call time rather than before the policy starts, if the intent is for each attempt to get its own full budget instead of sharing one absolute deadline across every retry.
  • If the intent is genuinely that the whole operation, across all retries, must finish within a fixed window, keep the single absolute deadline but reduce the retry count or backoff delay so there’s realistically still time left for at least one more attempt by the time a retry fires.
  • Log the remaining time until deadline at the start of each retry attempt during development, so a retry policy that’s silently starving itself shows up as a pattern in the logs instead of just looking like the backend is unreliable.

🚨 Why This Is Dangerous

A retry policy that’s supposed to improve reliability can end up making a struggling backend look completely broken instead – every retry attempt fails instantly with DeadlineExceeded, which reads exactly like the server itself is timing out, when the real cause is a client-side deadline that was already spent before the retry even started.

A gRPC deadline is a single countdown for the whole call, retries included – a retry policy that doesn’t know that is just spending the same shrinking budget faster.

— Backend Architect

Related posts:

Asp.Net Core: Fix a Test Factory Hitting the Real Database by Accident

Asp.Net Core: Fix a Test Factory Hitting the Real Database by Accident

ASP.NET Core APIs Hang Under Load

ASP.NET Core APIs Hang Under Load

ASP.NET Core: Fix SignalR Silently Falling Back to Long Polling in Production Because a Reverse Prox...

ASP.NET Core: Fix SignalR Silently Falling Back to Long Polling in Production Because a Reverse Prox...

Views: 10
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

The AI Prompt That Finds Which Test Assertion Is Flaky
Photoshop: Fix a Pattern Stamp Drifting Off Its Clone Source

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,080 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 999 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 998 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 874 views
  5. 05How to select distinct rows in a datatable in C# 869 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 673 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 632 views
  8. 08Average of all values in a column that are not zero in SQL 586 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 563 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com