dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.5k stars 10.04k forks source link

Application shutdown on Ctrl-C (SIGTERM) #5940

Closed RoySalisbury closed 5 years ago

RoySalisbury commented 6 years ago

I have 2 hosted services running within my ASPNET Core application.

services.AddSingleton<IHostedService, ServiceHost1>();
services.AddSingleton<IHostedService, ServiceHost2>();

Prior to 2.1.4 (2.1.402) a CTRL-C would cause the cancellationToken in each of the hostedservices to be set so that I could exist the tasks and the process would stop. However, that is no longer the case.

The CancellationToken does not get set and the hosted services DO NOT stop. Which will not let the ASPNET main task stop either. I get the message in the console "Application is shutting down", but nothing happens.

This is running on the latest Raspbian image on a Pi 3 B+.

Anyone else having this issue?

muratg commented 6 years ago

@RoySalisbury Does the app this issue on other platforms? Like a non pi linux?

Could you share an app that reproduces this?

RoySalisbury commented 6 years ago

I can try to create a small test case for you, but it appears it is not a framework/platform issue as much as a local app issue.

Basically, if one of the IHostedService apps does not start up correctly, then the main ASP.NET host does not complete its startup fully. The web site runs, but the Ctrl-C handler of the main host does not get hooked up.

Once I corrected the startup of the errant IHostedService, and the main ASP.NET host was able to fully start, everything worked.

So, bug? Sorta .. but if your code is correct, it does not happen. If you want to look into it as a edge case I can try to get something to you, but for me, its no longer an issue.

muratg commented 6 years ago

@RoySalisbury OK thanks for the explanation. If more folks start hitting this, we'll consider investigating this further, but for now I think it's fine, especially since you're unblocked.

Tratcher commented 6 years ago

Note the IHostedService exception handling behavior is different for the new generic host. I.e. there isn't any, if your IHostedService.StartAsync throws then you'll see that exception surface in Main.

Markeli commented 5 years ago

We reproduced this issue. When we run app from console, it stopping correctly. But when app is running as daemon via systemd it still running after command sudo systemctl stop %my_app_name%. As we investigated, default kill signal is SIGTERM. If we change it manual in config, all works fine (example of config listed below):

[Unit]
Description=AMI Proxy service (dotnet)

[Service]
ExecStart=/usr/bin/dotnet /opt/amiproxy/SIISLtd.DEX.AmiProxy.dll
WorkingDirectory=/opt/amiproxy/
User=maxim
Restart=on-failure
SyslogIdentifier=amiproxy
PrivateTmp=true
KillSignal=SIGTERM

[Install]
WantedBy=multi-user.target                           

Sample app All information about our environment: dotnet --info

.NET Core SDK (reflecting any global.json):
 Version:   2.1.403
 Commit:    04e15494b6

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  18.04
 OS Platform: Linux
 RID:         ubuntu.18.04-x64
 Base Path:   /usr/share/dotnet/sdk/2.1.403/

Host (useful for support):
  Version: 2.1.5
  Commit:  290303f510

.NET Core SDKs installed:
  2.1.403 [/usr/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.5 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.5 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.5 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

OS:

Distributor ID: Ubuntu
Description:    Ubuntu 18.04.1 LTS
Release:    18.04
voroninp commented 5 years ago

This example from docs is not stopped by CTRL+C:

public static async Task Main(string[] args)
{
    var host = new HostBuilder()
        .Build();

    await host.RunAsync();
}
analogrelay commented 5 years ago

@Tratcher is the SIGTERM issue @Markeli posted resolved by https://github.com/aspnet/AspNetCore/pull/8294 ?

@voroninp I can't reproduce that issue, it terminated just fine for me. Could you describe the specific environment you're running in? For example: What OS are you running? Are you running in systemd? Etc.

Tratcher commented 5 years ago

Unclear, I hadn't reproduced it yet either. #8294 should have only changed the exit code, not affected if it did or did not exit.

analogrelay commented 5 years ago

Ok, so we do still need to investigate if SIGTERM is working in this scenario then.

Tratcher commented 5 years ago

Tested this sample (https://github.com/aspnet/AspNetCore/issues/5940#issuecomment-449364978) on 2.2.4 (2.2.203) on Ubuntu 18.04 and CTL+C is working on the console as expected.

Backlogging any investigation of daemons until we focus on those in a future milestone.

analogrelay commented 5 years ago

Ok, closing this for now then. @voroninp if you have more information about the environment you are in where Ctrl-C is not working, please let us know!