From c809b669b98a559bb2aea5fdf0b455652a834e2d Mon Sep 17 00:00:00 2001 From: Justin Kotalik Date: Tue, 30 Jul 2019 12:55:13 -0700 Subject: [PATCH] Catch all exceptions from Exit shutdown (#12518) --- src/Hosting/Hosting/src/WebHostExtensions.cs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Hosting/Hosting/src/WebHostExtensions.cs b/src/Hosting/Hosting/src/WebHostExtensions.cs index e73399c419..6814d1d49f 100644 --- a/src/Hosting/Hosting/src/WebHostExtensions.cs +++ b/src/Hosting/Hosting/src/WebHostExtensions.cs @@ -135,18 +135,21 @@ namespace Microsoft.AspNetCore.Hosting { 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(); @@ -184,4 +187,4 @@ namespace Microsoft.AspNetCore.Hosting await host.StopAsync(); } } -} \ No newline at end of file +}