Hangfire icon indicating copy to clipboard operation
Hangfire copied to clipboard

Hangfire.Storage.DistributedLockTimeoutException: Timeout expired

Open trihanhcie opened this issue 6 years ago • 22 comments

Hi, I'm using Hangfire with Mssql. I keep having timeout issues on recurring jobs.

Hangfire.Storage.DistributedLockTimeoutException Timeout expired. The timeout elapsed prior to obtaining a distributed lock on the 'HangFire:HangfireBootstrapper.BackgroundTaskRunner.RunAll' resource.

Hangfire.Storage.DistributedLockTimeoutException: Timeout expired. The timeout elapsed prior to obtaining a distributed lock on the 'HangFire:HangfireBootstrapper.BackgroundTaskRunner.RunAll' resource. at Hangfire.SqlServer.SqlServerDistributedLock.Acquire(IDbConnection connection, String resource, TimeSpan timeout) at Hangfire.SqlServer.SqlServerConnection.AcquireLock(String resource, TimeSpan timeout) at Hangfire.SqlServer.SqlServerConnection.AcquireDistributedLock(String resource, TimeSpan timeout) at Hangfire.DisableConcurrentExecutionAttribute.OnPerforming(PerformingContext filterContext) at Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func`1 continuation)

Any idea how to fix that?

Thanks

trihanhcie avatar Jan 31 '19 04:01 trihanhcie

I am having the same issue. I have this attribute on my method:

[DisableConcurrentExecution(timeoutInSeconds: 10 * 60)]

In my case it is a recurring task that executes every hour.

VesselinVassilev avatar Feb 07 '19 01:02 VesselinVassilev

Having the same problem over here. Is there some configuration missing? Am using RavenDB for storage.

Hangfire.Server.DelayedJobScheduler:Debug: An exception was thrown during acquiring distributed lock on the locks:schedulepoller resource within 60 seconds. The scheduled jobs have not been handled this time. It will be retried in 15 seconds

Hangfire.Storage.DistributedLockTimeoutException: Timeout expired. The timeout elapsed prior to obtaining a distributed lock on the 'HangFire/locks:schedulepoller' resource. at Hangfire.Raven.DistributedLocks.RavenDistributedLock.Acquire(TimeSpan timeout) at Hangfire.Raven.DistributedLocks.RavenDistributedLock..ctor(RavenStorage storage, String resource, TimeSpan timeout, RavenStorageOptions options) at Hangfire.Raven.RavenConnection.AcquireDistributedLock(String resource, TimeSpan timeout) at Hangfire.Server.DelayedJobScheduler.UseConnectionDistributedLock[T](JobStorage storage, Func`2 action) Hangfire.Server.RecurringJobScheduler:Debug: An exception was thrown during acquiring distributed lock the recurring-jobs:lock resource within 60 seconds. The recurring jobs have not been handled this time.

Hangfire.Storage.DistributedLockTimeoutException: Timeout expired. The timeout elapsed prior to obtaining a distributed lock on the 'HangFire/recurring-jobs:lock' resource. at Hangfire.Raven.DistributedLocks.RavenDistributedLock.Acquire(TimeSpan timeout) at Hangfire.Raven.DistributedLocks.RavenDistributedLock..ctor(RavenStorage storage, String resource, TimeSpan timeout, RavenStorageOptions options) at Hangfire.Raven.RavenConnection.AcquireDistributedLock(String resource, TimeSpan timeout) at Hangfire.Server.RecurringJobScheduler.UseConnectionDistributedLock(JobStorage storage, Func`2 action)

Echarnus avatar May 17 '19 13:05 Echarnus

Hi has anyone found a solution to this yet?

FrenchMasta avatar Aug 14 '19 06:08 FrenchMasta

Happening for me too. I wish to find a solution to this. Again, it's only for long running jobs that run into each other.

kashyapus avatar Nov 22 '19 17:11 kashyapus

Having the same issue w a Long running job

davidrevoledo avatar Feb 04 '20 23:02 davidrevoledo

Set QueuePollInterval to 30 and it resolved the issue for me.

kashyapus avatar Feb 05 '20 00:02 kashyapus

@kashyapus u mean QueuePollInterval = TimeSpan.FromSeconds(30) (30 seconds or other value u meant? Thanks in advance for answer :)

urbankrzysztof avatar Mar 04 '20 08:03 urbankrzysztof

@kashyapus u mean QueuePollInterval = TimeSpan.FromSeconds(30) (30 seconds or other value u meant? Thanks in advance for answer :)

Yes you are correct. TimeSpan.FromSeconds(30)

kashyapus avatar Mar 04 '20 17:03 kashyapus

What does QueuePollInterval when set to a higher value actually do? Does it decrease the amount of polls per timespan?

MTrachsel avatar Jan 27 '21 15:01 MTrachsel

Did anyone manage to resolve this?

I tried increasing QueuePollInterval to 35seconds and still getting this issue.

weirdyang avatar Jun 14 '21 06:06 weirdyang

Any feedback on this? We are also getting this error on a recurring job.

JAspeling avatar Jun 25 '21 08:06 JAspeling

For my case, it was because I called app.UseHangfireServer(), when I've already added services.AddHangfireServer(); in the configure method above. Removing that line solved this issue for me.

weirdyang avatar Jun 25 '21 08:06 weirdyang

Everything works perfectly in development mode; this only happens to me when I deploy my service to the service. I have the following configuration: [AutomaticRetry(Attempts = 2)] [DisableConcurrentExecution(0)]

MustafaModern avatar Jan 17 '22 15:01 MustafaModern

the same to me , I also got this issue in the Production environment:

Stopped program because of exception||Hangfire.Storage.DistributedLockTimeoutException: Timeout expired. The timeout elapsed prior to obtaining a distributed lock on the 'HangFire:lock:recurring-job:PeriodicCleanupFileStorageJob' resource.
   at Hangfire.SqlServer.SqlServerDistributedLock.Acquire(IDbConnection connection, String resource, TimeSpan timeout)
   at Hangfire.SqlServer.SqlServerConnection.AcquireLock(String resource, TimeSpan timeout)
   at Hangfire.SqlServer.SqlServerConnection.AcquireDistributedLock(String resource, TimeSpan timeout)
   at Hangfire.RecurringJobExtensions.AcquireDistributedRecurringJobLock(IStorageConnection connection, String recurringJobId, TimeSpan timeout)
   at Hangfire.RecurringJobManager.AddOrUpdate(String recurringJobId, Job job, String cronExpression, RecurringJobOptions options)
   at Hangfire.RecurringJobManagerExtensions.AddOrUpdate(IRecurringJobManager manager, String recurringJobId, Job job, String cronExpression, TimeZoneInfo timeZone, String queue)

hippieZhou avatar Aug 24 '22 01:08 hippieZhou

I'm getting the same errors as the people above.

@odinserj Can you please look into it? It's quite annoying and it's basically breaking our entire environment.

mortenmoulder avatar Aug 29 '22 13:08 mortenmoulder

Please try to use Microsoft.Data.SqlClient package of the latest version instead as shown here – https://github.com/HangfireIO/Hangfire/issues/1735#issuecomment-831913142. There are strange problems with connections in System.Data.SqlClient that's solved by using MD.SqlClient, and connection can hold locks.

odinserj avatar Aug 29 '22 13:08 odinserj

All the recent problems with SQL Server were fixed by using that new package, some reported to support, and one of them is #2065. So there's a high chance this problem can also be resolved by a simple upgrade.

odinserj avatar Aug 29 '22 13:08 odinserj

Did you resolve it? I am trying call service from service and receive error too.

Excalib88 avatar Mar 07 '23 11:03 Excalib88

Everything works perfectly in development mode; this only happens to me when I deploy my service to the service. I have the following configuration: [AutomaticRetry(Attempts = 2)] [DisableConcurrentExecution(0)] @MustafaModern Have you found solution to your problem ?

nicpon avatar Mar 17 '23 14:03 nicpon

@odinserj i am facing the same issue "Hangfire.Storage.DistributedLockTimeoutException Timeout expired. The timeout elapsed prior to obtaining a distributed lock on the resource."

can you please let me know if this issue is fixed or not. Using attributes like [AutomaticRetry(Attempts = 0)] [DisableConcurrentExecution(timeoutInSeconds: 30)]

ChetanChandan avatar Jul 11 '23 13:07 ChetanChandan

I was having the same issue, there's nothing wrong with the application, it's just that this is a recurring job that runs once per 15 minutes. The programmer added the attribute to ensure that only 1 instance is running in case if the job runs for like an hour. if the next job runs before the first job finish, you will see that error in the hangfire admin site

nycgavin avatar Jul 27 '23 12:07 nycgavin