HangfireIO / HangfireIO/Hangfire
SqlServerDistributedLockException: Could not place a lock on the resource 'Task': The lock request timed out.
- Dominant language
- C#
- Stars
- 10.1k
- Forks
- 1.8k
- Avg merge
- 1h 19m
- Merged PRs (30d)
- 1
Description
Simple reproduction of this:
```
public class HomeController : Controller
{
public string Run()
{
for (int i = 0; i < 2; i++)
BackgroundJob.Enqueue(() => Services.Service1.Task());
return "ok";
}
}
public class Service1
{
[DisableConcurrentExecution(timeoutInSeconds: 5)]
[AutomaticRetry(Attempts = 0)]
public static void Task()
{
string guid = Guid.NewGuid().ToString();
string contents = guid + " started\n";
System.IO.File.AppendAllText("D:\\logs\\file1", contents);
System.Threading.Thread.Sleep(1000 * 10);
System.IO.File.AppendAllText("D:\\logs\\file1", guid + " finished\n");
}
}
```
The second job returns of course exception
Hangfire.SqlServer.SqlServerDistributedLockException
> Could not place a lock on the resource 'HangFire:Service1.Task': The lock request timed out.
>
> Hangfire.SqlServer.SqlServerDistributedLockException: Could not place a lock on the resource 'HangFire:Service1.Task': The lock request timed out.
> at Hangfire.SqlServer.SqlServerDistributedLock..ctor(String resource, TimeSpan timeout, IDbConnection connection)
> at Hangfire.SqlServer.SqlServerConnection.AcquireDistributedLock(String resource, TimeSpan timeout)
> at Hangfire.DisableConcurrentExecutionAttribute.OnPerforming(PerformingContext filterContext)
> at Hangfire.Server.DefaultJobPerformanceProcess.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func`1 continuation)
I already mentioned this on hangfire forum: https://discuss.hangfire.io/t/disableconcurentexecution-does-not-appear-to-work/158/3
Haven't debugged it, but talked with a colleague and it should work like this:
- both jobs are scheduled
- first job enters execution
- second job wants to execute, but like first one has DisableConcurrentExecution(timeoutInSeconds: 5). Second job waits for 5 seconds before it can try to run it. After 5 seconds have passed, it tries to run it but fail, because distributed lock is still not free (the first job is stil executing).
I would be great to describe **DisableConcurrentExecution behaviour and functionality in documentation**. Otherwise it's hard to understand how does this attribute affect job execution. It would really be helpful for anyone using this attribute.
Contributor guide
Assessment
This issue has not been assessed yet.