Poul K. Sørensen
Poul K. Sørensen

Reputation: 17540

Async/await in azure worker role causing the role to recycle

I am playing around with Tasks, Async and await in my WorkerRole (RoleEntryPoint).

I had some unexplained recycles and i have found out now that if something is running to long in a await call, the role recycles. To reproduce it, just do a await Task.Delay(60000) in the Run method.

Anyone who can explain to me why?

Upvotes: 13

Views: 4042

Answers (2)

Stephen Cleary
Stephen Cleary

Reputation: 456747

The Run method must block. From the docs:

If you do override the Run method, your code should block indefinitely. If the Run method returns, the role is automatically recycled by raising the Stopping event and calling the OnStop method so that your shutdown sequences may be executed before the role is taken offline.

A simple solution is to just do this:

public override void Run()
{
  RunAsync().Wait();
}

public async Task RunAsync()
{
  while (true)
  {
    await Task.Delay(60000);
  }
}

Alternatively, you can use AsyncContext from my AsyncEx library:

public override void Run()
{
  AsyncContext.Run(async () =>
  {
    while (true)
    {
      await Task.Delay(60000);
    }
  });
}

Whichever option you choose, Run should not be async. It's kind of like Main for a Console app (see my blog for why async Main is not allowed).

Upvotes: 22

Pascal
Pascal

Reputation: 1141

I would recommend a lower value for Task.Delay like 1000 (ms). I suspect that the worker role cannot respond quickly enough to the health check. The role is then considered unresponsive and restarted.

Make sure the Run method never returns with something like this

while (true)
{               
    Thread.Sleep(1000);
}

Or with Task.Delay in your case.

Upvotes: 0

Related Questions