TaskCanceledException during shutdown


I am trying to setup Hangfire in a way so my long running jobs (About 1 min) will have time to finish during a shutdown.
I have set the ShutdownTimeout and StopTimeout to 1 min but if a job is running during shutdown, I get a TaskCancelledException exactly 30 seconds after the shutdown signal.
Why is that?
I cannot find any timers in the source that should trigger something after 30 sec, and I guess an unhandled exception is a bug somehow?

Hangfire is configured like this:

.AddHangfireServer(options =>
    options.WorkerCount = 5;
    options.SchedulePollingInterval = TimeSpan.FromMilliseconds(500);
    options.ShutdownTimeout = TimeSpan.FromSeconds(60);
    options.StopTimeout  = TimeSpan.FromSeconds(60);

This is a screenshot of a job that should take 1 minute and the shutdown is triggered after 3 sec:


Please also try to extend the .NET’s HostOptions.ShutdownTimeout as written in this article - Extending the shutdown timeout setting to ensure graceful IHostedService shutdown (andrewlock.net).

1 Like

Thanks a lot… That seemed to do the trick… :+1: