Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling async methods from a synchronous context

I'm calling a service over HTTP (ultimately using the HttpClient.SendAsync method) from within my code. This code is then called into from a WebAPI controller action. Mostly, it works fine (tests pass) but then when I deploy on say IIS, I experience a deadlock because caller of the async method call has been blocked and the continuation cannot proceed on that thread until it finishes (which it won't).

While I could make most of my methods async I don't feel as if I have a basic understanding of when I'd must do this.

For example, let's say I did make most of my methods async (since they ultimately call other async service methods) how would I then invoke the first async method of my program if I built say a message loop where I want some control of the degree of parallelism?

Since the HttpClient doesn't have any synchronous methods, what can I safely presume to do if I have an abstraction that isn't async aware? I've read about the ConfigureAwait(false) but I don't really understand what it does. It's strange to me that it's set after the async invocation. To me that feels as if a race waiting to happen... however unlikely...

WebAPI example:

public HttpResponseMessage Get()
{
  var userContext = contextService.GetUserContext(); // <-- synchronous
  return ...
}

// Some IUserContextService implementation
public IUserContext GetUserContext()
{
  var httpClient = new HttpClient();
  var result = httpClient.GetAsync(...).Result; // <-- I really don't care if this is asynchronous or not
  return new HttpUserContext(result);
}

Message loop example:

var mq = new MessageQueue();
// we then run say 8 tasks that do this
for (;;)
{
  var m = mq.Get();
  var c = GetCommand(m);
  c.InvokeAsync().Wait();
  m.Delete();
}

When you have a message loop that allow things to happen in parallel and you have asynchronous methods, there's a opportunity to minimize latency. Basically, what I want to accomplish in this instance is to minimize latency and idle time. Though I'm actually unsure as to how to invoke into the command that's associated with the message that arrives off the queue.

To be more specific, if the command invocation needs to do service requests there's going to be latency in the invocation that could be used to get the next message. Stuff like that. I can totally do this simply by wrapping up things in queues and coordinating this myself but I'd like to see this work with just some async/await stuff.

like image 750
John Leidegren Avatar asked Dec 10 '25 05:12

John Leidegren


1 Answers

While I could make most of my methods async I don't feel as if I have a basic understanding of when I'd must do this.

Start at the lowest level. It sounds like you've already got a start, but if you're looking for more at the lowest level, then the rule of thumb is anything I/O-based should be made async (e.g., HttpClient).

Then it's a matter of repeating the async infection. You want to use async methods, so you call them with await. So that method must be async. So all of its callers must use await, so they must also be async, etc.

how would I then invoke the first async method of my program if I built say a message loop where I want some control of the degree of parallelism?

It's easiest to put the framework in charge of this. E.g., you can just return a Task<T> from a WebAPI action, and the framework understands that. Similarly, UI applications have a message loop built-in that async will work naturally with.

If you have a situation where the framework doesn't understand Task or have a built-in message loop (usually a Console application or a Win32 service), you can use the AsyncContext type in my AsyncEx library. AsyncContext just installs a "main loop" (that is compatible with async) onto the current thread.

Since the HttpClient doesn't have any synchronous methods, what can I safely presume to do if I have an abstraction that isn't async aware?

The correct approach is to change the abstraction. Do not attempt to block on asynchronous code; I describe that common deadlock scenario in detail on my blog.

You change the abstraction by making it async-friendly. For example, change IUserContext IUserContextService.GetUserContext() to Task<IUserContext> IUserContextService.GetUserContextAsync().

I've read about the ConfigureAwait(false) but I don't really understand what it does. It's strange to me that it's set after the async invocation.

You may find my async intro helpful. I won't say much more about ConfigureAwait in this answer because I think it's not directly applicable to a good solution for this question (but I'm not saying it's bad; it actually should be used unless you can't use it).

Just bear in mind that async is an operator with precedence rules and all that. It feels magical at first, but it's really not so much. This code:

var result = await httpClient.GetAsync(url).ConfigureAwait(false);

is exactly the same as this code:

var asyncOperation = httpClient.GetAsync(url).ConfigureAwait(false);
var result = await asyncOperation;

There are usually no race conditions in async code because - even though the method is asynchronous - it is also sequential. The method can be paused at an await, and it will not be resumed until that await completes.

When you have a message loop that allow things to happen in parallel and you have asynchronous methods, there's a opportunity to minimize latency.

This is the second time you've mentioned a "message loop" "in parallel", but I think what you actually want is to have multiple (asynchronous) consumers working off the same queue, correct? That's easy enough to do with async (note that there is just a single message loop on a single thread in this example; when everything is async, that's usually all you need):

await tasks.WhenAll(ConsumerAsync(), ConsumerAsync(), ConsumerAsync());

async Task ConsumerAsync()
{
  for (;;) // TODO: consider a CancellationToken for orderly shutdown
  {
    var m = await mq.ReceiveAsync();
    var c = GetCommand(m);
    await c.InvokeAsync();
    m.Delete();
  }
}

// Extension method
public static Task<Message> ReceiveAsync(this MessageQueue mq)
{
  return Task<Message>.Factory.FromAsync(mq.BeginReceive, mq.EndReceive, null);
}

You'd probably also be interested in TPL Dataflow. Dataflow is a library that understands and works well with async code, and has nice parallel options built-in.

like image 114
Stephen Cleary Avatar answered Dec 11 '25 17:12

Stephen Cleary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!