From bc56d11d8ca9ba3e3b6a3c84900b0f8027ff2e96 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Wed, 17 Feb 2016 16:33:34 -0800 Subject: [PATCH] Set IsBackground property to true on libuv Threads for non-debug builds - If libuv doesn't shutdown as expected, the process will still stop. Thanks @benaadams! - Address other minor PR feedback. --- .../Http/ConnectionManager.cs | 4 ++-- src/Microsoft.AspNetCore.Server.Kestrel/Http/Frame.cs | 3 +-- .../Infrastructure/KestrelThread.cs | 6 ++++++ .../FrameResponseHeadersTests.cs | 8 ++++---- .../TestHelpers/MockConnection.cs | 3 --- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Http/ConnectionManager.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Http/ConnectionManager.cs index 0d807aed1f..640832a412 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Http/ConnectionManager.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Http/ConnectionManager.cs @@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http { if (_connectionStopTasks != null) { - throw new InvalidOperationException(nameof(WalkConnectionsAndClose) + " cannot be called twice."); + throw new InvalidOperationException($"{nameof(WalkConnectionsAndClose)} cannot be called twice."); } _connectionStopTasks = new List(); @@ -44,7 +44,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http { if (_connectionStopTasks == null) { - throw new InvalidOperationException(nameof(WalkConnectionsAndClose) + " must be called first."); + throw new InvalidOperationException($"{nameof(WalkConnectionsAndClose)} must be called first."); } return Task.WhenAll(_connectionStopTasks); diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Http/Frame.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Http/Frame.cs index 6b99b9898f..c830fcc2ee 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Http/Frame.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Http/Frame.cs @@ -189,12 +189,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http protected FrameRequestHeaders FrameRequestHeaders => _frameHeaders.RequestHeaders; - public Frame InitializeHeaders() + public void InitializeHeaders() { _frameHeaders = HttpComponentFactory.CreateHeaders(DateHeaderValueManager); RequestHeaders = _frameHeaders.RequestHeaders; ResponseHeaders = _frameHeaders.ResponseHeaders; - return this; } diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Infrastructure/KestrelThread.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Infrastructure/KestrelThread.cs index 1cbeef9fd9..5b7d998f8e 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Infrastructure/KestrelThread.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Infrastructure/KestrelThread.cs @@ -79,6 +79,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel public void Stop(TimeSpan timeout) { +#if !DEBUG + // Mark the thread as being as unimportant to keeping the process alive. + // Don't do this for debug builds, so we know if the thread isn't terminating. + _thread.IsBackground = true; +#endif + if (!_initCompleted) { return; diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/FrameResponseHeadersTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/FrameResponseHeadersTests.cs index 30c03a0b35..f03deed0e3 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/FrameResponseHeadersTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/FrameResponseHeadersTests.cs @@ -26,8 +26,8 @@ namespace Microsoft.AspNetCore.Server.KestrelTests ServerInformation = serverInformation, HttpComponentFactory = new HttpComponentFactory(serverInformation) }; - var frame = new Frame(application: null, context: connectionContext) - .InitializeHeaders(); + var frame = new Frame(application: null, context: connectionContext); + frame.InitializeHeaders(); IDictionary headers = frame.ResponseHeaders; @@ -60,8 +60,8 @@ namespace Microsoft.AspNetCore.Server.KestrelTests ServerInformation = serverInformation, HttpComponentFactory = new HttpComponentFactory(serverInformation) }; - var frame = new Frame(application: null, context: connectionContext) - .InitializeHeaders(); + var frame = new Frame(application: null, context: connectionContext); + frame.InitializeHeaders(); Assert.True(frame.ResponseHeaders.Count > 0); diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/MockConnection.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/MockConnection.cs index e667ab7783..3daea602c7 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/MockConnection.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/MockConnection.cs @@ -3,10 +3,7 @@ using System; using System.Threading; -using Microsoft.AspNetCore.Server.Kestrel; using Microsoft.AspNetCore.Server.Kestrel.Http; -using Microsoft.AspNetCore.Server.Kestrel.Infrastructure; -using Microsoft.AspNetCore.Server.Kestrel.Networking; namespace Microsoft.AspNetCore.Server.KestrelTests.TestHelpers {