I implement Graceful shutdown on ASP.NET Core 2.1. I noticed that when you call SIGINT or Ctrl + C in the console, all requests that are currently being processed are interrupted. Accordingly, if the Controller, through its Actions, performs any queries in the DB without transactions, the system may be in a non-consistent state.

From the IApplicationLifetime Description https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.iapplicationlifetime?view=aspnetcore-2.1

Consider the ApplicationStopping event.

ApplicationStopping Triggered when the application host is performing a graceful shutdown. Requests may still be in flight. Shutdown will block until this event completes.

I expected that the server will wait until the completion of the processing of current requests, prohibiting new arrivals. In reality, it is cut down despite existing requests.

How to wait for the completion of current requests?

Example:

public static void Main(string[] args) { WebHostExtensions.Run(BuildWebHost(args)); } private static IWebHost BuildWebHost(string[] args) { return WebHostExtensions.CreateDefaultBuilder(args) .UseSerilog() .UseStartup<Startup>() .UseUrls("http://0.0.0.0:9100") .Build(); } 

Controller:

 public class UserOrdersController : Controller { public async Task<IActionResult> GetUserOrders(string productId) { await Task.Delay(5000); Console.WriteLine("Finished"); //..................................... } } string productId) public class UserOrdersController : Controller { public async Task<IActionResult> GetUserOrders(string productId) { await Task.Delay(5000); Console.WriteLine("Finished"); //..................................... } } 

"Finished" will be displayed, if you send a signal of completion.

  • Yes like I should wait (but not longer than ShutdownTimeout). You can bring the minimum reproducible example ? - Pavel Mayorov
  • @PavelMayorov example added - the Destroy
  • It seems 5000 - too many for the default timeout - Pavel Mayorov
  • "Some connections failed to close gracefully during server shutdown" - this error in the console. I could not find where this ShutdownTimeout configured in the core 2.1 .net - the Destroy
  • @PavelMayorov Added .UseShutdownTimeout(TimeSpan.FromSeconds(10)) in the builder, but it did not help. - the Destroy

0