Skip to content

Commit 69feac2

Browse files
authored
Catch all exceptions from Exit shutdown (master). (#12522)
1 parent 34903da commit 69feac2

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,21 @@ private void ProcessExit(object sender, EventArgs eventArgs)
6262

6363
private void Shutdown()
6464
{
65-
if (!_cts.IsCancellationRequested)
65+
try
6666
{
67-
if (!string.IsNullOrEmpty(_shutdownMessage))
68-
{
69-
Console.WriteLine(_shutdownMessage);
70-
}
71-
try
67+
if (!_cts.IsCancellationRequested)
7268
{
69+
if (!string.IsNullOrEmpty(_shutdownMessage))
70+
{
71+
Console.WriteLine(_shutdownMessage);
72+
}
7373
_cts.Cancel();
7474
}
75-
catch (ObjectDisposedException) { }
7675
}
77-
76+
// When hosting with IIS in-process, we detach the Console handle on main thread exit.
77+
// Console.WriteLine may throw here as we are logging to console on ProcessExit.
78+
// We catch and ignore all exceptions here. Do not log to Console in thie exception handler.
79+
catch (Exception) {}
7880
// Wait on the given reset event
7981
_resetEvent.Wait();
8082
}

0 commit comments

Comments
 (0)