In some situations my web application requires to immediately quit during start-up. When doing so via System.Environment.Exit(int exitCode), the dotnet process does not exit.
Minimal set-up to reproduce:
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseStartup<Startup>()
.Build();
host.Run();
}
public override void Configure(IApplicationBuilder app)
{
System.Environment.Exit(1);
}
It seems that in this scenario Set() will no longer be called on the instance of ManualResetEventSlim passed to AttachCtrlcSigtermShutdown(..) resulting in the Shutdown() method to block indefinitely.
https://github.com/aspnet/Hosting/blob/97c9510a95e676324ed8fa03d8b6dcf3b1c19ccf/src/Microsoft.AspNetCore.Hosting/WebHostExtensions.cs#L134-L162
One possible solution could be applying the WebHostOptions.ShutdownTimeout when waiting for the manual reset event.
System Info:
- dotnet core: 2.1
- Microsoft.AspNetCore.Hosting: 2.1.1