32

Per the Hangfire 0.8.2 announcement post, Hangfire has a DisableConcurrentExecution filter which, when applied to a method, prevents multiple instances of the method from executing concurrently.

The DisableConcurrentExecution filter takes a timeoutInSeconds int parameter. From the example in the linked article:

[DisableConcurrentExecution(timeoutInSeconds: 10 * 60)]
public void SomeMethod()
{
    // Operations performed inside a distributed lock
}

My question is: What happens when, given a job which is waiting on obtaining the lock for a DisableConcurrentExecution-filtered method, the time that the job has been waiting exceeds the timeoutInSeconds value?

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
  • 1
    Note: If you have tasks waiting they'll block the worker threads waiting for the lock to be released. So be careful if you rely deliberately or accidentally on this attribute for running items in sequence because you may be preventing OTHER tasks from running too. – Simon_Weaver Jan 18 '21 at 20:56

1 Answers1

44

I tested that recently. That job instance was recorded as failed in the dashboard and listed the exception which indicated that the timeout had expired while waiting for an exclusive lock.

You'll see the following exception:

Hangfire.Storage.DistributedLockTimeoutException: Timeout expired. The timeout elapsed prior to obtaining a distributed lock on the 'xxx' resource.
    at Hangfire.SqlServer.SqlServerDistributedLock.Acquire(IDbConnection connection, String resource, TimeSpan timeout)
Alpha
  • 7,586
  • 8
  • 59
  • 92
Nat Wallbank
  • 1,377
  • 12
  • 12
  • 3
    Here you go, more detailed exception information (apologies for lack of formatting): Hangfire.Storage.DistributedLockTimeoutException: Timeout expired. The timeout elapsed prior to obtaining a distributed lock on the 'xxx' resource. at Hangfire.SqlServer.SqlServerDistributedLock.Acquire(IDbConnection connection, String resource, TimeSpan timeout) – Nat Wallbank Nov 10 '16 at 10:10
  • I'm having a very similar issue, its a common issue of recurring jobs, its so sad... – Richard Lee Sep 05 '17 at 22:44
  • Is there any easier way to avoid this error exception? in case we need to not need re-try – niz_sh Aug 23 '23 at 05:35