Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchron calls after Asynchron calls with no result blocks forever #5626

Open
MJE-GTI opened this issue Aug 9, 2024 · 4 comments
Open

Synchron calls after Asynchron calls with no result blocks forever #5626

MJE-GTI opened this issue Aug 9, 2024 · 4 comments
Assignees
Labels

Comments

@MJE-GTI
Copy link

MJE-GTI commented Aug 9, 2024

Describe the bug
When I call a sync WCF OperatinContract after an async OperationContract call with no result the synchron call is never resolved. If the perverious async call has a result this problem does not happen.

To Reproduce
Create a WCF Service with Async Methods that returns somsthing (Test1Async) and one that returns nothing (Test2Async) and add a sync Method (Test3)

[ServiceContract]
public interface ITestService
{
    [OperationContract]
    Task<string> Test1Async(string text);

    [OperationContract]
    Task Test2Async(string text);

    [OperationContract]
    string Test3(string text);
}

Call the WCF Interface (see code below with comments)

private static async Task CallNetPipeBinding(string hostAddr)
{
    IClientChannel channel = null;

    var binding = new NetNamedPipeBinding();

    var factory = new ChannelFactory<ITestService>(binding, new EndpointAddress($"{hostAddr}/netPipe"));
    await Task.Factory.FromAsync(factory.BeginOpen, factory.EndOpen, TaskCreationOptions.None);
    try
    {
        ITestService client = factory.CreateChannel();
        channel = client as IClientChannel;
        await Task.Factory.FromAsync(channel.BeginOpen, channel.EndOpen, TaskCreationOptions.None);

        await client.Test1Async("Test");
        client.Test3("Test");// This call works because the perverious call returns something
        await client.Test2Async("Test");
        client.Test3("Test");// This call does not work becausee the pereverious call returns nothing
    }
    finally
    {
        factory.Close();
    }
}

Expected behavior
Synchron calls after async call with no result don't block forever.

Additional context
I think the problem is that the TaskCompletionSource withtout result is not created with TaskCreationOptions.RunContinuationsAsynchronously

TaskCompletionSource Creation with return Type:

return new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);

TaskCompletionSourceProxy tcsp = new TaskCompletionSourceProxy(operation.TaskTResult);

TaskCompletionSource Creation without return Type:

TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();

@mconnew
Copy link
Member

mconnew commented Aug 9, 2024

Which package version are you using? I believe this was fixed in the last year or two.

@mconnew
Copy link
Member

mconnew commented Aug 9, 2024

I believe this is the explanation of what's going on along with a workaround if you can't upgrade to a fixed version.
#4946 (comment)
Basically you do this:

        await client.Test1Async("Test");
        client.Test3("Test");// This call works because the perverious call returns something
        await client.Test2Async("Test");
        await Task.Yield();
        client.Test3("Test");// This call does not work becausee the pereverious call returns nothing

@MJE-GTI
Copy link
Author

MJE-GTI commented Aug 10, 2024

I tested it with the NuGet Packages Version 8.0.0 and with a local build of the master branch witch is less then a month old.
The Example in #4946 (comment) only uses Tasks with a result Task<T> and this works without any problems for me. But if my Task does not have a result Task is doesn't work.

@HongGit
Copy link
Contributor

HongGit commented Aug 20, 2024

@imcarolwang can you please create a PR and add unit tests for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants