Aspnetcore.docs: What happens with IHostedService.StopAsync when the cancellationToken is set cancelled?

Created on 21 Oct 2018  ·  14Comments  ·  Source: dotnet/AspNetCore.Docs

Issue description

The documentation for IHostedService.StopAsync doesn't explain what the parameter is used for and what happens when it is used. Will the host just ignore these services and quit anyway? Or is this just an indication that we should hurry up? I don't know what to do with this parameter in my IHostedService implementation.

Software versions

Check the .NET target framework(s) being used, and include the version number(s).

  • [x] .NET Core 2.1
  • [ ] .NET Framework
  • [ ] .NET Standard
P3 Source - Docs.ms

Most helpful comment

I opened https://github.com/aspnet/Docs/issues/9727 to add HostOptions to the Generic Host topic.

All 14 comments

@Tratcher

StopAsync is intended for a graceful shutdown. The cancellationToken is used to indicate that your grace period has expired, you should abort any remaining operations, and return promptly.

So if the ongoing tasks, even if I pass the cancellation on to them, won’t complete anytime soon, they’re still “allowed” to complete and won’t be left behind by not waiting anymore before terminating the process?

And as long as the cancellation token isn’t set, the host happily agrees with my service taking some time and there’s no need to hurry up and prefer ending things in an unsafe way?

That means that I’ll just block the StopAsync method as long as activity is still going on, not doing anything else, and only when the cancellation token is set, I’ll notify these tasks to hurry up. Is that correct? And in anyway, it takes as long as it takes.

Right, the caller does not abandon tasks, it will wait for you.

FYI there's a default 5 second timeout the host sets on that cancellationToken.

Ok, thanks for the clarification. Might be helpful to read that in the documentation.

Thanks @ygoe ... I'll see what I can do here. I'm 🏃🏃🏃 at the moment, so it will probably take a few weeks (_or months!_ 😅). I'll get to this asap.

@Tratcher / @scottaddie do you know if there is any way to extend this 5 seconds time before the Cancellation is set to true?

@Tratcher thanks for the answer! I am using the IHost, thanks for the HostOptions reference, at the end I've used something like this :
services.AddSingleton<IOptions<HostOptions>>(provider => new OptionsWrapper<HostOptions>( new HostOptions() { ShutdownTimeout = TimeSpan.FromSeconds(long.Parse(GracefulPeriodInSeconds)) }));

I opened https://github.com/aspnet/Docs/issues/9727 to add HostOptions to the Generic Host topic.

services.Configure<HostOptions> should be sufficient.

Right, the caller does not abandon tasks, it will wait for you.

This is true for IHostedService, where your own implementation determines what happens. Note that for BackgroundService the implementation uses the cancellation token, resulting in a TaskCancelledException once shutdown is no longer graceful.

Hi,

I'm trying to test IHostedService.StopAsnc() method to understand more about. Can someone help me on how to trigger a situation where StopAsync() will execute.

The host has a timeout that will trip that CT, it's 5 seconds by default.

Was this page helpful?
0 / 5 - 0 ratings