Skip to content

Commit 0456c9d

Browse files
authored
Handle SIGTERM exit code #6526 (#8294)
1 parent 37a48d8 commit 0456c9d

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

src/Hosting/Hosting/src/Internal/WebHostLifetime.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ internal class WebHostLifetime : IDisposable
1313
private readonly string _shutdownMessage;
1414

1515
private bool _disposed = false;
16+
private bool _exitedGracefully = false;
1617

1718
public WebHostLifetime(CancellationTokenSource cts, ManualResetEventSlim resetEvent, string shutdownMessage)
1819
{
@@ -24,6 +25,11 @@ public WebHostLifetime(CancellationTokenSource cts, ManualResetEventSlim resetEv
2425
Console.CancelKeyPress += CancelKeyPress;
2526
}
2627

28+
internal void SetExitedGracefully()
29+
{
30+
_exitedGracefully = true;
31+
}
32+
2733
public void Dispose()
2834
{
2935
if (_disposed)
@@ -46,6 +52,12 @@ private void CancelKeyPress(object sender, ConsoleCancelEventArgs eventArgs)
4652
private void ProcessExit(object sender, EventArgs eventArgs)
4753
{
4854
Shutdown();
55+
if (_exitedGracefully)
56+
{
57+
// On Linux if the shutdown is triggered by SIGTERM then that's signaled with the 143 exit code.
58+
// Suppress that since we shut down gracefully. https://github.com/aspnet/AspNetCore/issues/6526
59+
Environment.ExitCode = 0;
60+
}
4961
}
5062

5163
private void Shutdown()

src/Hosting/Hosting/src/WebHostExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public static async Task WaitForShutdownAsync(this IWebHost host, CancellationTo
4949
try
5050
{
5151
await host.WaitForTokenShutdownAsync(cts.Token);
52+
lifetime.SetExitedGracefully();
5253
}
5354
finally
5455
{
@@ -91,6 +92,7 @@ public static async Task RunAsync(this IWebHost host, CancellationToken token =
9192
try
9293
{
9394
await host.RunAsync(cts.Token, "Application started. Press Ctrl+C to shut down.");
95+
lifetime.SetExitedGracefully();
9496
}
9597
finally
9698
{

src/Hosting/test/FunctionalTests/ShutdownTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public ShutdownTests(ITestOutputHelper output) : base(output) { }
2828
[ConditionalFact]
2929
[OSSkipCondition(OperatingSystems.Windows)]
3030
[OSSkipCondition(OperatingSystems.MacOSX)]
31-
[OSSkipCondition(OperatingSystems.Linux)] // https://github.com/aspnet/AspNetCore-Internal/issues/1687
3231
public async Task ShutdownTestRun()
3332
{
3433
await ExecuteShutdownTest(nameof(ShutdownTestRun), "Run");

0 commit comments

Comments
 (0)