Open fgheysels opened 3 years ago
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.
Best label would be area-Extensions-Hosting imho.
Tagging subscribers to this area: @eerhardt, @maryamariyan See info in area-owners.md if you want to be subscribed.
Author: | fgheysels |
---|---|
Assignees: | - |
Labels: | `untriaged`, `area-Extensions-Hosting` |
Milestone: | - |
It is the same for StartAsync
, they are started sequentially and not in parallel.
In your above code example, if you call base.StopAsync
right away, your ExecuteAsync
method will stop running right away:
public override async Task StopAsync(CancellationToken cancellationToken)
{
Console.WriteLine("stop called for 2");
await base.StopAsync(cancellationToken);
await Task.Delay(TimeSpan.FromSeconds(10));
Console.WriteLine("stop 2 finished");
}
Concurrent StopAsync was implemented in https://github.com/dotnet/runtime/pull/84048. I suggest closing this issue as a duplicate of https://github.com/dotnet/runtime/issues/68036.
I've noticed that, if you have multiple
BackgroundServices
, theStopAsync
method of those services is not called in parallel when the host is stopping. They're rather called in a blocking fashion.Suppose:
And we have these backgroundservices:
When running the application, and then pressing ctrl+c, we see this output:
This means that the
StopAsync
method for the last registered backgroundservice is called first, and the runtime is waiting for thisStopAsync
method to finish beforeStopAsync
for the other worker is called.Is there a reason why the
StopAsync
methods are not called in parallel, and all the returned tasks are awaited ? Something like this ? (pseudo-code off course)