From dc12f3150ee04f1edf808d4a805b7f995a7cb4a7 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Tue, 19 Jul 2016 16:49:19 -0700 Subject: [PATCH] Wait for graceful shutdown now that it usually works - Unreference the async handle to allow the loop to stop without walking first - Wait before walking open handles to allow earlier uv_close calls to complete --- .../Internal/Infrastructure/KestrelThread.cs | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/KestrelThread.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/KestrelThread.cs index 47f56ef229..02ad95bd75 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/KestrelThread.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/KestrelThread.cs @@ -105,34 +105,40 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal } } + var stepTimeout = (int)(timeout.TotalMilliseconds / 3); + if (_thread.IsAlive) { // These operations need to run on the libuv thread so it only makes // sense to attempt execution if it's still running DisposeConnections(); - var stepTimeout = (int)(timeout.TotalMilliseconds / 2); - try + Post(t => t.AllowStop()); + if (!_thread.Join(stepTimeout)) { - Post(t => t.OnStopRude()); - if (!_thread.Join(stepTimeout)) + + try { - Post(t => t.OnStopImmediate()); + Post(t => t.OnStopRude()); + if (!_thread.Join(stepTimeout)) + { + Post(t => t.OnStopImmediate()); + if (!_thread.Join(stepTimeout)) + { + _log.LogError(0, null, "KestrelThread.Stop failed to terminate libuv thread."); + } + } + } + catch (ObjectDisposedException) + { + // REVIEW: Should we log something here? + // Until we rework this logic, ODEs are bound to happen sometimes. if (!_thread.Join(stepTimeout)) { _log.LogError(0, null, "KestrelThread.Stop failed to terminate libuv thread."); } } } - catch (ObjectDisposedException) - { - // REVIEW: Should we log something here? - // Until we rework this logic, ODEs are bound to happen sometimes. - if (!_thread.Join(stepTimeout)) - { - _log.LogError(0, null, "KestrelThread.Stop failed to terminate libuv thread."); - } - } } if (_closeError != null)