Add try..finally around RunAsync and WaitForTokenShutdownAsync #1194

The try..finally ensures that the ManualResetEventSlim which AttachCtrlcSigtermShutdown uses is set even when an exception occurs in these two methods.
This commit is contained in:
Rutger Storm 2018-05-25 18:21:26 +02:00 committed by Chris Ross
parent 09d3b32fe5
commit a3300a6a60
1 changed files with 16 additions and 5 deletions

View File

@ -45,10 +45,16 @@ namespace Microsoft.AspNetCore.Hosting
{ {
AttachCtrlcSigtermShutdown(cts, done, shutdownMessage: string.Empty); AttachCtrlcSigtermShutdown(cts, done, shutdownMessage: string.Empty);
try
{
await host.WaitForTokenShutdownAsync(cts.Token); await host.WaitForTokenShutdownAsync(cts.Token);
}
finally
{
done.Set(); done.Set();
} }
} }
}
/// <summary> /// <summary>
/// Runs a web application and block the calling thread until host shutdown. /// Runs a web application and block the calling thread until host shutdown.
@ -80,10 +86,16 @@ namespace Microsoft.AspNetCore.Hosting
var shutdownMessage = host.Services.GetRequiredService<WebHostOptions>().SuppressStatusMessages ? string.Empty : "Application is shutting down..."; var shutdownMessage = host.Services.GetRequiredService<WebHostOptions>().SuppressStatusMessages ? string.Empty : "Application is shutting down...";
AttachCtrlcSigtermShutdown(cts, done, shutdownMessage: shutdownMessage); AttachCtrlcSigtermShutdown(cts, done, shutdownMessage: shutdownMessage);
try
{
await host.RunAsync(cts.Token, "Application started. Press Ctrl+C to shut down."); await host.RunAsync(cts.Token, "Application started. Press Ctrl+C to shut down.");
}
finally
{
done.Set(); done.Set();
} }
} }
}
private static async Task RunAsync(this IWebHost host, CancellationToken token, string shutdownMessage) private static async Task RunAsync(this IWebHost host, CancellationToken token, string shutdownMessage)
{ {
@ -92,7 +104,6 @@ namespace Microsoft.AspNetCore.Hosting
await host.StartAsync(token); await host.StartAsync(token);
var hostingEnvironment = host.Services.GetService<IHostingEnvironment>(); var hostingEnvironment = host.Services.GetService<IHostingEnvironment>();
var applicationLifetime = host.Services.GetService<IApplicationLifetime>();
var options = host.Services.GetRequiredService<WebHostOptions>(); var options = host.Services.GetRequiredService<WebHostOptions>();
if (!options.SuppressStatusMessages) if (!options.SuppressStatusMessages)