Skip to content

Commit

Permalink
Fix sync hang issue after async task method call (#5634)
Browse files Browse the repository at this point in the history
  • Loading branch information
imcarolwang authored Aug 21, 2024
1 parent 5c73c1a commit 01e1055
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public interface IWcfService

[OperationContract]
Dictionary<string, string> GetRequestHttpHeaders();

[OperationContract]
Task EchoReturnTaskAsync();
}

[ServiceContract]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,45 @@ public static async Task SecurityModeNone_Echo_RoundTrips_String_SyncAfterAsync(
}
}

// Test for https://github.com/dotnet/wcf/issues/5626
[WcfFact]
[OuterLoop]
public static async Task SecurityModeNone_Echo_RoundTrips_Task_SyncAfterAsync()
{
string testString = "Hello";
ChannelFactory<IWcfService> factory = null;
IWcfService serviceProxy = null;

try
{
// *** SETUP *** \\
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
factory = new ChannelFactory<IWcfService>(binding, new EndpointAddress(Endpoints.Tcp_NoSecurity_Address));
serviceProxy = factory.CreateChannel();

// *** EXECUTE *** \\
string result = serviceProxy.Echo(testString);
Assert.Equal(testString, result);

// Without the ConfigureAwait, the xunit sync context will hop threads from the
// completing thread to their sync context. ConfigureAwait means it continues
// executing on the completing thread and triggers the bug (without the fix).
await serviceProxy.EchoReturnTaskAsync().ConfigureAwait(false);

result = serviceProxy.Echo(testString);
Assert.Equal(testString, result);

// *** CLEANUP *** \\
((ICommunicationObject)serviceProxy).Close();
factory.Close();
}
finally
{
// *** ENSURE CLEANUP *** \\
ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
}
}

// Test for https://github.com/dotnet/wcf/issues/5134
[WcfFact]
[OuterLoop]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,8 @@ public interface IWcfService

[OperationContract]
Dictionary<string,string> GetRequestHttpHeaders();

[OperationContract]
System.Threading.Tasks.Task EchoReturnTask();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -468,5 +468,13 @@ private static Stream StringToStream(string str)

return ms;
}

public System.Threading.Tasks.Task EchoReturnTask()
{
return System.Threading.Tasks.Task.Run(() =>
{
System.Threading.Tasks.Task.Delay(1).Wait();
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private static Task CreateGenericTask(ServiceChannel channel, ProxyOperationRunt

private static Task CreateTask(ServiceChannel channel, ProxyOperationRuntime operation, object[] inputParameters)
{
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
bool completedCallback = false;

Action<IAsyncResult> endCallDelegate = (asyncResult) =>
Expand Down

0 comments on commit 01e1055

Please sign in to comment.