From 69feac263332424ceccfb673cbf205e31855abdc Mon Sep 17 00:00:00 2001 From: Justin Kotalik Date: Wed, 24 Jul 2019 14:56:04 -0700 Subject: [PATCH] Catch all exceptions from Exit shutdown (master). (#12522) --- .../Hosting/src/Internal/WebHostLifetime.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Hosting/Hosting/src/Internal/WebHostLifetime.cs b/src/Hosting/Hosting/src/Internal/WebHostLifetime.cs index 9d47fb68f5..19d9006647 100644 --- a/src/Hosting/Hosting/src/Internal/WebHostLifetime.cs +++ b/src/Hosting/Hosting/src/Internal/WebHostLifetime.cs @@ -62,19 +62,21 @@ namespace Microsoft.AspNetCore.Hosting private void Shutdown() { - if (!_cts.IsCancellationRequested) + try { - if (!string.IsNullOrEmpty(_shutdownMessage)) - { - Console.WriteLine(_shutdownMessage); - } - try + if (!_cts.IsCancellationRequested) { + if (!string.IsNullOrEmpty(_shutdownMessage)) + { + Console.WriteLine(_shutdownMessage); + } _cts.Cancel(); } - catch (ObjectDisposedException) { } } - + // When hosting with IIS in-process, we detach the Console handle on main thread exit. + // Console.WriteLine may throw here as we are logging to console on ProcessExit. + // We catch and ignore all exceptions here. Do not log to Console in thie exception handler. + catch (Exception) {} // Wait on the given reset event _resetEvent.Wait(); }