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

Handle SIGTERMs for graceful shutdown #876

Merged
merged 1 commit into from
Nov 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sort

#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 = () =>
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How big of a change is this from the default behavior? Any observable differences?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is something in Program.Main after host.Run it would not get run. But at least it would be consistent between SIGTERM and ctrl+c

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a big one... We need to think about that...

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();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior of shutdown() is now synchronous vs. asynchronous. So long as CancelKeyPress comes in on a random background thread, this shouldn't matter. Just pointing out there may be a behavioral difference from the existing code.

// 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