From 9a5d6c8879182c737d7243d54a190887aae9d4e2 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Sat, 29 Apr 2017 01:15:10 -0700 Subject: [PATCH] More shutdown tweaks (#1760) * More shutdown tweaks - Added assert if loop has ended before starting the shutdown sequence. --- .../Internal/LibuvThread.cs | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/LibuvThread.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/LibuvThread.cs index f5e4909635..1591e57074 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/LibuvThread.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/LibuvThread.cs @@ -98,39 +98,35 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal } } - if (!_threadTcs.Task.IsCompleted) - { - var stepTimeout = TimeSpan.FromTicks(timeout.Ticks / 3); + Debug.Assert(!_threadTcs.Task.IsCompleted, "The loop thread was completed before calling uv_unref on the post handle."); - try + var stepTimeout = TimeSpan.FromTicks(timeout.Ticks / 3); + + try + { + Post(t => t.AllowStop()); + if (!await WaitAsync(_threadTcs.Task, stepTimeout).ConfigureAwait(false)) { - Post(t => t.AllowStop()); + Post(t => t.OnStopRude()); if (!await WaitAsync(_threadTcs.Task, stepTimeout).ConfigureAwait(false)) { - Post(t => t.OnStopRude()); + Post(t => t.OnStopImmediate()); if (!await WaitAsync(_threadTcs.Task, stepTimeout).ConfigureAwait(false)) { - Post(t => t.OnStopImmediate()); - if (!await WaitAsync(_threadTcs.Task, stepTimeout).ConfigureAwait(false)) - { - _log.LogCritical($"{nameof(LibuvThread)}.{nameof(StopAsync)} failed to terminate libuv thread."); - } + _log.LogCritical($"{nameof(LibuvThread)}.{nameof(StopAsync)} failed to terminate libuv thread."); } } } - catch (ObjectDisposedException) + } + catch (ObjectDisposedException) + { + if (!await WaitAsync(_threadTcs.Task, stepTimeout).ConfigureAwait(false)) { - if (!await WaitAsync(_threadTcs.Task, stepTimeout).ConfigureAwait(false)) - { - _log.LogCritical($"{nameof(LibuvThread)}.{nameof(StopAsync)} failed to terminate libuv thread."); - } + _log.LogCritical($"{nameof(LibuvThread)}.{nameof(StopAsync)} failed to terminate libuv thread."); } } - if (_closeError != null) - { - _closeError.Throw(); - } + _closeError?.Throw(); } #if DEBUG