Skip to content

Commit

Permalink
Revert "Prefer using CancellationToken.WaitHandle again"
Browse files Browse the repository at this point in the history
This reverts commit 4ced644.
  • Loading branch information
odinserj committed Nov 26, 2024
1 parent 9685dd9 commit 49a8023
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/Hangfire.Core/Common/CancellationTokenExtentions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public static class CancellationTokenExtentions
/// on cancellation token registration and avoids using the <see cref="CancellationToken.WaitHandle"/>
/// property as it may lead to high CPU issues.
/// </summary>
[Obsolete("CancellationToken.WaitHandle is now preferred, since early days of .NET Core passed. Will be removed in 2.0.0.")]
public static CancellationEvent GetCancellationEvent(this CancellationToken cancellationToken)
{
return new CancellationEvent(cancellationToken);
Expand Down Expand Up @@ -57,8 +56,10 @@ public static void WaitOrThrow(this CancellationToken cancellationToken, TimeSpa
/// </summary>
public static bool Wait(this CancellationToken cancellationToken, TimeSpan timeout)
{
using var cancellationEvent = GetCancellationEvent(cancellationToken);

var stopwatch = Stopwatch.StartNew();
var waitResult = cancellationToken.WaitHandle.WaitOne(timeout);
var waitResult = cancellationEvent.WaitHandle.WaitOne(timeout);
stopwatch.Stop();

var timeoutThreshold = TimeSpan.FromMilliseconds(1000);
Expand All @@ -83,7 +84,6 @@ public static bool Wait(this CancellationToken cancellationToken, TimeSpan timeo
return waitResult;
}

[Obsolete("CancellationToken.WaitHandle is now preferred, since early days of .NET Core passed. Will be removed in 2.0.0.")]
public sealed class CancellationEvent : IDisposable
{
private static readonly Action<object> SetEventCallback = SetEvent;
Expand Down
4 changes: 3 additions & 1 deletion src/Hangfire.Core/Processing/TaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public static bool WaitOne([NotNull] this WaitHandle waitHandle, TimeSpan timeou

token.ThrowIfCancellationRequested();

var waitHandles = new[] { waitHandle, token.WaitHandle };
using var ev = token.GetCancellationEvent();

var waitHandles = new[] { waitHandle, ev.WaitHandle };

var stopwatch = Stopwatch.StartNew();
var waitResult = WaitHandle.WaitAny(waitHandles, timeout);
Expand Down
3 changes: 2 additions & 1 deletion src/Hangfire.Core/Server/CoreBackgroundJobPerformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ private static object InvokeOnTaskPump(PerformContext context, Tuple<MethodInfo,
try
{
using (var syncContext = new InlineSynchronizationContext())
using (var cancellationEvent = context.CancellationToken.ShutdownToken.GetCancellationEvent())
{
SynchronizationContext.SetSynchronizationContext(syncContext);

Expand All @@ -191,7 +192,7 @@ private static object InvokeOnTaskPump(PerformContext context, Tuple<MethodInfo,
var task = getTaskFunc(result);
var asyncResult = (IAsyncResult)task;

var waitHandles = new[] { syncContext.WaitHandle, asyncResult.AsyncWaitHandle, context.CancellationToken.ShutdownToken.WaitHandle };
var waitHandles = new[] { syncContext.WaitHandle, asyncResult.AsyncWaitHandle, cancellationEvent.WaitHandle };

while (!asyncResult.IsCompleted && WaitHandle.WaitAny(waitHandles) == 0)
{
Expand Down

0 comments on commit 49a8023

Please sign in to comment.