From 6a403d231e72be8de83933b3fcf36060c7abcb5f Mon Sep 17 00:00:00 2001 From: David Fowler Date: Sat, 15 Apr 2017 19:04:44 -0700 Subject: [PATCH] Clean up IThreadPool interface (#1696) - Remove tcs completion methods --- .../Internal/Infrastructure/IThreadPool.cs | 4 -- .../Infrastructure/InlineLoggingThreadPool.cs | 37 ----------- .../Infrastructure/LoggingThreadPool.cs | 63 ------------------- .../LoggingThreadPoolTests.cs | 48 -------------- 4 files changed, 152 deletions(-) delete mode 100644 test/Microsoft.AspNetCore.Server.Kestrel.Core.Tests/LoggingThreadPoolTests.cs diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Infrastructure/IThreadPool.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Infrastructure/IThreadPool.cs index 819da220bc..92fa225bc8 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Infrastructure/IThreadPool.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Infrastructure/IThreadPool.cs @@ -3,16 +3,12 @@ using System; using System.Threading; -using System.Threading.Tasks; using Microsoft.AspNetCore.Server.Kestrel.Internal.System.IO.Pipelines; namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure { public interface IThreadPool : IScheduler { - void Complete(TaskCompletionSource tcs); - void Cancel(TaskCompletionSource tcs); - void Error(TaskCompletionSource tcs, Exception ex); void Run(Action action); void UnsafeRun(WaitCallback action, object state); } diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Infrastructure/InlineLoggingThreadPool.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Infrastructure/InlineLoggingThreadPool.cs index b81bbfab0a..0160b59590 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Infrastructure/InlineLoggingThreadPool.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Infrastructure/InlineLoggingThreadPool.cs @@ -3,7 +3,6 @@ using System; using System.Threading; -using System.Threading.Tasks; using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure @@ -29,42 +28,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure } } - public void Complete(TaskCompletionSource tcs) - { - try - { - tcs.TrySetResult(null); - } - catch (Exception e) - { - _log.LogError(0, e, "InlineLoggingThreadPool.Complete"); - } - } - - public void Cancel(TaskCompletionSource tcs) - { - try - { - tcs.TrySetCanceled(); - } - catch (Exception e) - { - _log.LogError(0, e, "InlineLoggingThreadPool.Cancel"); - } - } - - public void Error(TaskCompletionSource tcs, Exception ex) - { - try - { - tcs.TrySetException(ex); - } - catch (Exception e) - { - _log.LogError(0, e, "InlineLoggingThreadPool.Error"); - } - } - public void UnsafeRun(WaitCallback action, object state) { action(state); diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Infrastructure/LoggingThreadPool.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Infrastructure/LoggingThreadPool.cs index 6fcad137fd..1718967c50 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Infrastructure/LoggingThreadPool.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Infrastructure/LoggingThreadPool.cs @@ -3,7 +3,6 @@ using System; using System.Threading; -using System.Threading.Tasks; using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure @@ -13,8 +12,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure private readonly IKestrelTrace _log; private WaitCallback _runAction; - private WaitCallback _cancelTcs; - private WaitCallback _completeTcs; public LoggingThreadPool(IKestrelTrace log) { @@ -25,8 +22,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure // call stack for exceptions and profiling else it shows up as LoggingThreadPool.ctor>b__4_0 // and you aren't sure which of the 3 functions was called. RunAction(); - CompleteTcs(); - CancelTcs(); } private void RunAction() @@ -45,38 +40,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure }; } - private void CompleteTcs() - { - // Capture _log in a singleton closure - _completeTcs = (o) => - { - try - { - ((TaskCompletionSource)o).TrySetResult(null); - } - catch (Exception e) - { - _log.LogError(0, e, "LoggingThreadPool.Complete"); - } - }; - } - - private void CancelTcs() - { - // Capture _log in a singleton closure - _cancelTcs = (o) => - { - try - { - ((TaskCompletionSource)o).TrySetCanceled(); - } - catch (Exception e) - { - _log.LogError(0, e, "LoggingThreadPool.Cancel"); - } - }; - } - public void Run(Action action) { ThreadPool.QueueUserWorkItem(_runAction, action); @@ -87,32 +50,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure ThreadPool.QueueUserWorkItem(action, state); } - public void Complete(TaskCompletionSource tcs) - { - ThreadPool.QueueUserWorkItem(_completeTcs, tcs); - } - - public void Cancel(TaskCompletionSource tcs) - { - ThreadPool.QueueUserWorkItem(_cancelTcs, tcs); - } - - public void Error(TaskCompletionSource tcs, Exception ex) - { - // ex and _log are closure captured - ThreadPool.QueueUserWorkItem((o) => - { - try - { - ((TaskCompletionSource)o).TrySetException(ex); - } - catch (Exception e) - { - _log.LogError(0, e, "LoggingThreadPool.Error"); - } - }, tcs); - } - public void Schedule(Action action) { Run(action); diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.Core.Tests/LoggingThreadPoolTests.cs b/test/Microsoft.AspNetCore.Server.Kestrel.Core.Tests/LoggingThreadPoolTests.cs deleted file mode 100644 index 9616315bae..0000000000 --- a/test/Microsoft.AspNetCore.Server.Kestrel.Core.Tests/LoggingThreadPoolTests.cs +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - - -using System; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; -using Microsoft.AspNetCore.Testing; -using Xunit; - -namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests -{ - public class LoggingThreadPoolTests - { - [Fact] - public void TcsContinuationErrorsDontGetLoggedAsGeneralErrors() - { - var testLogger = new TestApplicationErrorLogger(); - var testKestrelTrace = new TestKestrelTrace(testLogger); - var threadPool = new LoggingThreadPool(testKestrelTrace); - - var completeTcs = new TaskCompletionSource(); - ThrowSynchronously(completeTcs.Task); - threadPool.Complete(completeTcs); - - var errorTcs = new TaskCompletionSource(); - ThrowSynchronously(errorTcs.Task); - threadPool.Error(errorTcs, new Exception()); - - var cancelTcs = new TaskCompletionSource(); - ThrowSynchronously(cancelTcs.Task); - threadPool.Cancel(cancelTcs); - - Assert.Throws(() => - Task.WhenAll(completeTcs.Task, errorTcs.Task, cancelTcs.Task).Wait()); - - Assert.Equal(0, testLogger.TotalErrorsLogged); - } - - private void ThrowSynchronously(Task task) - { - task.ContinueWith(_ => - { - throw new Exception(); - }, TaskContinuationOptions.ExecuteSynchronously); - } - } -}