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

Handle SIGTERMs for graceful shutdown #876

merged 1 commit into from
Nov 2, 2016

Conversation

pakrym
Copy link
Contributor

@pakrym pakrym commented Nov 1, 2016

No description provided.

@pakrym pakrym force-pushed the pakrym/SIGTERM branch 3 times, most recently from 436f735 to c6400a1 Compare November 1, 2016 15:51
@@ -16,18 +19,26 @@ public static class WebHostExtensions
/// <param name="host">The <see cref="IWebHost"/> to run.</param>
public static void Run(this IWebHost host)
{
AutoResetEvent done = new AutoResetEvent(false);
Copy link

Choose a reason for hiding this comment

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

var :D

@@ -30,7 +30,7 @@
"System.Net.Http": ""
}
},
"netstandard1.3": {
"netstandard1.5": {
Copy link
Member

Choose a reason for hiding this comment

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

This is a pretty big deal. Can't this also target both?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it would.


// Don't terminate the process immediately, wait for the Main thread to exit gracefully.
eventArgs.Cancel = true;
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...

@pakrym pakrym force-pushed the pakrym/SIGTERM branch 2 times, most recently from 185c48f to f235a10 Compare November 1, 2016 18:29
@@ -5,6 +5,9 @@
using System.Threading;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.DependencyInjection;
#if NETSTANDARD1_5
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


// Don't terminate the process immediately, wait for the Main thread to exit gracefully.
eventArgs.Cancel = true;
done.WaitOne();
Copy link
Member

Choose a reason for hiding this comment

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

Not running client code after Run is a major regression.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no way we can support this with SIGTERM. So we need to decide if we want to have two behaviors or one common.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds like we need better CLR events for SIGTERM.

@@ -34,6 +34,11 @@
"dependencies": {
"System.Diagnostics.Contracts": "4.3.0-*"
}
},
"netstandard1.5": {
Copy link
Member

Choose a reason for hiding this comment

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

why did TestHost need to change? Shouldn't it have been fine building against the 1.3 version of Hosting?

@pakrym pakrym force-pushed the pakrym/SIGTERM branch 2 times, most recently from 8dfafda to 4390e0e Compare November 1, 2016 23:31
@pakrym
Copy link
Contributor Author

pakrym commented Nov 1, 2016

Fixed comments, brought old behavior for ctrl+c back

};

#if NETSTANDARD1_5
AssemblyLoadContext.Default.Unloading += context => shutdown();
Copy link
Member

Choose a reason for hiding this comment

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

So 80% of the case will be using this in program.Main(), for the 20% case though you might want to use the load context associated with this assembly instance.

var alc = AssemblyLoadContext.GetLoadContext(typeof(WebHostExtensions).GetTypeInfo().Assembly);
alc.Unloading  += context => shutdown();

@@ -16,18 +19,32 @@ public static class WebHostExtensions
/// <param name="host">The <see cref="IWebHost"/> to run.</param>
public static void Run(this IWebHost host)
{
var done = new ManualResetEvent(false);
Copy link
Member

Choose a reason for hiding this comment

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

Slim?

Copy link
Member

@davidfowl davidfowl left a comment

Choose a reason for hiding this comment

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

LGTM. We should add a server test for this (wherever we have those).

@pakrym pakrym changed the title [WIP] Handle SIGTERMs for graceful shutdown Handle SIGTERMs for graceful shutdown Nov 2, 2016
@pakrym pakrym merged commit 2bddba8 into dev Nov 2, 2016
#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.

@JunTaoLuo JunTaoLuo deleted the pakrym/SIGTERM branch May 19, 2017 17:55
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants