Skip to content

Using .Result to run methods synchronously causes deadlocks #238

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

Open
chippy opened this issue Jan 18, 2017 · 5 comments
Open

Using .Result to run methods synchronously causes deadlocks #238

chippy opened this issue Jan 18, 2017 · 5 comments
Assignees
Labels

Comments

@chippy
Copy link

chippy commented Jan 18, 2017

I am using ForceClient in an ASP.NET MVC project. I have a Salesforce service called by my controllers which makes use of ForceClient. I have implemented all of these methods asynchronously, however in MVC 5, child actions can't be marked async, so in a few cases I have had to create a synchronous version of these methods which uses .Result as follows (the async versions are identical to this except they use await instead of .Result):

string query = $"SELECT Id, Name, AccountId, Email, NOP_Customer_ID__c FROM Contact WHERE Id='{salesforceId}'";

try
{
    var results = forceClient.QueryAsync<SalesforceContact>(query).Result;
    return results.Records.FirstOrDefault();
}
catch (Exception ex)
{
    throw new ApplicationException("Unable to retrieve contact.", ex);
}

This works fine on the version that's currently available on Nuget, but on the latest version, it results in a deadlock.

If it helps at all, I'm able to avoid the deadlock on the newest version by instead calling the async version of the method as follows (which is arguably cleaner anyway):

return Task.Run(async () => await GetContactByIdAsync(salesforceId)).Result;
@joshbrekke
Copy link

Hey chippy,

This definitely seemed to fix the deadlock I was having as well with querying.

Thanks!

Josh

@gabrieluy
Copy link

chippy i love you , you safe my life

@chippy
Copy link
Author

chippy commented Mar 5, 2018

You're very welcome. I'm happy that I could help!

@wadewegner wadewegner self-assigned this Aug 17, 2018
@wadewegner wadewegner added the bug label Aug 17, 2018
@drahlf
Copy link

drahlf commented Nov 1, 2018

I'm running into this problem using v2.07.
As chippy opened the original request on Jan. 2017 - will there ever be a solution to this?

Does anybody have a simple enough workaround to use these calls in a syncronous way?
Don't know how chippy saved gabrieluy's live with the code example of how to trap the library to deadlock ;-)

@kentrue
Copy link

kentrue commented Nov 7, 2019

I observed this issue as well, but was able to resolve it by adding .ConfigureAwait(false) to all the awaited async method calls inside the library that were missing it. I've added a pull request (#359) to resolve this issue. This resolved the issue for me.

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

6 participants