Skip to content

Commit 6316cac

Browse files
committed
Switching back to CancellationEvent usage instead of CancellationToken.WaitHandle
Closes #2447
1 parent 49a8023 commit 6316cac

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

Diff for: src/Hangfire.Core/Common/CancellationTokenExtentions.cs

+3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public static bool Wait(this CancellationToken cancellationToken, TimeSpan timeo
6666
var elapsedThreshold = TimeSpan.FromMilliseconds(500);
6767
var protectionTime = TimeSpan.FromSeconds(1);
6868

69+
// There was a precedent of the following message logged when CancellationToken.WaitHandle
70+
// was used directly, instead of having our custom CancellationEvent class used, please see
71+
// https://github.com/HangfireIO/Hangfire/issues/2447
6972
if (!cancellationToken.IsCancellationRequested &&
7073
timeout >= timeoutThreshold &&
7174
stopwatch.Elapsed < elapsedThreshold)

Diff for: src/Hangfire.SqlServer/SqlServerJobQueue.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using System.Linq;
2323
using System.Threading;
2424
using Hangfire.Annotations;
25+
using Hangfire.Common;
2526
using Hangfire.Storage;
2627

2728
// ReSharper disable RedundantAnonymousTypePropertyName
@@ -124,7 +125,8 @@ private SqlServerTimeoutJob DequeueUsingSlidingInvisibilityTimeout(string[] queu
124125
var queuesString = String.Join("_", queues.OrderBy(static x => x));
125126
var resource = Tuple.Create(_storage, queuesString);
126127

127-
var waitArray = GetWaitArrayForQueueSignals(_storage, queues, cancellationToken);
128+
using var cancellationEvent = cancellationToken.GetCancellationEvent();
129+
var waitArray = GetWaitArrayForQueueSignals(_storage, queues, cancellationEvent);
128130

129131
SemaphoreSlim semaphore = null;
130132

@@ -221,7 +223,8 @@ private SqlServerTransactionJob DequeueUsingTransaction(string[] queues, Cancell
221223
? _options.QueuePollInterval
222224
: TimeSpan.FromSeconds(1);
223225

224-
var waitArray = GetWaitArrayForQueueSignals(_storage, queues, cancellationToken);
226+
using var cancellationEvent = cancellationToken.GetCancellationEvent();
227+
var waitArray = GetWaitArrayForQueueSignals(_storage, queues, cancellationEvent);
225228

226229
while (!cancellationToken.IsCancellationRequested)
227230
{
@@ -305,11 +308,11 @@ private static DbCommand CreateTransactionalFetchCommand(
305308
.AddExpandedParameter("@queues", queues, DbType.String);
306309
}
307310

308-
private static WaitHandle[] GetWaitArrayForQueueSignals(SqlServerStorage storage, string[] queues, CancellationToken cancellationToken)
311+
private static WaitHandle[] GetWaitArrayForQueueSignals(SqlServerStorage storage, string[] queues, CancellationTokenExtentions.CancellationEvent cancellationEvent)
309312
{
310313
var waitList = new List<WaitHandle>(capacity: queues.Length + 1)
311314
{
312-
cancellationToken.WaitHandle
315+
cancellationEvent.WaitHandle
313316
};
314317

315318
foreach (var queue in queues)

0 commit comments

Comments
 (0)