Skip to content

Commit 802cfd4

Browse files
committed
1 parent d42c835 commit 802cfd4

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

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

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Runtime.InteropServices;
5+
46
namespace Microsoft.AspNetCore.Hosting;
57

68
internal sealed class WebHostLifetime : IDisposable
@@ -9,22 +11,22 @@ internal sealed class WebHostLifetime : IDisposable
911
private readonly ManualResetEventSlim _resetEvent;
1012
private readonly string _shutdownMessage;
1113

14+
private PosixSignalRegistration _sigIntRegistration;
15+
private PosixSignalRegistration _sigQuitRegistration;
16+
private PosixSignalRegistration _sigTermRegistration;
17+
1218
private bool _disposed;
13-
private bool _exitedGracefully;
1419

1520
public WebHostLifetime(CancellationTokenSource cts, ManualResetEventSlim resetEvent, string shutdownMessage)
1621
{
1722
_cts = cts;
1823
_resetEvent = resetEvent;
1924
_shutdownMessage = shutdownMessage;
2025

21-
AppDomain.CurrentDomain.ProcessExit += ProcessExit;
22-
Console.CancelKeyPress += CancelKeyPress;
23-
}
24-
25-
internal void SetExitedGracefully()
26-
{
27-
_exitedGracefully = true;
26+
Action<PosixSignalContext> handler = HandlePosixSignal;
27+
_sigIntRegistration = PosixSignalRegistration.Create(PosixSignal.SIGINT, handler);
28+
_sigQuitRegistration = PosixSignalRegistration.Create(PosixSignal.SIGQUIT, handler);
29+
_sigTermRegistration = PosixSignalRegistration.Create(PosixSignal.SIGTERM, handler);
2830
}
2931

3032
public void Dispose()
@@ -35,26 +37,18 @@ public void Dispose()
3537
}
3638

3739
_disposed = true;
38-
AppDomain.CurrentDomain.ProcessExit -= ProcessExit;
39-
Console.CancelKeyPress -= CancelKeyPress;
40-
}
4140

42-
private void CancelKeyPress(object? sender, ConsoleCancelEventArgs eventArgs)
43-
{
44-
Shutdown();
45-
// Don't terminate the process immediately, wait for the Main thread to exit gracefully.
46-
eventArgs.Cancel = true;
41+
_sigIntRegistration.Dispose() ;
42+
_sigQuitRegistration.Dispose();
43+
_sigTermRegistration.Dispose();
4744
}
4845

49-
private void ProcessExit(object? sender, EventArgs eventArgs)
46+
private void HandlePosixSignal(PosixSignalContext context)
5047
{
5148
Shutdown();
52-
if (_exitedGracefully)
53-
{
54-
// On Linux if the shutdown is triggered by SIGTERM then that's signaled with the 143 exit code.
55-
// Suppress that since we shut down gracefully. https://github.com/dotnet/aspnetcore/issues/6526
56-
Environment.ExitCode = 0;
57-
}
49+
50+
// Don't terminate the process immediately, wait for the Main thread to exit gracefully.
51+
context.Cancel = true;
5852
}
5953

6054
private void Shutdown()

src/Hosting/Hosting/src/WebHostExtensions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public static async Task WaitForShutdownAsync(this IWebHost host, CancellationTo
5252
try
5353
{
5454
await host.WaitForTokenShutdownAsync(cts.Token);
55-
lifetime.SetExitedGracefully();
5655
}
5756
finally
5857
{
@@ -95,7 +94,6 @@ public static async Task RunAsync(this IWebHost host, CancellationToken token =
9594
try
9695
{
9796
await host.RunAsync(cts.Token, "Application started. Press Ctrl+C to shut down.");
98-
lifetime.SetExitedGracefully();
9997
}
10098
finally
10199
{

0 commit comments

Comments
 (0)