diff --git a/src/Hosting/Hosting/src/Internal/WebHostLifetime.cs b/src/Hosting/Hosting/src/Internal/WebHostLifetime.cs index 17fc86ed69..ea0e631a94 100644 --- a/src/Hosting/Hosting/src/Internal/WebHostLifetime.cs +++ b/src/Hosting/Hosting/src/Internal/WebHostLifetime.cs @@ -13,6 +13,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal private readonly string _shutdownMessage; private bool _disposed = false; + private bool _exitedGracefully = false; public WebHostLifetime(CancellationTokenSource cts, ManualResetEventSlim resetEvent, string shutdownMessage) { @@ -24,6 +25,11 @@ namespace Microsoft.AspNetCore.Hosting.Internal Console.CancelKeyPress += CancelKeyPress; } + internal void SetExitedGracefully() + { + _exitedGracefully = true; + } + public void Dispose() { if (_disposed) @@ -46,6 +52,12 @@ namespace Microsoft.AspNetCore.Hosting.Internal private void ProcessExit(object sender, EventArgs eventArgs) { 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() diff --git a/src/Hosting/Hosting/src/WebHostExtensions.cs b/src/Hosting/Hosting/src/WebHostExtensions.cs index 53923f85f6..9bbb39dd11 100644 --- a/src/Hosting/Hosting/src/WebHostExtensions.cs +++ b/src/Hosting/Hosting/src/WebHostExtensions.cs @@ -49,6 +49,7 @@ namespace Microsoft.AspNetCore.Hosting try { await host.WaitForTokenShutdownAsync(cts.Token); + lifetime.SetExitedGracefully(); } finally { @@ -91,6 +92,7 @@ namespace Microsoft.AspNetCore.Hosting try { await host.RunAsync(cts.Token, "Application started. Press Ctrl+C to shut down."); + lifetime.SetExitedGracefully(); } finally { diff --git a/src/Hosting/test/FunctionalTests/ShutdownTests.cs b/src/Hosting/test/FunctionalTests/ShutdownTests.cs index aab36e5856..82c66fb97c 100644 --- a/src/Hosting/test/FunctionalTests/ShutdownTests.cs +++ b/src/Hosting/test/FunctionalTests/ShutdownTests.cs @@ -28,7 +28,6 @@ namespace Microsoft.AspNetCore.Hosting.FunctionalTests [ConditionalFact] [OSSkipCondition(OperatingSystems.Windows)] [OSSkipCondition(OperatingSystems.MacOSX)] - [OSSkipCondition(OperatingSystems.Linux)] // https://github.com/aspnet/AspNetCore-Internal/issues/1687 public async Task ShutdownTestRun() { await ExecuteShutdownTest(nameof(ShutdownTestRun), "Run");