From 39bd49ea1ae9418d1b8b7aeb6eff2f3e01ba15a7 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Fri, 9 Sep 2016 11:28:49 -0700 Subject: [PATCH] Avoid blocking the MockLibuv loop with test code - This allows for a graceful shutdown with dotnet test -Parallel None - By default, the xunit synccontext will dispatch automatically off the KestrelThread, but it's best not to rely on this behavior. --- .../TestHelpers/MockLibuv.cs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/MockLibuv.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/MockLibuv.cs index c56dc3c491..80584f65be 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/MockLibuv.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/MockLibuv.cs @@ -69,7 +69,6 @@ namespace Microsoft.AspNetCore.Server.KestrelTests.TestHelpers { _loopWh.Wait(); KestrelThreadBlocker.Wait(); - TaskCompletionSource onPostTcs = null; lock (_postLock) { @@ -84,14 +83,14 @@ namespace Microsoft.AspNetCore.Server.KestrelTests.TestHelpers // Ensure any subsequent calls to uv_async_send // create a new _onPostTcs to be completed. _completedOnPostTcs = true; - onPostTcs = _onPostTcs; + + // Calling TrySetResult outside the lock to avoid deadlock + // when the code attempts to call uv_async_send after awaiting + // OnPostTask. Task.Run so the run loop doesn't block either. + var onPostTcs = _onPostTcs; + Task.Run(() => onPostTcs.TrySetResult(null)); } } - - // Calling TrySetResult outside the lock to avoid deadlock - // when the code attempts to call uv_async_send after awaiting - // OnPostTask. - onPostTcs?.TrySetResult(null); } return 0;