.NET Core / Kubernetes - SIGTERM, clean shutdown
SIGTERM
does indeed signal termination. The less obvious part is that when the SIGTERM
handler returns, everything is considered finished.
The fix is to not return from the SIGTERM
handler until the app has finished shutting down. For example, using a ManualResetEvent
and Wait()
ing it in the handler.
I've started to look into this for my own purposes and have come across your question over a year after it was posted... This is a bit late, but have you tried GraceTerm?There is an associated NuGET package for this.
From the description...
Graceterm middleware provides implementation to ensure graceful shutdown of AspNet Core applications. The basic concept is: After application received a SIGTERM (a signal asking it to terminate), Graceterm will hold it alive till all pending requests are completed or a timeout occur.
I haven't personally tried this yet, but it does look promising.