How to gracefully shutdown server
See original GitHub issueHow to gracefully close stream during host shutdown? i am always getting " The operation was canceled." exception on CTRL+C (Asp.NET core 3.1 gRPC template). `
try
{
await foreach (var response in requestStream.ReadAllAsync(context.CancellationToken))
{
_ = Task.Run(() =>
{
ProcessResponse(response);
});
}
}
catch (Exception) when (context.CancellationToken.IsCancellationRequested)
{
_logger.LogInformation("Server shutdown");
//Graceful shutdown
}
`
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Gracefully Shutdown a Server - Hyper
hyper Server s have the ability to “gracefully” shutdown. This means stopping to accept new requests, and shutting down once all in-progress requests...
Read more >Graceful shutdown in Go http server | by Sam Wang
Shutdown works by first closing all open listeners, then closing all idle connections, and then waiting indefinitely for connections to return ...
Read more >Gracefully Shut Down Go Web Servers | by Halil Yıldırım
If your web server is waiting your active requests to be handled before shutting down the server, it means you are gracefully shutting...
Read more >How to gracefully shutdown your Linux system
The shutdown command will trigger the proper Init level resulting in the OS gracefully stopping all running services. Any stuck processes will be...
Read more >Shutdown server gracefully
Under Manage > Remote Task, you can create a power task selecting any of "Reboot, Power Cycle & Power Off" with "Shutdown OS...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
When you Ctrl+C Kestrel will attempt to gracefully shutdown. That means it will wait some time for requests that are in-progress to end. That is why you see a delay.
This is good. The server will end reading from the stream when
ApplicationStopping
is triggered. The call then ends on the server and Kestrel stops quickly.I don’t think you need
ApplicationStopped
:Thanks @wicharypawel Yes its client streaming scenario. When I press Ctrl+C on server gRPC server console, its taking more time to stop server since cancellationToken is not triggered from client. I modified code as follows to close stream immediately (Using IHostApplicationLifeTime / IHostLifetime)
`
`