Skip to content

Commit 96dbbc7

Browse files
author
Vytautas Kasparavičius
committed
Solved possible deadlock because of PostgreSqlConnection.SetRangeInHash isolation level solution.
1 parent 7118c2d commit 96dbbc7

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

src/Hangfire.PostgreSql/Hangfire.PostgreSql.csproj

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
<PackageReleaseNotes>https://github.com/frankhommers/Hangfire.PostgreSql/releases</PackageReleaseNotes>
1515
<PackageProjectUrl>http://hmm.rs/Hangfire.PostgreSql</PackageProjectUrl>
1616
<PackageLicenseUrl>https://raw.github.com/frankhommers/Hangfire.PostgreSql/master/LICENSE.md</PackageLicenseUrl>
17-
<Version>1.4.8</Version>
18-
<FileVersion>1.4.8.0</FileVersion>
19-
<AssemblyVersion>1.4.8.0</AssemblyVersion>
17+
<Version>1.4.8.1</Version>
18+
<FileVersion>1.4.8.1</FileVersion>
19+
<AssemblyVersion>1.4.8.1</AssemblyVersion>
20+
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
2021
</PropertyGroup>
2122

2223
<ItemGroup>
@@ -25,7 +26,7 @@
2526

2627
<ItemGroup>
2728
<PackageReference Include="Dapper" Version="1.50.2" />
28-
<PackageReference Include="Hangfire.Core" Version="1.6.12" />
29+
<PackageReference Include="Hangfire.Core" Version="1.6.14" />
2930
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
3031
<PackageReference Include="Npgsql" Version="3.2.2" />
3132
</ItemGroup>

src/Hangfire.PostgreSql/PostgreSqlConnection.cs

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using System;
2323
using System.Collections.Generic;
2424
using System.Data;
25+
using System.Diagnostics;
2526
using System.Globalization;
2627
using System.Linq;
2728
using System.Threading;
@@ -325,6 +326,7 @@ SELECT 1
325326
);
326327
";
327328
var execute = true;
329+
var executionTimer = Stopwatch.StartNew();
328330
while (execute)
329331
{
330332
try
@@ -344,6 +346,9 @@ SELECT 1
344346
if (!exception.SqlState.Equals("40001"))
345347
throw;
346348
}
349+
350+
if(executionTimer.Elapsed > _options.TransactionSynchronisationTimeout)
351+
throw new TimeoutException("SetRangeInHash experienced timeout while trying to execute transaction");
347352
}
348353
}
349354

src/Hangfire.PostgreSql/PostgreSqlStorageOptions.cs

+18-6
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,23 @@ public class PostgreSqlStorageOptions
2828
private TimeSpan _queuePollInterval;
2929
private TimeSpan _invisibilityTimeout;
3030
private TimeSpan _distributedLockTimeout;
31+
private TimeSpan _transactionSerializationTimeout;
3132

3233
public PostgreSqlStorageOptions()
3334
{
3435
QueuePollInterval = TimeSpan.FromSeconds(15);
3536
InvisibilityTimeout = TimeSpan.FromMinutes(30);
3637
DistributedLockTimeout = TimeSpan.FromMinutes(10);
38+
TransactionSynchronisationTimeout = TimeSpan.FromMilliseconds(500);
3739
SchemaName = "hangfire";
3840
UseNativeDatabaseTransactions = true;
3941
PrepareSchemaIfNecessary = true;
4042
}
4143

4244
public TimeSpan QueuePollInterval
4345
{
44-
get { return _queuePollInterval; }
45-
set
46+
get => _queuePollInterval;
47+
set
4648
{
4749
ThrowIfValueIsNotPositive(value, nameof(QueuePollInterval));
4850
_queuePollInterval = value;
@@ -51,8 +53,8 @@ public TimeSpan QueuePollInterval
5153

5254
public TimeSpan InvisibilityTimeout
5355
{
54-
get { return _invisibilityTimeout; }
55-
set
56+
get => _invisibilityTimeout;
57+
set
5658
{
5759
ThrowIfValueIsNotPositive(value, nameof(InvisibilityTimeout));
5860
_invisibilityTimeout = value;
@@ -61,14 +63,24 @@ public TimeSpan InvisibilityTimeout
6163

6264
public TimeSpan DistributedLockTimeout
6365
{
64-
get { return _distributedLockTimeout; }
65-
set
66+
get => _distributedLockTimeout;
67+
set
6668
{
6769
ThrowIfValueIsNotPositive(value, nameof(DistributedLockTimeout));
6870
_distributedLockTimeout = value;
6971
}
7072
}
7173

74+
public TimeSpan TransactionSynchronisationTimeout
75+
{
76+
get => _transactionSerializationTimeout;
77+
set
78+
{
79+
ThrowIfValueIsNotPositive(value, nameof(TransactionSynchronisationTimeout));
80+
_transactionSerializationTimeout = value;
81+
}
82+
}
83+
7284
public bool UseNativeDatabaseTransactions { get; set; }
7385
public bool PrepareSchemaIfNecessary { get; set; }
7486
public string SchemaName { get; set; }

0 commit comments

Comments
 (0)