Skip to content

Commit c809b66

Browse files
jkotalikanalogrelay
authored andcommitted
Catch all exceptions from Exit shutdown (#12518)
1 parent 9feeb66 commit c809b66

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/Hosting/Hosting/src/WebHostExtensions.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,21 @@ private static void AttachCtrlcSigtermShutdown(CancellationTokenSource cts, Manu
135135
{
136136
void Shutdown()
137137
{
138-
if (!cts.IsCancellationRequested)
138+
try
139139
{
140-
if (!string.IsNullOrEmpty(shutdownMessage))
141-
{
142-
Console.WriteLine(shutdownMessage);
143-
}
144-
try
140+
if (!cts.IsCancellationRequested)
145141
{
142+
if (!string.IsNullOrEmpty(shutdownMessage))
143+
{
144+
Console.WriteLine(shutdownMessage);
145+
}
146146
cts.Cancel();
147147
}
148-
catch (ObjectDisposedException) { }
149148
}
149+
// When hosting with IIS in-process, we detach the Console handle on main thread exit.
150+
// Console.WriteLine may throw here as we are logging to console on ProcessExit.
151+
// We catch and ignore all exceptions here. Do not log to Console in thie exception handler.
152+
catch (Exception) { }
150153

151154
// Wait on the given reset event
152155
resetEvent.Wait();
@@ -184,4 +187,4 @@ private static async Task WaitForTokenShutdownAsync(this IWebHost host, Cancella
184187
await host.StopAsync();
185188
}
186189
}
187-
}
190+
}

0 commit comments

Comments
 (0)