runtime: CoreCLR should handle SIGTERM as equivalent to Environment.Exit()
SIGTERM is meant to be used for graceful shutdown of an app. The runtime should handle it by doing the equivalent of Environment.Exit(). Then with https://github.com/dotnet/corefx/issues/5205, apps will also be able to plug in their own cleanup that’ll be triggered on any graceful shutdown, including those triggered by SIGTERM.
As an example, docker stop
will initially send a SIGTERM to give the app a chance to gracefully shutdown before it eventually then sends SIGKILL if the process is still alive.
https://github.com/aspnet/Hosting/issues/516
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 4
- Comments: 17 (10 by maintainers)
I am wondering the same thing, @tugberkugurlu. There was a lot of discussion on the implementation details but I can’t find any guidance on how I should be implementing graceful shutdown in a .NET Core console application so it behaves the same on any OS.
@adityamandaleeka, @stephentoub might not be relevant here but how should I listen on
SIGTERM
on a .NET Core console application? My app level entry point acceptsCancellationToken
and I am planning to trigger a cancellation through aCancellationTokenSource
when I receiveSIGTERM
in my staticMain
method.According to this,
SIGKILL
can’t be caught or ignored.SIGTERM
raises theAssemblyLoadContext.Default.Unloading
andAppDomain.CurrentDomain.ProcessExit
events. The equivalent on Windows isCTRL_CLOSE_EVENT
.The events above are equivalent. The
Unloading
event was added whenAppDomain
was initially not going to be exposed in the .NET Core API. That has since changed, so now both events exist.At the point when these events are raised, nothing significant has been torn down yet, so the event handlers should be able to do most things. Those events are currently raised on the finalizer thread, so finalizers can’t be relied upon, even with
GC.WaitForPendingFinalizers()
. Probably would be good to raise those events on a different thread in the future.Any exception thrown by an event handler will crash the process.
SIGINT
andSIGQUIT
are currently treated as abrupt termination and do not raise the events above. However, aConsole.CancelKeyPress
handler can prevent termination.Thanks for pointing that out, I’ll file an issue.
That is for .NET Framework, the docs need to be updated, I’ll file an issue. .NET Core currently does not time-out the event handlers above, though that may change in the future.
Kubernetes may have its own timeout but that’s separate and not in .NET Core’s control. The parent of the .NET Core process may for instance issue a
SIGTERM
and if the process does not exit within a timeout the parent may issue aSIGKILL
. A parent process may do something like that to ensure that a child process is torn down in a timely manner. It may be possible to configure the timeout, you’d have to consult the container’s docs.Yea there was a time when
SIGTERM
was not handled. There was some other bug related to handling theCancelKeyPress
event, I don’t remember the details. You can ignore these.I was just editing to add that above 😃