Skip to content

Commit

Permalink
Prefer using CancellationToken.WaitHandle again
Browse files Browse the repository at this point in the history
We should trust CancellationToken.WaitHandle again, since the early days of .NET Core passed. This helps to avoid using timers unnecessarily on a fully synchronous path, and we now also have a protection agains problems with waiting.
  • Loading branch information
odinserj committed Jun 11, 2024
1 parent b928ca3 commit 4ced644
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 8 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,6 +28,7 @@ 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 @@ -56,10 +57,8 @@ 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 = cancellationEvent.WaitHandle.WaitOne(timeout);
var waitResult = cancellationToken.WaitHandle.WaitOne(timeout);
stopwatch.Stop();

var timeoutThreshold = TimeSpan.FromMilliseconds(1000);
Expand All @@ -84,6 +83,7 @@ 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: 1 addition & 3 deletions src/Hangfire.Core/Processing/TaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public static bool WaitOne([NotNull] this WaitHandle waitHandle, TimeSpan timeou

token.ThrowIfCancellationRequested();

using var ev = token.GetCancellationEvent();

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

var stopwatch = Stopwatch.StartNew();
var waitResult = WaitHandle.WaitAny(waitHandles, timeout);
Expand Down
3 changes: 1 addition & 2 deletions src/Hangfire.Core/Server/CoreBackgroundJobPerformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ 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 @@ -192,7 +191,7 @@ private static object InvokeOnTaskPump(PerformContext context, Tuple<MethodInfo,
var task = getTaskFunc(result);
var asyncResult = (IAsyncResult)task;

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

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

0 comments on commit 4ced644

Please sign in to comment.