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

Task result obtained synchronously inside an asynchronous method #130

Open
ilyalukyanov opened this issue Sep 16, 2020 · 0 comments · May be fixed by #178
Open

Task result obtained synchronously inside an asynchronous method #130

ilyalukyanov opened this issue Sep 16, 2020 · 0 comments · May be fixed by #178

Comments

@ilyalukyanov
Copy link

At this line

var response = SendRequest(url, method, content).Result;
inside an asynchronous method a result of SendRequest is taken synchronously.

public async Task<string> MakeRequest(string url, HttpMethod method, HttpContent content = null)
{
  var response = SendRequest(url, method, content).Result;

  var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

  if (!response.IsSuccessStatusCode)
  {
    HandleHTTPErrors(response, responseContent);
  }
  return responseContent;
}

I'm wondering if there was a specific reason for that, or can it become just var response = await SendRequest(url, method, content)?

There's a very minor issue with that - if SendRequest throws an exception (e.g. TaskCancelled upon timeout), then caller would get an AggregateException, which is a bit counter-intuitive and less convenient to process.

Also, such a pattern seem a bit dangerous as theoretically it could lead to deadlocks depending on circumstances. In our application we haven't seen any and there are ConfigureAwait-s, so that mightn't be a real issue.

euantorano added a commit to euantorano/notifications-net-client that referenced this issue Sep 27, 2023
@euantorano euantorano linked a pull request Sep 27, 2023 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant