Skip to content

Commit

Permalink
Switching back to CancellationEvent usage instead of CancellationToke…
Browse files Browse the repository at this point in the history
…n.WaitHandle

Closes #2447
  • Loading branch information
odinserj committed Nov 26, 2024
1 parent 49a8023 commit 6316cac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/Hangfire.Core/Common/CancellationTokenExtentions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public static bool Wait(this CancellationToken cancellationToken, TimeSpan timeo
var elapsedThreshold = TimeSpan.FromMilliseconds(500);
var protectionTime = TimeSpan.FromSeconds(1);

// There was a precedent of the following message logged when CancellationToken.WaitHandle
// was used directly, instead of having our custom CancellationEvent class used, please see
// https://github.com/HangfireIO/Hangfire/issues/2447
if (!cancellationToken.IsCancellationRequested &&
timeout >= timeoutThreshold &&
stopwatch.Elapsed < elapsedThreshold)
Expand Down
11 changes: 7 additions & 4 deletions src/Hangfire.SqlServer/SqlServerJobQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Linq;
using System.Threading;
using Hangfire.Annotations;
using Hangfire.Common;
using Hangfire.Storage;

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

var waitArray = GetWaitArrayForQueueSignals(_storage, queues, cancellationToken);
using var cancellationEvent = cancellationToken.GetCancellationEvent();
var waitArray = GetWaitArrayForQueueSignals(_storage, queues, cancellationEvent);

SemaphoreSlim semaphore = null;

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

var waitArray = GetWaitArrayForQueueSignals(_storage, queues, cancellationToken);
using var cancellationEvent = cancellationToken.GetCancellationEvent();
var waitArray = GetWaitArrayForQueueSignals(_storage, queues, cancellationEvent);

while (!cancellationToken.IsCancellationRequested)
{
Expand Down Expand Up @@ -305,11 +308,11 @@ private static DbCommand CreateTransactionalFetchCommand(
.AddExpandedParameter("@queues", queues, DbType.String);
}

private static WaitHandle[] GetWaitArrayForQueueSignals(SqlServerStorage storage, string[] queues, CancellationToken cancellationToken)
private static WaitHandle[] GetWaitArrayForQueueSignals(SqlServerStorage storage, string[] queues, CancellationTokenExtentions.CancellationEvent cancellationEvent)
{
var waitList = new List<WaitHandle>(capacity: queues.Length + 1)
{
cancellationToken.WaitHandle
cancellationEvent.WaitHandle
};

foreach (var queue in queues)
Expand Down

0 comments on commit 6316cac

Please sign in to comment.