Handle SIGTERM exit code #6526 (#8294)

This commit is contained in:
Chris Ross 2019-03-19 19:15:00 -04:00 committed by GitHub
parent 37a48d897b
commit 0456c9dcc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 1 deletions

View File

@ -13,6 +13,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
private readonly string _shutdownMessage; private readonly string _shutdownMessage;
private bool _disposed = false; private bool _disposed = false;
private bool _exitedGracefully = false;
public WebHostLifetime(CancellationTokenSource cts, ManualResetEventSlim resetEvent, string shutdownMessage) public WebHostLifetime(CancellationTokenSource cts, ManualResetEventSlim resetEvent, string shutdownMessage)
{ {
@ -24,6 +25,11 @@ namespace Microsoft.AspNetCore.Hosting.Internal
Console.CancelKeyPress += CancelKeyPress; Console.CancelKeyPress += CancelKeyPress;
} }
internal void SetExitedGracefully()
{
_exitedGracefully = true;
}
public void Dispose() public void Dispose()
{ {
if (_disposed) if (_disposed)
@ -46,6 +52,12 @@ namespace Microsoft.AspNetCore.Hosting.Internal
private void ProcessExit(object sender, EventArgs eventArgs) private void ProcessExit(object sender, EventArgs eventArgs)
{ {
Shutdown(); Shutdown();
if (_exitedGracefully)
{
// On Linux if the shutdown is triggered by SIGTERM then that's signaled with the 143 exit code.
// Suppress that since we shut down gracefully. https://github.com/aspnet/AspNetCore/issues/6526
Environment.ExitCode = 0;
}
} }
private void Shutdown() private void Shutdown()

View File

@ -49,6 +49,7 @@ namespace Microsoft.AspNetCore.Hosting
try try
{ {
await host.WaitForTokenShutdownAsync(cts.Token); await host.WaitForTokenShutdownAsync(cts.Token);
lifetime.SetExitedGracefully();
} }
finally finally
{ {
@ -91,6 +92,7 @@ namespace Microsoft.AspNetCore.Hosting
try 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.");
lifetime.SetExitedGracefully();
} }
finally finally
{ {

View File

@ -28,7 +28,6 @@ namespace Microsoft.AspNetCore.Hosting.FunctionalTests
[ConditionalFact] [ConditionalFact]
[OSSkipCondition(OperatingSystems.Windows)] [OSSkipCondition(OperatingSystems.Windows)]
[OSSkipCondition(OperatingSystems.MacOSX)] [OSSkipCondition(OperatingSystems.MacOSX)]
[OSSkipCondition(OperatingSystems.Linux)] // https://github.com/aspnet/AspNetCore-Internal/issues/1687
public async Task ShutdownTestRun() public async Task ShutdownTestRun()
{ {
await ExecuteShutdownTest(nameof(ShutdownTestRun), "Run"); await ExecuteShutdownTest(nameof(ShutdownTestRun), "Run");