Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
Handle SIGTERMs for graceful shutdown (#876)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym authored Nov 2, 2016
1 parent a29ceeb commit 2bddba8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private RequestDelegate BuildApplication()
var model = new ErrorPageModel();
var runtimeType = Microsoft.Extensions.Internal.RuntimeEnvironment.RuntimeType;
model.RuntimeDisplayName = (runtimeType == "CoreCLR") ? ".NET Core" : runtimeType == "CLR" ? ".NET Framework" : "Mono";
#if NETSTANDARD1_3
#if NETSTANDARD1_3 || NETSTANDARD1_5
var systemRuntimeAssembly = typeof(System.ComponentModel.DefaultValueAttribute).GetTypeInfo().Assembly;
var assemblyVersion = new AssemblyName(systemRuntimeAssembly.FullName).Version.ToString();
var clrVersion = assemblyVersion;
Expand Down
25 changes: 22 additions & 3 deletions src/Microsoft.AspNetCore.Hosting/WebHostExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
#if NETSTANDARD1_5
using System.Reflection;
using System.Runtime.Loader;
#endif
using System.Threading;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -16,18 +20,33 @@ public static class WebHostExtensions
/// <param name="host">The <see cref="IWebHost"/> to run.</param>
public static void Run(this IWebHost host)
{
var done = new ManualResetEventSlim(false);
using (var cts = new CancellationTokenSource())
{
Console.CancelKeyPress += (sender, eventArgs) =>
Action shutdown = () =>
{
Console.WriteLine("Application is shutting down...");
cts.Cancel();
if (!cts.IsCancellationRequested)
{
Console.WriteLine("Application is shutting down...");
cts.Cancel();
}

done.Wait();
};

#if NETSTANDARD1_5
var assemblyLoadContext = AssemblyLoadContext.GetLoadContext(typeof(WebHostExtensions).GetTypeInfo().Assembly);
assemblyLoadContext.Unloading += context => shutdown();
#endif
Console.CancelKeyPress += (sender, eventArgs) =>
{
shutdown();
// Don't terminate the process immediately, wait for the Main thread to exit gracefully.
eventArgs.Cancel = true;
};

host.Run(cts.Token, "Application started. Press Ctrl+C to shut down.");
done.Set();
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/Microsoft.AspNetCore.Hosting/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
"dependencies": {
"System.Diagnostics.StackTrace": "4.3.0-*"
}
},
"netstandard1.5": {
"dependencies": {
"System.Diagnostics.StackTrace": "4.3.0-*",
"System.Runtime.Loader": "4.3.0-*"
}
}
},
"tools": {
Expand Down

0 comments on commit 2bddba8

Please sign in to comment.