-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Asp.net Framework 4.7.2 graceful shutdown #546
Comments
Thank you for creating an Issue. Please note that GitHub is not an official channel for Microsoft support requests. To create an official support request, please open a ticket here. Microsoft and the GitHub Community strive to provide a best effort in answering questions and supporting Issues on GitHub. |
I am assuming IIS hosted inside a container here. IIS already provides mechanisms to react on the pool lifecycle, please take a look at: This is an example handler:
How does this relate to a containerized application? It depends on the container runtime. If running in docker desktop the shutdown is a total pain. How the engine handles killing will destroy the container with a 5s timeout by deafult. You can take a look at this Windows Server image which has several adjustments to deal with graceful shutdown: https://github.com/david-garcia-garcia/windowscontainers/tree/master/servercore2022 This is the code dealing with changing this shutdown timeout:
When using that image, you can control it through the environment variable:
Even after setting this timeout this does not mean that you are actually waiting for IIS to terminate/finish. You should take care of that in the entrypoint by responding to the signal termination. This is where it gets tricky and the actual implementation depends on what you are using as an entrypoint. Take a look at this entrypoint implementation to see how to handle termination signal from a powershell script: The relevant code is:
You can then loop and wait for the termination signal:
I am not 100% sure if WaitToKillServiceTimeout does the trick and properly waits for IIS to shutdown, if not, you should take care of stopping the service in the entrypoint itself after receiving the termination signal. But all of this is for Docker Desktop... which you are probably not going to use for running your container workloads. In K8S you have lifecycle hooks: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ which is much more reliable than all those sigterm hacks/workarounds. The image in https://github.com/david-garcia-garcia/windowscontainers/tree/master/servercore2022 handles both scenarios (docker and k8s) by coordinating shutdown through this powershell script: The entrypoint uses a flag (a file in written to the filesystem to that purpose) to detect if it needs to call shutdown scripts, of they have been called externally. I hope this servers a starting point. |
Thank you for that very comprehensive post. Yes, we are using kubernetes via Aws EKS. |
@david-garcia-garcia Thanks for that deep dive and all the helpful links! |
Add graceful shutdown documentation (with examples) to our backlog. |
Do you have a sample asp.net application using .net framework 4.7.2 that shows how to gracefully shutdown when kubernetes sends a sigterm, for example when calling kubectrl stop or kubectrl rollout restart?
Examples such as this graceful shutdown are for .net core.
Thanks
The text was updated successfully, but these errors were encountered: