-
Notifications
You must be signed in to change notification settings - Fork 307
Handle SIGTERMs for graceful shutdown #876
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 = () => | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there is something in There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sort