diff --git a/.gitignore b/.gitignore index 5de40f3cd7..0042f1e9e0 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ _ReSharper.*/ packages/ artifacts/ PublishProfiles/ +.vs/ *.user *.suo *.cache diff --git a/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/KestrelThread.cs b/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/KestrelThread.cs index 5807898bb2..e24b4e1f12 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/KestrelThread.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/KestrelThread.cs @@ -53,12 +53,16 @@ namespace Microsoft.AspNet.Server.Kestrel Post(OnStop, null); if (!_thread.Join((int)timeout.TotalMilliseconds)) { - Post(OnStopImmediate, null); + Post(OnStopRude, null); if (!_thread.Join((int)timeout.TotalMilliseconds)) { + Post(OnStopImmediate, null); + if (!_thread.Join((int)timeout.TotalMilliseconds)) + { #if DNX451 - _thread.Abort(); + _thread.Abort(); #endif + } } } if (_closeError != null) @@ -72,6 +76,21 @@ namespace Microsoft.AspNet.Server.Kestrel _post.Unreference(); } + private void OnStopRude(object obj) + { + _engine.Libuv.walk( + _loop, + (ptr, arg) => + { + var handle = UvMemory.FromIntPtr(ptr); + if (handle != _post) + { + handle.Dispose(); + } + }, + IntPtr.Zero); + } + private void OnStopImmediate(object obj) { _stopImmediate = true; @@ -133,14 +152,8 @@ namespace Microsoft.AspNet.Server.Kestrel // run the loop one more time to delete the open handles _post.Reference(); _post.DangerousClose(); - _engine.Libuv.walk( - _loop, - (ptr, arg) => - { - var handle = UvMemory.FromIntPtr(ptr); - handle.Dispose(); - }, - IntPtr.Zero); + + // Ensure the "DangerousClose" operation completes in the event loop. var ran2 = _loop.Run(); _loop.Dispose(); @@ -203,7 +216,7 @@ namespace Microsoft.AspNet.Server.Kestrel queue = _closeHandleAdding; _closeHandleAdding = _closeHandleRunning; _closeHandleRunning = queue; - } + } while (queue.Count != 0) { var closeHandle = queue.Dequeue(); diff --git a/src/Microsoft.AspNet.Server.Kestrel/Networking/UcAsyncHandle.cs b/src/Microsoft.AspNet.Server.Kestrel/Networking/UvAsyncHandle.cs similarity index 100% rename from src/Microsoft.AspNet.Server.Kestrel/Networking/UcAsyncHandle.cs rename to src/Microsoft.AspNet.Server.Kestrel/Networking/UvAsyncHandle.cs diff --git a/src/Microsoft.AspNet.Server.Kestrel/Networking/UvHandle.cs b/src/Microsoft.AspNet.Server.Kestrel/Networking/UvHandle.cs index 98a0f1e077..4c9f23aec1 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Networking/UvHandle.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Networking/UvHandle.cs @@ -32,9 +32,12 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking { _uv.close(memory, _destroyMemory); } - else + else if (_queueCloseHandle != null) { - _queueCloseHandle(memory2 => _uv.close(memory2, _destroyMemory), memory); + // This can be called from the finalizer. + // Ensure the closure doesn't reference "this". + var uv = _uv; + _queueCloseHandle(memory2 => uv.close(memory2, _destroyMemory), memory); } } return true;