Repository navigation
ConsoleLifetime doesn't allow full graceful shutdown for ProcessExit #35990
Description
Describe the bug
When ConsoleLifetime shuts down via the AppDomain.ProcessExit path, it skips a number of steps done in normal graceful program termination.
Related to dotnet/extensions#1363, but perhaps the opposite problem (in this case, it shuts down too soon rather than hanging/too late).
To Reproduce
Program.cs:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.Threading;
class Program
{
static int Main()
{
var builder = new HostBuilder()
.ConfigureLogging(l =>
{
l.AddConsole();
l.Services.AddSingleton<ILoggerProvider, CustomLoggerProvider>();
}).ConfigureServices(s =>
{
s.AddSingleton<OtherService>();
});
using (IHost host = builder.Build())
{
host.Services.GetRequiredService<OtherService>();
host.Start();
// Edit: ignore Environment.Exit; instead use CTRL+C or docker stop here, per comments below.
//Thread thread = new Thread(() => Environment.Exit(456));
//thread.Start();
host.WaitForShutdown();
Console.WriteLine("Ran cleanup code inside using host block.");
}
Console.WriteLine("Ran cleanup code outside using host block.");
Console.WriteLine("Returning exit code from Program.Main.");
return 123;
}
class CustomLoggerProvider : ILoggerProvider
{
public ILogger CreateLogger(string categoryName)
{
return NullLogger.Instance;
}
public void Dispose()
{
Console.WriteLine("Ran logger cleanup code.");
}
}
class OtherService : IDisposable
{
public void Dispose()
{
Console.WriteLine("Ran most service's cleanup code.");
}
}
}App.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>false</SelfContained>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
</ItemGroup>
</Project>- Run the code above as-is, and terminate with Ctrl+C. See all the cleanup code that runs.
- Uncomment the two Thread lines above to let the program self-terminate via Process.Exit.
Expected behavior
The same cleanup code runs when exiting via ProcessExit as it did with Ctrl+C:
Ran cleanup code inside using host block.
Ran most service's cleanup code.
Ran logger cleanup code.
Ran cleanup code outside using host block.
Returning exit code from Program.Main.
X:\path\to\App.exe (process nnn) exited with code 123.
Actual behavior
Only the following cleanup code runs:
Ran cleanup code inside using host block.
Ran most service's cleanup code.
X:\path\to\App.exe (process nnn) exited with code 456.
Note that the return code for a normal ProcessExit case (rather than one simulated by Environment.Exit) would likely be 3221225786 (STATUS_CONTROL_C_EXIT) on Windows for v2.2.0 of this package and 0 for the latest (I think; based on ConsoleLifetime source).
Additional context
Trapping ProcessExit and then deciding how long to wait overall is rather problematic; it could wait too long/block, as in dotnet/extensions#1363, or too short, as here. The main options I can think of are:
- Only have ProcessExit trigger the start of graceful shutdown (like Ctrl+C does), and give something else the responsibility to keep the shutdown from completing until Program.Main finishes.
- Have some automatic way to know when Program.Main returns. Not sure if this is possible - I tried doing a thread.Join on the main thread, but that doesn't quite work. Polling it for when IsBackground flips to false looks like it could work, but that seems not exactly ideal.
- Push the control up to the user for when to have the shutdown handler terminate (have the user pass in an event signaled from Program.Main maybe or something roughly like that; more complicated API to make that work).
Overall, the host itself doesn't own the entirety of Program.Main, so having it decide when to stop running is tricky.
I did find that at least the logger cleanup can likely be done better by changing the order of the disposables in the service container, including by adding a first constructor parameter to Host that gets registered first - currently, the IHostApplicationLifetime is after loggers in the list of things to dispose, and the logic to dispose in reverse order on the service provider/scope means loggers don't get to cleanup.
For application insights, for example, having the logger not get to have Dispose called means it can't call Flush, so logs currently get lost on shutdown.
The full list of things that currently get lost are:
- Earlier registered service provider disposables, including logger providers (likely could be fixed with some registration order/constructor chaining/dependency order changes).
- Code after the using host block.
- Program.Main's exit code.
Activity
davidmatson commentedon Jul 15, 2019
A couple other things that likely currently get lost: full disposing of custom service providers, full disposing of custom hosts.
I ended up working around this with a wrapper around the entry point for now. Here's what it looks like:
When run with the original set of things hooked up above, all of the cleanup code runs (including code outside the using block and setting the exit code).
The IProcessLifetime interface looks like this:
If you're interested in going down this route further, let me know and I can provide the other details as well (UseProcessLifetime extension method and ProcessLifetimeHost.Run method).
analogrelay commentedon Jul 24, 2019
We'll likely need some corefx support on this one to give us an API to properly handle SIGTERM.
The rough proposal we have right now is to have a new API in corefx to hook the
SIGTERMdirectly (right now we hookProcessExitwhich can happen gracefully or viaSIGTERM). In the new API, there would be aCancelargument that can be used to stop the default behavior of the CLR (to shut down). In our lifetime we can hook that new event, stop the termination and signal theIApplicationLifetimeto stop.It means that if the app doesn't gracefully shutdown,
SIGTERMwon't shut it down, butSIGTERMis intended to be "graceful" so that makes sense. If a user needs to forcibly terminate the app, they can useSIGKILLdavidmatson commentedon Jul 24, 2019
Unfortunately, I don't think it's possible for corefx to provide a cancellable API on Windows - the underlying signal sent by docker stop on Windows containers changed to CTRL_SHUTDOWN_EVENT, which can't be canceled. The MSDN docs could be a little clearer - they more say what can be done in this case (inline cleanup in the handler) rather than what can't be done (canceling the signal). See also a related moby issue for details.
analogrelay commentedon Jul 24, 2019
Good to know. I was referring to graceful termination on Linux which sends
SIGTERMbut it may be more challenging on Windows.This will be a tricky one, we'll have to investigate some options.
MikhaelSorkin commentedon Nov 14, 2019
Hello!
Any update here? Is it possible to achieve graceful shutdown in windows container?
davidmatson commentedon Nov 14, 2019
It is possible to achieve graceful shutdown in a Windows container, but it currently requires a bit of custom code to make it work. Specifically, it requires using P/Invoke to get a shutdown notification (see dotnet/extensions#2029 and moby/moby#25982). And then this signal cannot be canceled, so it requires different wiring to make it work with IHostBuilder/Host. The ProcessLifetimeHost snippet above shows how I'm currently doing that from a design standpoint (the implementation is rather complicated).
So yes it's possible, but it requires custom code and is not easy.
MikhaelSorkin commentedon Nov 14, 2019
@davidmatson
Thank you for answer. Do you plan to publish your implementation? As a nuget package or as a source code?
davidmatson commentedon Nov 14, 2019
My hope with this bug was to get a solution into the official product, so I wasn't planning on publishing my implementation. I'd have to check, but I could probably paste it here as a temporary workaround if there's interest.
MikhaelSorkin commentedon Nov 15, 2019
@davidmatson
Well, i'm very interested in this workaround :)
davidmatson commentedon Nov 15, 2019
Here's a zip of the source I'm using as a workaround. The usage is as described above. I'm using it with v2.2.0 of the hosting extensions package (it may work with a later version but I haven't tested that).
ProcessLifetimeWorkaround.zip
Disclaimer: as-is; just a workaround; not an official solution; not offering support for this code; etc.
Tragetaschen commentedon Dec 17, 2019
This has cost me the better part of the day :-(
Whenever I ran my application on the command line on Linux, CTRL+C would gracefully exit and dispose my Autofac container. When running as systemd service, that would just start the disposing work, but then stop in the middle of it. Since that service would normally only be stopped when my embedded instrument with a volatile system log would be shut down, I had to unwrap a lot of layers before this became clear.
For the time being as a workaround, I have internalized the code for
UseSystemd, replaced theProcessExitwithConsole.CancelKeyPressand addedKillSignal=SIGINTto my systemd unit.Is there a planned path forward already? I think this should be more than "Backlog"…
analogrelay commentedon Apr 24, 2020
Triage: The right approach here is twofold:
Get an event that runs before ProcessExit to capture
SIGTERM(either by a full signal handler API or aSIGTERM) and gives the option to cancel the default behavior (terminating the process)Change ConsoleLifetime to hook that event and use it to signal a graceful shutdown.
analogrelay commentedon Apr 24, 2020
The above approach would also fix dotnet/extensions#1363
davidmatson commentedon Apr 24, 2020
@anurse - the equivalent of SIGTERM on Windows/Docker (CTRL_SHUTDOWN_EVENT) can't be canceled. Thoughts?
tmds commentedon Apr 25, 2020
Sharing my view of dotnet/extensions#1363 (comment).
ConsoleLifetime shouldn't be used by default.
Though there should be an API to Run a Host from Main that includes it.
That avoids the issue because the Host no longer assumes it gets disposed on ProcessExit.
And, not all hosts should be tied to these events, like a host that runs shortly as part of a larger application.
18 remaining items
jkotas commentedon Apr 22, 2021
Not exactly.
Environment.Exitis low-level API that is not designed to be overridable.Ctrl-C and similar signals exit the process by default, but that behavior can be overriden. #50527 is about making the APIs for these signals better.
This issue looks like duplicate of #50527 and can be closed. @davidfowl Do you agree?
Unhandled exception is abnormal exit. It does not follow the same path as
Environment.Exitdavidmatson commentedon Apr 22, 2021
Yeah, I'm not all that concerned with Environment.Exit per se as long as all the other cases (Ctrl+C, docker stop, system shutdown notification, and unhandled exceptions from other threads) are covered.
I'm guessing the other issue was filed with this kind of scenario in mind; as long as we're covering the same cases there, closing this one in favor of that one sounds fine to me.
Handle SIGTERM in Hosting and handle just like SIGINT (CTRL+C)
Consume PosixSignal in Hosting's ConsoleLifetime (#56057)
davidmatson commentedon Jul 22, 2021
@eerhardt - to clarify the status here: with the merged fix, does the repro at the top now work as in the Expected behavior section above (run all the cleanup steps listed and return an exit code of 123 from Main)?
Thanks
davidmatson commentedon Jul 22, 2021
Also, does it work that way specifically on Windows?
eerhardt commentedon Jul 22, 2021
No. That is unreasonable expected behavior. See https://docs.microsoft.com/en-us/dotnet/api/system.environment.exit?view=net-5.0#remarks for what to expect with
Environment.Exit. Specifically:When calling
Environment.Exit, your "Main" method doesn't complete. Also see @jkotas's comment above.With the merged fix, Hosting no longer subscribes to
ProcessExit. It was only handlingProcessExitin previous versions of .NET because it wanted to handle SIGTERM (fordocker stop). In .NET 6, we can now specifically handle SIGTERM without usingProcessExit. If your app callsEnvironment.Exitand it requires specific clean up to happen, it is your apps responsibility for coordinating the shutdown. Hosting doesn't try to gracefully shutdown the process whenEnvironment.Exitis called, becauseEnvironment.Exitisn't really a "graceful" way of shutting down a process. Your Main method doesn't complete, running threads are terminated,finallyblocks aren't executed, etc.In short,
Environment.Exit!=CTRL+Cordocker stop. If your app is using Hosting, and you want to gracefully stop the host, you should be callingIHostApplicationLifetime.StopApplication().See https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.Hosting/docs/HostShutdown.md for more information.
davidmatson commentedon Jul 22, 2021
@eerhardt - sorry, I should clarify ignoring Environment.Exit to trigger process stop as in jkota's comment above. If, at that point in the code, CTRL+C or docker stop happens, will it now behave as in the Expected behavior section, even on Windows?
Thanks
davidmatson commentedon Jul 22, 2021
I'm particularly interested in the docker stop case on Windows - at least last time I looked, docker stop used system shutdown, which is a non-cancellable event.
eerhardt commentedon Jul 22, 2021
Yes. When
CTRL+Cordocker stopis issued to the process, the Hosted process will now gracefully exit and return123. Yes, even on Windows and in Windows containers.Above I ran
docker stop 93c982cee9f8from a separate command prompt afterdocker run.davidmatson commentedon Jul 22, 2021
Excellent. Thanks, @eerhardt!