-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
CoreCLR should handle SIGTERM as equivalent to Environment.Exit() #4950
Comments
On Windows, a graceful shutdown request is a request, generally provided by the user hitting ALT-F4 or clicking the red X. In many cases it is inappropriate to call a shutdown routine at this point; rather you need to confirm with the user and give them the chance to save and/or cancel shutdown. (There's nothing graceful about having the application pulled out from under you; from the end-user's perspective that's a crash, not a graceful shutdown.) We should do our best to maintain consistency across platforms. SIGTERM should be treated as a polite request to shut down, to be handled in an application-defined manner, not as a kill switch that automatically calls |
Right now SIGTERM isn't handled at all, so it's a kill switch that's even less graceful than calling Exit, running any Unloading event handlers, etc. |
Fair enough. I'm just saying that if we want to handle this, it's important to get it right, no? |
My thinking is this:
I don't see us designing, implementing, and shipping that last bullet for an initial release. But since it can be done purely additively on top of the second bullet, I think we should do the second bullet, as in my mind it's significantly better than the first and for relatively little work. |
Done with dotnet/coreclr#4309 |
Thanks. |
@adityamandaleeka, @stephentoub might not be relevant here but how should I listen on |
A few other places like microsoft/azure-pipelines-agent#215 suggests that |
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. |
With AssemblyLoadContext.Default.Unloading literally you are enabled to place a shutdown hook, so imo it is a question in what state the runtime is in that point. A related question which I not found here: When I call IApplicationLifeTime.StoppApplication in other place than the Unloading event (e.g. ina controller handler), after the unloading executes, the Kestler will throw an ObjectDisposedException and the ServiceProvider I've created won't call any Dispose on any singleton. there is a related stackoverflow question, but as I see StopApplication suffers from the same issue than the original requestor's problem. |
Finalizers don't run on shutdown in .NET Core, so if the finalizer calls If you have unmanaged resources that need to be cleaned up on graceful shutdown, the |
@kouvel All the posts I read on SO and GitHub seem to contradict each other. This post says use AssemblyLoadContext.Default.Unloading, then to use AppDomain.CurrentDomain.ProcessExit But the official documentation is vague and ambiguous. In the former it says one has 2 seconds to do the work, but here it says in Kubernetes one has 30 seconds. How do we do this bit: ‘The system [Kubernetes] waits terminationGracePeriodSeconds (default 30s), or until the Pod completes on it's own.’? Please could we have some official advice? |
According to this,
The events above are equivalent. The 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 Any exception thrown by an event handler will crash the process.
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
Yea there was a time when |
But will also raise the Console.CancelKeyPress event, which can override that termination. |
I was just editing to add that above :) |
@kouvel @stephentoub Perfect answer for me, thanks. |
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.aspnet/Hosting#516
The text was updated successfully, but these errors were encountered: