Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Service running Async code not waiting on work to complete

In Brief

I have a Windows Service that executes several jobs as async Tasks in parallel. However, when the OnStop is called, it seems that these are all immediately terminated instead of being allowed to stop in a more gracious manner.

In more detail

Each job represents an iteration of work, so having completed its work the job then needs to run again.

To accomplish this, I am writing a proof-of-concept Windows Service that:

  • runs each job as an awaited async TPL Task (these are all I/O bound tasks)
  • each job is run iteratively within a loop
  • each job's loop is run in parallel

When I run the Service, I see everything executing as I expect. However, when I Stop the service, it seems that everything stops dead.

Okay - so how is this working?

In the Service I have a cancellation token, and a TaskCompletion Source:

private static CancellationTokenSource _cancelSource = new CancellationTokenSource();
private TaskCompletionSource<bool> _jobCompletion = new TaskCompletionSource<bool>();
private Task<bool> AllJobsCompleted { get { return _finalItems.Task; } }

The idea is that when every Job has gracefully stopped, then the Task AllJobsCompleted will be marked as completed.

The OnStart simply starts running these jobs:

protected override async void OnStart(string[] args)
{
    _cancelSource = new CancellationTokenSource();  
    var jobsToRun = GetJobsToRun(); // details of jobs not relevant 
    Task.Run(() => this.RunJobs(jobsToRun, _cancelSource.Token).ConfigureAwait(false), _cancelSource.Token);
}

The Task RunJobs will run each job in a parallel loop:

private async Task RunModules(IEnumerable<Jobs> jobs, CancellationToken cancellationToken)
{
    var parallelOptions = new ParallelOptions { CancellationToken = cancellationToken };    
    int jobsRunningCount = jobs.Count();
    object lockObject = new object();

    Parallel.ForEach(jobs, parallelOptions, async (job, loopState) =>
    {
        try
        {
            do
            {
                await job.DoWork().ConfigureAwait(false); // could take 5 seconds
                parallelOptions.CancellationToken.ThrowIfCancellationRequested();
            }while(true);
        }
        catch(OperationCanceledException)
        {
            lock (lockObject) { jobsRunningCount --; }
        }
    }); 

    do
    {
        await Task.Delay(TimeSpan.FromSeconds(5));
    } while (modulesRunningCount > 0);

    _jobCompletion.SetResult(true);
}

So, what should be happening is that when each job finishes its current iteration, it should see that the cancellation has been signalled and it should then exit the loop and decrement the counter.

Then, when jobsRunningCount reaches zero, then we update the TaskCompletionSource. (There may be a more elegant way of achieving this...)

So, for the OnStop:

protected override async void OnStop()
{
    this.RequestAdditionalTime(100000); // some large number        
    _cancelSource.Cancel();     
    TraceMessage("Task cancellation requested."); // Last thing traced

    try
    {
        bool allStopped = await this.AllJobsCompleted;          
        TraceMessage(string.Format("allStopped = '{0}'.", allStopped));
    }
    catch (Exception e)
    {
        TraceMessage(e.Message);
    }
} 

What what I expect is this:

  1. Click [STOP] on the Service
  2. The Service should take sometime to stop
  3. I should see a trace statement "Task cancellation requested."
  4. I should see a trace statement saying either "allStopped = true", or the exception message

And when I debug this using a WPF Form app, I get this.

However, when I install it as a service:

  1. Click [STOP] on the Service
  2. The Service stops almost immediately
  3. I only see the trace statement "Task cancellation requested."

What do I need to do to ensure the OnStop doesn't kill off my parallel async jobs and waits for the TaskCompletionSource?

like image 967
DrGriff Avatar asked Mar 20 '15 14:03

DrGriff


People also ask

What happens if an async method is not awaited?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.

How do you call async method without await?

@Servy async creates a state machine that manages any awaits within the async method. If there are no await s within the method it still creates that state machine--but the method is not asynchronous. And if the async method returns void , there's nothing to await. So, it's more than just awaiting a result.

Can you use async without await C?

Consider Using async without await. think that maybe you misunderstand what async does. The warning is exactly right: if you mark your method async but don't use await anywhere, then your method won't be asynchronous. If you call it, all the code inside the method will execute synchronously.

Does async create a new thread?

The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active.


1 Answers

Your problem is that OnStop is async void. So, when it does await this.AllJobsCompleted, what actually happens is that it returns from OnStop, which the SCM interprets as having stopped, and terminates the process.

This is one of the rare scenarios where you'd need to block on a task, because you cannot allow OnStop to return until after the task completes.

This should do it:

protected override void OnStop()
{
  this.RequestAdditionalTime(100000); // some large number        
  _cancelSource.Cancel();     
  TraceMessage("Task cancellation requested."); // Last thing traced

  try
  {
    bool allStopped = this.AllJobsCompleted.GetAwaiter().GetResult();          
    TraceMessage(string.Format("allStopped = '{0}'.", allStopped));
  }
  catch (Exception e)
  {
    TraceMessage(e.Message);
  }
}
like image 191
Stephen Cleary Avatar answered Sep 23 '22 19:09

Stephen Cleary