From a197a757b177562f9fad1e36998545aea3d183f8 Mon Sep 17 00:00:00 2001 From: Matt Connew Date: Wed, 3 Aug 2022 13:18:43 -0700 Subject: [PATCH] Hide task scheduler before calling TaskHelpers.CloseHelperAsync or OpenHelperAsync to prevent async deadlock on low/single thread sync context --- .../src/System/ServiceModel/ClientBase.cs | 2 ++ .../tests/Common/Infrastructure/xunit/WcfTestCase.cs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/System.Private.ServiceModel/src/System/ServiceModel/ClientBase.cs b/src/System.Private.ServiceModel/src/System/ServiceModel/ClientBase.cs index a83417d7bf8..56c573b0254 100644 --- a/src/System.Private.ServiceModel/src/System/ServiceModel/ClientBase.cs +++ b/src/System.Private.ServiceModel/src/System/ServiceModel/ClientBase.cs @@ -308,6 +308,7 @@ public Task OpenAsync() private async Task OpenAsync(TimeSpan timeout) { TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); + await TaskHelpers.EnsureDefaultTaskScheduler(); if (!_useCachedFactory) { await GetChannelFactory().OpenHelperAsync(timeoutHelper.RemainingTime()); @@ -368,6 +369,7 @@ private async Task CloseAsync(TimeSpan timeout) } TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); + await TaskHelpers.EnsureDefaultTaskScheduler(); if (_channel != null) { await InnerChannel.CloseHelperAsync(timeoutHelper.RemainingTime()); diff --git a/src/System.Private.ServiceModel/tests/Common/Infrastructure/xunit/WcfTestCase.cs b/src/System.Private.ServiceModel/tests/Common/Infrastructure/xunit/WcfTestCase.cs index 497e98ed711..c3a1c1afa30 100644 --- a/src/System.Private.ServiceModel/tests/Common/Infrastructure/xunit/WcfTestCase.cs +++ b/src/System.Private.ServiceModel/tests/Common/Infrastructure/xunit/WcfTestCase.cs @@ -51,7 +51,7 @@ public override async Task RunAsync( ConcurrentQueue events = new ConcurrentQueue(); s_testListener.EventWritten = events.Enqueue; Timer timer = null; - if (_failFastDuration != System.Threading.Timeout.InfiniteTimeSpan) + if (_failFastDuration != System.Threading.Timeout.InfiniteTimeSpan && !System.Diagnostics.Debugger.IsAttached) { timer = new Timer((s) => Environment.FailFast("Test timed out"), null,